File: message.go

package info (click to toggle)
aerc 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (52 lines) | stat: -rw-r--r-- 1,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ipc

import "encoding/json"

// Request contains all parameters needed for the main instance to respond to
// a request.
type Request struct {
	// Arguments contains the commandline arguments. The detection of what
	// action to take is left to the receiver.
	Arguments []string `json:"arguments"`
}

// Response is used to report the results of a command.
type Response struct {
	// Error contains the success-state of the command. Error is an empty
	// string if everything ran successfully.
	Error string `json:"error"`
}

// Encode transforms the message in an easier to transfer format
func (msg *Request) Encode() ([]byte, error) {
	return json.Marshal(msg)
}

// DecodeMessage consumes a raw message and returns the message contained
// within.
func DecodeMessage(data []byte) (*Request, error) {
	msg := new(Request)
	err := json.Unmarshal(data, msg)
	return msg, err
}

// Encode transforms the message in an easier to transfer format
func (msg *Response) Encode() ([]byte, error) {
	return json.Marshal(msg)
}

// DecodeRequest consumes a raw message and returns the message contained
// within.
func DecodeRequest(data []byte) (*Request, error) {
	msg := new(Request)
	err := json.Unmarshal(data, msg)
	return msg, err
}

// DecodeResponse consumes a raw message and returns the message contained
// within.
func DecodeResponse(data []byte) (*Response, error) {
	msg := new(Response)
	err := json.Unmarshal(data, msg)
	return msg, err
}