-
Notifications
You must be signed in to change notification settings - Fork 30
/
frames.rs
164 lines (148 loc) · 4.93 KB
/
frames.rs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use atrium_api::com::atproto::sync::subscribe_repos::{Commit, Message};
use libipld_core::ipld::Ipld;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io::Cursor;
#[derive(Debug, Clone, PartialEq, Eq)]
struct FrameTypeError;
impl Display for FrameTypeError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid frame type")
}
}
impl Error for FrameTypeError {}
// original definition:
//```
// export enum FrameType {
// Message = 1,
// Error = -1,
// }
// export const messageFrameHeader = z.object({
// op: z.literal(FrameType.Message), // Frame op
// t: z.string().optional(), // Message body type discriminator
// })
// export type MessageFrameHeader = z.infer<typeof messageFrameHeader>
// export const errorFrameHeader = z.object({
// op: z.literal(FrameType.Error),
// })
// export type ErrorFrameHeader = z.infer<typeof errorFrameHeader>
// ```
#[derive(Debug, Clone, PartialEq, Eq)]
enum FrameHeader {
Message(Option<String>),
Error,
}
impl TryFrom<Ipld> for FrameHeader {
type Error = FrameTypeError;
fn try_from(value: Ipld) -> Result<Self, <FrameHeader as TryFrom<Ipld>>::Error> {
if let Ipld::Map(map) = value {
if let Some(Ipld::Integer(i)) = map.get("op") {
match i {
1 => {
let t = if let Some(Ipld::String(s)) = map.get("t") {
Some(s.clone())
} else {
None
};
return Ok(FrameHeader::Message(t));
}
-1 => return Ok(FrameHeader::Error),
_ => {}
}
}
}
Err(FrameTypeError)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Frame {
Message(Box<MessageFrame>),
Error(ErrorFrame),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageFrame {
pub body: Message,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorFrame {
// TODO
// body: Value,
}
impl TryFrom<&[u8]> for Frame {
type Error = Box<dyn Error>;
fn try_from(value: &[u8]) -> Result<Self, <Frame as TryFrom<&[u8]>>::Error> {
let mut cursor = Cursor::new(value);
let (left, right) = match serde_ipld_dagcbor::from_reader::<Ipld, _>(&mut cursor) {
Err(serde_ipld_dagcbor::DecodeError::TrailingData) => {
value.split_at(cursor.position() as usize)
}
_ => {
// TODO
return Err(Box::new(FrameTypeError));
}
};
let ipld = serde_ipld_dagcbor::from_slice::<Ipld>(left)?;
let header = FrameHeader::try_from(ipld)?;
match header {
FrameHeader::Message(t) => match t.as_deref() {
Some("#commit") => Ok(Frame::Message(Box::new(MessageFrame {
body: Message::Commit(Box::new(serde_ipld_dagcbor::from_slice::<Commit>(
right,
)?)),
}))),
_ => unimplemented!("{t:?}"),
},
FrameHeader::Error => Ok(Frame::Error(ErrorFrame {})),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn serialized_data(s: &str) -> Vec<u8> {
assert!(s.len() % 2 == 0);
let b2u = |b: u8| match b {
b'0'..=b'9' => b - b'0',
b'a'..=b'f' => b - b'a' + 10,
_ => unreachable!(),
};
s.as_bytes()
.chunks(2)
.map(|b| (b2u(b[0]) << 4) + b2u(b[1]))
.collect()
}
#[test]
fn deserialize_message_frame_header() {
// {"op": 1, "t": "#commit"}
let data = serialized_data("a2626f700161746723636f6d6d6974");
let ipld = serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
assert_eq!(
FrameHeader::try_from(ipld),
Ok(FrameHeader::Message(Some(String::from("#commit"))))
);
}
#[test]
fn deserialize_error_frame_header() {
// {"op": -1}
let data = serialized_data("a1626f7020");
let ipld = serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
assert_eq!(FrameHeader::try_from(ipld), Ok(FrameHeader::Error));
}
#[test]
fn deserialize_invalid_frame_header() {
{
// {"op": 2, "t": "#commit"}
let data = serialized_data("a2626f700261746723636f6d6d6974");
let ipld =
serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
assert_eq!(FrameHeader::try_from(ipld), Err(FrameTypeError));
}
{
// {"op": -2}
let data = serialized_data("a1626f7021");
let ipld =
serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
assert_eq!(FrameHeader::try_from(ipld), Err(FrameTypeError));
}
}
}