1package sessionwire
2
3import (
4 "github.com/bnema/vev/internal/protocol"
5 "github.com/bnema/vev/internal/protocol/wire"
6)
7
8func suspendAttachmentToWire(m protocol.SuspendAttachment) (*wire.SuspendAttachment, error) {
9 if err := m.Validate(); err != nil {
10 return nil, err
11 }
12 return &wire.SuspendAttachment{RequestId: m.RequestID}, nil
13}
14func suspendAttachmentFromWire(m *wire.SuspendAttachment) (protocol.SuspendAttachment, error) {
15 out := protocol.SuspendAttachment{RequestID: m.GetRequestId()}
16 return out, out.Validate()
17}
18func attachmentSuspendedToWire(m protocol.AttachmentSuspended) (*wire.AttachmentSuspended, error) {
19 if err := m.Validate(); err != nil {
20 return nil, err
21 }
22 return &wire.AttachmentSuspended{RequestId: m.RequestID, Target: exactTargetToWire(&m.Target)}, nil
23}
24func attachmentSuspendedFromWire(m *wire.AttachmentSuspended) (protocol.AttachmentSuspended, error) {
25 target, err := exactTargetFromWire(m.GetTarget())
26 if err != nil || target == nil {
27 return protocol.AttachmentSuspended{}, protocol.ErrInvalidAttachment
28 }
29 out := protocol.AttachmentSuspended{RequestID: m.GetRequestId(), Target: *target}
30 return out, out.Validate()
31}
32func activateAttachmentToWire(m protocol.ActivateAttachment) (*wire.ActivateAttachment, error) {
33 if err := m.Validate(); err != nil {
34 return nil, err
35 }
36 cols, rows, pw, ph, err := geometryToWire(m.Size, m.PixelWidth, m.PixelHeight)
37 if err != nil {
38 return nil, err
39 }
40 return &wire.ActivateAttachment{RequestId: m.RequestID, Target: exactTargetToWire(&m.Target), Cols: cols, Rows: rows, PixelWidth: pw, PixelHeight: ph}, nil
41}
42func activateAttachmentFromWire(m *wire.ActivateAttachment) (protocol.ActivateAttachment, error) {
43 target, err := exactTargetFromWire(m.GetTarget())
44 if err != nil || target == nil {
45 return protocol.ActivateAttachment{}, protocol.ErrInvalidAttachment
46 }
47 geometry, err := resizeFromWire(&wire.Resize{Cols: m.GetCols(), Rows: m.GetRows(), PixelWidth: m.GetPixelWidth(), PixelHeight: m.GetPixelHeight()})
48 if err != nil {
49 return protocol.ActivateAttachment{}, err
50 }
51 out := protocol.ActivateAttachment{RequestID: m.GetRequestId(), Target: *target, Size: geometry.Size, PixelWidth: geometry.PixelWidth, PixelHeight: geometry.PixelHeight}
52 return out, out.Validate()
53}
54func attachmentActivatedToWire(m protocol.AttachmentActivated) (*wire.AttachmentActivated, error) {
55 if err := m.Validate(); err != nil {
56 return nil, err
57 }
58 identity, err := committedIdentityToWire(m.Identity)
59 if err != nil {
60 return nil, err
61 }
62 return &wire.AttachmentActivated{RequestId: m.RequestID, Identity: identity, Epoch: m.Epoch, State: m.State, ViewPublication: m.ViewPublication}, nil
63}
64func attachmentActivatedFromWire(m *wire.AttachmentActivated) (protocol.AttachmentActivated, error) {
65 identity, err := committedIdentityFromWire(m.GetIdentity())
66 if err != nil {
67 return protocol.AttachmentActivated{}, protocol.ErrInvalidAttachment
68 }
69 out := protocol.AttachmentActivated{RequestID: m.GetRequestId(), Identity: identity, Epoch: m.GetEpoch(), State: m.GetState(), ViewPublication: m.GetViewPublication()}
70 return out, out.Validate()
71}