Skip to content

Commit 1e95c18

Browse files
committed
chore(data): create data crate
1 parent 9dec7d8 commit 1e95c18

File tree

8 files changed

+296
-0
lines changed

8 files changed

+296
-0
lines changed

.idea/OpenSyndicationProtocol.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.lock

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/data/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "data"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
uuid = { version = "1.9.1", features = ["v4"] }
8+
serde = { version = "1.0.203", optional = true }
9+
tokio = { version = "1", features = ["full"] }
10+
tokio-byteorder = "0.3.0"
11+
bytes = "1.6.0"
12+
13+
[features]
14+
default = ["serde"]
15+
serde = ["dep:serde"]

crates/data/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use uuid::Uuid;
2+
3+
#[cfg(feature = "serde")]
4+
pub mod serde;
5+
mod ser;
6+
7+
pub struct DataType<T> {
8+
id: Uuid
9+
}

crates/data/src/ser.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use bytes::BytesMut;
2+
3+
pub struct DataSerializer<T> {
4+
pub(crate) output: BytesMut,
5+
pub(crate) bytes_written: usize,
6+
}
7+
8+
impl<T> DataSerializer<T> {
9+
pub fn new() -> Self {
10+
DataSerializer {
11+
output: BytesMut::new(),
12+
bytes_written: 0,
13+
}
14+
}
15+
}
16+
17+
pub trait SerializeData {
18+
19+
}

crates/data/src/serde/error.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use serde::{de, ser};
2+
use std::fmt::{self, Display};
3+
4+
pub type Result<T> = std::result::Result<T, Error>;
5+
6+
// This is a bare-bones implementation. A real library would provide additional
7+
// information in its error type, for example the line and column at which the
8+
// error occurred, the byte offset into the input, or the current key being
9+
// processed.
10+
#[derive(Debug)]
11+
pub enum Error {
12+
// One or more variants that can be created by data structures through the
13+
// `ser::Error` and `de::Error` traits. For example the Serialize impl for
14+
// Mutex<T> might return an error because the mutex is poisoned, or the
15+
// Deserialize impl for a struct may return an error because a required
16+
// field is missing.
17+
Message(String),
18+
19+
// Zero or more variants that can be created directly by the Serializer and
20+
// Deserializer without going through `ser::Error` and `de::Error`. These
21+
// are specific to the format, in this case JSON.
22+
Eof,
23+
// Syntax,
24+
// ExpectedBoolean,
25+
// ExpectedInteger,
26+
// ExpectedString,
27+
// ExpectedNull,
28+
// ExpectedArray,
29+
// ExpectedArrayComma,
30+
// ExpectedArrayEnd,
31+
// ExpectedMap,
32+
// ExpectedMapColon,
33+
// ExpectedMapComma,
34+
// ExpectedMapEnd,
35+
// ExpectedEnum,
36+
// TrailingCharacters,
37+
}
38+
39+
impl ser::Error for Error {
40+
fn custom<T: Display>(msg: T) -> Self {
41+
Error::Message(msg.to_string())
42+
}
43+
}
44+
45+
impl de::Error for Error {
46+
fn custom<T: Display>(msg: T) -> Self {
47+
Error::Message(msg.to_string())
48+
}
49+
}
50+
51+
impl Display for Error {
52+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53+
match self {
54+
Error::Message(msg) => write!(f, "{}", msg),
55+
Error::Eof => f.write_str("unexpected end of input"),
56+
/* and so forth */
57+
_ => unimplemented!(),
58+
}
59+
}
60+
}
61+
62+
impl std::error::Error for Error {}

crates/data/src/serde/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod ser;
2+
mod error;

crates/data/src/serde/ser.rs

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
use bytes::{BufMut, BytesMut};
2+
use serde::{Serialize, ser};
3+
use crate::ser::DataSerializer;
4+
use crate::serde::error::{Error, Result};
5+
6+
pub fn to_bytes<'ser, T>(value: &T) -> Result<&'ser mut BytesMut>
7+
where
8+
T: Serialize,
9+
{
10+
let mut serializer = DataSerializer::<T>::new();
11+
value.serialize(&mut serializer)?;
12+
Ok(&mut serializer.output)
13+
}
14+
15+
impl<'a, Data> ser::Serializer for &'a mut DataSerializer<T> where Data : Serialize {
16+
type Ok = ();
17+
type Error = Error;
18+
type SerializeSeq = ();
19+
type SerializeTuple = ();
20+
type SerializeTupleStruct = ();
21+
type SerializeTupleVariant = ();
22+
type SerializeMap = ();
23+
type SerializeStruct = ();
24+
type SerializeStructVariant = ();
25+
26+
fn serialize_bool(self, v: bool) -> Result<()> {
27+
self.output.put_u8(v as u8)?
28+
}
29+
30+
fn serialize_i8(self, v: i8) -> Result<()> {
31+
self.output.put_i8(v)?
32+
}
33+
34+
fn serialize_i16(self, v: i16) -> Result<()> {
35+
self.output.put_i16(v)?
36+
}
37+
38+
fn serialize_i32(self, v: i32) -> Result<()> {
39+
self.output.put_i32(v)?
40+
}
41+
42+
fn serialize_i64(self, v: i64) -> Result<()> {
43+
self.output.put_i64(v)?
44+
}
45+
46+
fn serialize_i128(self, v: i128) -> Result<()> {
47+
self.output.put_i128(v)?
48+
}
49+
50+
fn serialize_u8(self, v: u8) -> Result<()> {
51+
self.output.put_u8(v)?
52+
}
53+
54+
fn serialize_u16(self, v: u16) -> Result<()> {
55+
self.output.put_u16(v)?
56+
}
57+
58+
fn serialize_u32(self, v: u32) -> Result<()> {
59+
self.output.put_u32(v)?
60+
}
61+
62+
fn serialize_u64(self, v: u64) -> Result<()> {
63+
self.output.put_u64(v)?
64+
}
65+
66+
fn serialize_u128(self, v: u128) -> Result<()> {
67+
self.output.put_u128(v)?
68+
}
69+
70+
fn serialize_f32(self, v: f32) -> Result<()> {
71+
self.output.put_f32(v)?
72+
}
73+
74+
fn serialize_f64(self, v: f64) -> Result<()> {
75+
self.output.put_f64(v)?
76+
}
77+
78+
fn serialize_char(self, v: char) -> Result<()> {
79+
self.output.put_u8(v as u8)?
80+
}
81+
82+
fn serialize_str(self, v: &str) -> Result<()> {
83+
let bytes = v.as_bytes();
84+
self.output.put_u16(bytes.len() as u16);
85+
self.output.put_slice(bytes);
86+
Ok(())
87+
}
88+
89+
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
90+
todo!()
91+
}
92+
93+
fn serialize_none(self) -> Result<()> {
94+
todo!()
95+
}
96+
97+
fn serialize_some<T>(self, value: &T) -> Result<()>
98+
where
99+
T: ?Sized + Serialize
100+
{
101+
todo!()
102+
}
103+
104+
fn serialize_unit(self) -> Result<()> {
105+
todo!()
106+
}
107+
108+
fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
109+
todo!()
110+
}
111+
112+
fn serialize_unit_variant(self, name: &'static str, variant_index: u32, variant: &'static str) -> Result<()> {
113+
todo!()
114+
}
115+
116+
fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<()>
117+
where
118+
T: ?Sized + Serialize
119+
{
120+
todo!()
121+
}
122+
123+
fn serialize_newtype_variant<T>(self, name: &'static str, variant_index: u32, variant: &'static str, value: &T) -> Result<()>
124+
where
125+
T: ?Sized + Serialize
126+
{
127+
todo!()
128+
}
129+
130+
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
131+
todo!()
132+
}
133+
134+
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
135+
todo!()
136+
}
137+
138+
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeTupleStruct, Self::Error> {
139+
todo!()
140+
}
141+
142+
fn serialize_tuple_variant(self, name: &'static str, variant_index: u32, variant: &'static str, len: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
143+
todo!()
144+
}
145+
146+
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
147+
todo!()
148+
}
149+
150+
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, Self::Error> {
151+
todo!()
152+
}
153+
154+
fn serialize_struct_variant(self, name: &'static str, variant_index: u32, variant: &'static str, len: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
155+
todo!()
156+
}
157+
}

0 commit comments

Comments
 (0)