1use std::error;
2use std::fmt;
3use std::io;
4use std::result;
5
6use crate::metadata::*;
7use crate::proto::*;
8
9mod append;
10mod jtop;
11mod ptoj;
12
13pub use jtop::trans_json_to_proto;
14pub use ptoj::trans_proto_to_json;
15
16#[derive(Debug)]
17pub enum Error {
18 UnexpectedEof,
19 UnexpectedToken,
20 TypeMismatch,
21 InvalidWireType,
22 Io(io::Error),
23 Wrap(Box<dyn error::Error>),
24}
25
26impl fmt::Display for Error {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match &self {
29 Error::UnexpectedEof => f.write_str("unexpected eof"),
30 Error::UnexpectedToken => f.write_str("unexpected token"),
31 Error::TypeMismatch => f.write_str("type mismatch"),
32 Error::InvalidWireType => f.write_str("invalid wire-type"),
33 Error::Io(e) => write!(f, "io: {}", e),
34 Error::Wrap(e) => write!(f, "wrap: {}", e),
35 }
36 }
37}
38
39impl error::Error for Error {}
40
41impl From<io::Error> for Error {
42 fn from(e: io::Error) -> Self {
43 Error::Io(e)
44 }
45}
46
47pub type Result<T> = result::Result<T, Error>;
48
49pub(self) fn wire_type(ty: &Type) -> u32 {
50 match ty {
51 Type::Double => WIRE_64BIT,
52 Type::Float => WIRE_32BIT,
53 Type::Int32 => WIRE_VARINT,
54 Type::Int64 => WIRE_VARINT,
55 Type::Uint32 => WIRE_VARINT,
56 Type::Uint64 => WIRE_VARINT,
57 Type::Sint32 => WIRE_VARINT,
58 Type::Sint64 => WIRE_VARINT,
59 Type::Fixed32 => WIRE_32BIT,
60 Type::Fixed64 => WIRE_64BIT,
61 Type::Sfixed32 => WIRE_32BIT,
62 Type::Sfixed64 => WIRE_64BIT,
63 Type::Bool => WIRE_VARINT,
64 Type::String => WIRE_LEN_DELIM,
65 Type::Bytes => WIRE_LEN_DELIM,
66 Type::Array(_) => WIRE_LEN_DELIM,
67 Type::Map(_, _) => WIRE_LEN_DELIM,
68 Type::Message(_) => WIRE_LEN_DELIM,
69 }
70}
71
72#[cfg(test)]
73pub mod tests {
74 use std::rc::Rc;
75
76 use super::*;
77
78 pub fn printable(s: &[u8]) -> String {
79 s.iter()
80 .map(|&v| v.to_string())
81 .collect::<Vec<_>>()
82 .join(" ")
83 }
84
85 pub fn get_msg_elem_type() -> Type {
86 Type::Message(Message::new(
87 "pbmsg.Elem".to_string(),
88 vec![
89 Field {
90 name: "a".to_string(),
91 tag: 1,
92 ty: Rc::new(Type::Int32),
93 },
94 Field {
95 name: "s".to_string(),
96 tag: 2,
97 ty: Rc::new(Type::String),
98 },
99 ],
100 true,
101 ))
102 }
103
104 pub fn get_msg_foo_embed_type() -> Type {
105 Type::Message(Message::new(
106 "pbmsg.Foo.Embed".to_string(),
107 vec![
108 Field {
109 name: "a".to_string(),
110 tag: 1,
111 ty: Rc::new(Type::Int32),
112 },
113 Field {
114 name: "b".to_string(),
115 tag: 2,
116 ty: Rc::new(Type::String),
117 },
118 ],
119 true,
120 ))
121 }
122
123 pub fn get_msg_foo_type() -> Type {
124 Type::Message(Message::new(
125 "pbmsg.Foo".to_string(),
126 vec![
127 Field {
128 name: "a".to_string(),
129 tag: 1,
130 ty: Rc::new(Type::String),
131 },
132 Field {
133 name: "b".to_string(),
134 tag: 2,
135 ty: Rc::new(Type::Bool),
136 },
137 Field {
138 name: "c".to_string(),
139 tag: 3,
140 ty: Rc::new(Type::Int32),
141 },
142 Field {
143 name: "d".to_string(),
144 tag: 4,
145 ty: Rc::new(get_msg_foo_embed_type()),
146 },
147 Field {
148 name: "e".to_string(),
149 tag: 5,
150 ty: Rc::new(Type::Array(Rc::new(Type::Int32))),
151 },
152 Field {
153 name: "f".to_string(),
154 tag: 6,
155 ty: Rc::new(Type::Array(Rc::new(Type::String))),
156 },
157 Field {
158 name: "g".to_string(),
159 tag: 7,
160 ty: Rc::new(Type::Array(Rc::new(get_msg_elem_type()))),
161 },
162 ],
163 true,
164 ))
165 }
166}