Skip to content

Commit

Permalink
Add optional __path__ and __required__ fields in Config deseria…
Browse files Browse the repository at this point in the history
…lization
  • Loading branch information
fuzzypixelz committed Nov 30, 2023
1 parent 278c486 commit 2ac02b3
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const DEFAULT_HTTP_INTERFACE: &str = "0.0.0.0";
pub(crate) struct Config {
#[serde(deserialize_with = "deserialize_http_port")]
pub(crate) http_port: String,
__required__: Option<bool>,
#[serde(default, deserialize_with = "deserialize_path")]
__path__: Option<Vec<String>>,
}

fn deserialize_http_port<'de, D>(deserializer: D) -> Result<String, D::Error>
Expand Down Expand Up @@ -67,3 +70,66 @@ impl<'de> Visitor<'de> for HttpPortVisitor {
Ok(format!("{interface}:{port}"))
}
}

fn deserialize_path<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_option(OptPathVisitor)
}

struct OptPathVisitor;

impl<'de> serde::de::Visitor<'de> for OptPathVisitor {
type Value = Option<Vec<String>>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "none or a string or an array of strings")
}

fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}

fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(PathVisitor).map(Some)
}
}

struct PathVisitor;

impl<'de> serde::de::Visitor<'de> for PathVisitor {
type Value = Vec<String>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a string or an array of strings")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(vec![v.into()])
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut v = if let Some(l) = seq.size_hint() {
Vec::with_capacity(l)
} else {
Vec::new()
};
while let Some(s) = seq.next_element()? {
v.push(s);
}
Ok(v)
}
}

0 comments on commit 2ac02b3

Please sign in to comment.