fix(rust): create custom serializer

This commit is contained in:
Artemy Egorov 2024-07-28 21:37:42 +03:00
parent 71db51b960
commit 306cc30791
3 changed files with 32 additions and 6 deletions

2
libs/rust/Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 3
[[package]] [[package]]
name = "dalet" name = "dalet"
version = "1.0.0-pre0" version = "1.0.0-pre1"
dependencies = [ dependencies = [
"serde", "serde",
] ]

View file

@ -1,6 +1,6 @@
[package] [package]
name = "dalet" name = "dalet"
version = "1.0.0-pre0" version = "1.0.0-pre1"
edition = "2021" edition = "2021"
authors = ["artegoser"] authors = ["artegoser"]
license = "MIT" license = "MIT"

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::Serialize;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Debug)]
pub struct Tag { pub struct Tag {
id: i8, id: i8,
body: Body, body: Body,
@ -13,16 +13,42 @@ impl Tag {
} }
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Debug)]
pub enum Body { pub enum Body {
Text(String), Text(String),
Tag(Box<Tag>), Tag(Box<Tag>),
Null, Null,
} }
#[derive(Serialize, Deserialize, Debug)] impl Serialize for Body {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match *self {
Body::Text(ref text) => serializer.serialize_str(text),
Body::Tag(ref tag) => tag.serialize(serializer),
Body::Null => serializer.serialize_str("null"),
}
}
}
#[derive(Debug)]
pub enum Argument { pub enum Argument {
Text(String), Text(String),
Number(i8), Number(i8),
Null, Null,
} }
impl Serialize for Argument {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match *self {
Argument::Text(ref text) => serializer.serialize_str(text),
Argument::Number(number) => serializer.serialize_i8(number),
Argument::Null => serializer.serialize_str("null"),
}
}
}