Skip to content

Commit

Permalink
add serialize and deserialize to step
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaOber committed Dec 14, 2024
1 parent db2fd7e commit 8fb2611
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions charming/src/element/step.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,77 @@
use serde::Serialize;
use serde::{de::Visitor, Deserialize, Serialize};

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "snake_case")]
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub enum Step {
True,
False,
Start,
Middle,
End,
}

impl From<bool> for Step {
fn from(value: bool) -> Self {
if value {
Step::True
} else {
Step::False
}
}
}

impl Serialize for Step {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Step::True => serializer.serialize_bool(true),
Step::False => serializer.serialize_bool(false),
Step::Start => serializer.serialize_str("start"),
Step::Middle => serializer.serialize_str("middle"),
Step::End => serializer.serialize_str("end"),
}
}
}

impl<'de> Deserialize<'de> for Step {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct StepVisitor;

impl Visitor<'_> for StepVisitor {
type Value = Step;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str(
"a boolean or a string containing either \"start\", \"middle\" or \"end\"",
)
}

fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
true => Ok(Step::True),
false => Ok(Step::False),
}
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
"start" => Ok(Step::Start),
"middle" => Ok(Step::Middle),
"end" => Ok(Step::End),
_ => Err(E::custom("unable to parse step type, invalid string")),
}
}
}
deserializer.deserialize_any(StepVisitor)
}
}

0 comments on commit 8fb2611

Please sign in to comment.