Skip to content

Commit

Permalink
Merge pull request #188 from bjohnson5/187-value-range-serialize
Browse files Browse the repository at this point in the history
Fixing the serializer for ValueOrRange
  • Loading branch information
carlaKC authored Jun 25, 2024
2 parents a9aba5d + 0e7cfa8 commit e369d61
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sim-lib/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ pub mod serde_node_id {

pub mod serde_value_or_range {
use super::*;
use serde::de::Error;
use serde::{de::Error, ser::SerializeTuple};

use crate::ValueOrRange;

pub fn serialize<S, T>(x: &ValueOrRange<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
T: std::fmt::Display,
T: serde::Serialize,
{
serializer.serialize_str(&match x {
ValueOrRange::Value(p) => p.to_string(),
ValueOrRange::Range(x, y) => format!("[{}, {}]", x, y),
})
match x {
ValueOrRange::Value(p) => serializer.serialize_some(p),
ValueOrRange::Range(x, y) => {
let mut tup = serializer.serialize_tuple(2)?;
tup.serialize_element(x)?;
tup.serialize_element(y)?;
tup.end()
},
}
}

pub fn deserialize<'de, D, T>(deserializer: D) -> Result<ValueOrRange<T>, D::Error>
Expand Down

0 comments on commit e369d61

Please sign in to comment.