Skip to content

Commit

Permalink
Refactor serialization and add error handling in blk_type.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareFlo committed Jan 16, 2024
1 parent 2271667 commit 7aa1f43
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
30 changes: 21 additions & 9 deletions src/blk/blk_type.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{
fmt::{Display, Formatter},
fmt::{Display, Formatter as StdFormatter},
sync::Arc,
};
use color_eyre::eyre::bail;
use color_eyre::Report;

use serde::{Deserialize, Serialize, Serializer};

Check warning on line 8 in src/blk/blk_type.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `Serializer`
use serde::ser::{SerializeSeq, SerializeStruct};

Check warning on line 9 in src/blk/blk_type.rs

View workflow job for this annotation

GitHub Actions / lint

unused imports: `SerializeSeq`, `SerializeStruct`
use serde_json::ser::PrettyFormatter;
use serde_json::ser::{Formatter, PrettyFormatter};

use crate::blk::{
blk_type::blk_type_id::*,
Expand Down Expand Up @@ -270,18 +272,20 @@ impl BlkType {
"c"
)
}
pub fn serialize_streaming(self, w: &mut serde_json::Serializer<Vec<u8>, PrettyFormatter>) {
pub fn serialize_streaming(&self, w: &mut Vec<u8>, ser: &mut PrettyFormatter) -> Result<(), Report> {
bail!("fuck");
match self {

Check warning on line 277 in src/blk/blk_type.rs

View workflow job for this annotation

GitHub Actions / lint

unreachable statement
BlkType::Str(s) => {
w.serialize_str(&s).unwrap();
ser.begin_string(w)?;
ser.write_string_fragment(w, s.as_ref())?;
ser.end_string(w)?;
}
BlkType::Int(s) => {
w.serialize_i32(s).unwrap();
ser.write_i32(w, *s)?;
}
BlkType::Int2(s) => {

Check warning on line 286 in src/blk/blk_type.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
let mut seq = w.serialize_seq(Some(2)).unwrap();
seq.serialize_element(&s).unwrap();
SerializeSeq::end(seq).unwrap();
ser.begin_array(w)?;
//ser.begin_ar
}
BlkType::Int3(s) => {

Check warning on line 290 in src/blk/blk_type.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
()
Expand All @@ -297,11 +301,19 @@ impl BlkType {
BlkType::Bool(s) => {}
BlkType::Color { r, g, b, a } => {}
}
Ok(())
}
}

fn ser_array_type<T>(arr_writer: fn(&mut Vec<u8>, &mut PrettyFormatter) -> Result<(), Report>, w: &mut Vec<u8>, ser: &mut PrettyFormatter) -> Result<(), Report> {
ser.begin_array(w)?;
arr_writer(w, ser)?;
ser.end_array(w)?;
Ok(())
}

impl Display for BlkType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut StdFormatter<'_>) -> std::fmt::Result {
let value = match self {
BlkType::Str(v) => {
format!("\"{}\"", v)
Expand Down
27 changes: 20 additions & 7 deletions src/blk/plaintext_serialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use color_eyre::Report;
use serde::ser::{SerializeMap, SerializeSeq, SerializeStruct};

Check warning on line 4 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused imports: `SerializeMap`, `SerializeSeq`, `SerializeStruct`
use serde::Serializer;

Check warning on line 5 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `serde::Serializer`
use serde_json::{json, Number, Value};
use serde_json::ser::PrettyFormatter;
use serde_json::ser::{Formatter, PrettyFormatter};

use crate::blk::{blk_structure::BlkField, blk_type::BlkType};
use crate::blk::blk_type::BlkString;
Expand Down Expand Up @@ -114,17 +114,31 @@ impl BlkField {
}
}

pub fn as_serde_json_streaming(&self, w: &mut serde_json::Serializer<Vec<u8>, PrettyFormatter>, apply_overrides: bool) -> Result<(), Report> {
pub fn as_serde_json_streaming(&self, w: &mut Vec<u8>, apply_overrides: bool) -> Result<(), Report> {
let mut ser = PrettyFormatter::with_indent(b"\t");
self._as_serde_json_streaming(w, apply_overrides, &mut ser)?;
Ok(())
}

fn _as_serde_json_streaming(&self, w: &mut Vec<u8>, apply_overrides: bool, ser: &mut PrettyFormatter) -> Result<(), Report> {
#[inline(always)]
fn std_num(num: f32) -> Value {
Value::Number(Number::from_str(&format!("{:?}", num)).expect("Infallible"))
}

match self {
BlkField::Value(k, v) => {
ser.begin_object_key(w, false)?;
ser.write_string_fragment(w, k.as_ref())?;
ser.begin_object_value(w)?;
v.serialize_streaming(w, ser)?;
}
BlkField::Struct(k, v) => {

ser.begin_object(w)?;
for value in v {
value._as_serde_json_streaming(w, apply_overrides, ser)?;
}
ser.end_object(w)?;
}
BlkField::Merged(k, v) => {}
}
Expand Down Expand Up @@ -284,10 +298,9 @@ mod test {
let mut blk = make_strict_test();
// println!("Found: {:#?}", blk.as_serde_obj());
// println!("Expected: {:#?}", expected);
let buf = vec![];
let mut ser = Serializer::pretty(buf);
blk.as_serde_json_streaming(&mut ser, false).unwrap();
println!("{}", String::from_utf8(ser.into_inner()).unwrap());
let mut buf = vec![];
blk.as_serde_json_streaming(&mut buf, false).unwrap();
println!("{}", String::from_utf8(buf).unwrap());
}

#[test]
Expand Down

0 comments on commit 7aa1f43

Please sign in to comment.