Skip to content

Commit

Permalink
impl From and FromStr functions in some tactical places (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl authored Oct 20, 2023
1 parent 9d7d07f commit 31230cb
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use crate::*;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -228,24 +230,62 @@ pub enum NumberFormat {
Double,
}

impl FromStr for NumberFormat {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"float" => Ok(Self::Float),
"double" => Ok(Self::Double),
_ => Err(()),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum IntegerFormat {
Int32,
Int64,
}

impl FromStr for IntegerFormat {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"int32" => Ok(Self::Int32),
"int64" => Ok(Self::Int64),
_ => Err(()),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
#[serde(rename_all = "kebab-case")]
pub enum StringFormat {
Date,
#[serde(rename = "date-time")]
DateTime,
Password,
Byte,
Binary,
}

impl FromStr for StringFormat {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"date" => Ok(Self::Date),
"date-time" => Ok(Self::DateTime),
"password" => Ok(Self::Password),
"byte" => Ok(Self::Byte),
"binary" => Ok(Self::Binary),
_ => Err(()),
}
}
}

#[cfg(test)]
mod tests {
use serde_json::json;
Expand Down
50 changes: 50 additions & 0 deletions src/variant_or.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand All @@ -7,6 +9,18 @@ pub enum VariantOrUnknown<T> {
Unknown(String),
}

impl<T> From<String> for VariantOrUnknown<T>
where
T: FromStr,
{
fn from(s: String) -> Self {
match T::from_str(&s) {
Ok(t) => Self::Item(t),
Err(_) => Self::Unknown(s),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum VariantOrUnknownOrEmpty<T> {
Expand All @@ -29,3 +43,39 @@ impl<T> Default for VariantOrUnknownOrEmpty<T> {
VariantOrUnknownOrEmpty::Empty
}
}

impl<T> From<Option<String>> for VariantOrUnknownOrEmpty<T>
where
T: FromStr,
{
fn from(v: Option<String>) -> Self {
match v {
Some(s) => match T::from_str(&s) {
Ok(t) => VariantOrUnknownOrEmpty::Item(t),
Err(_) => VariantOrUnknownOrEmpty::Unknown(s),
},
None => VariantOrUnknownOrEmpty::Empty,
}
}
}

#[cfg(test)]
mod tests {
use crate::{StringFormat, VariantOrUnknownOrEmpty};

#[test]
fn test_variant_from() {
assert_eq!(
VariantOrUnknownOrEmpty::<StringFormat>::from(None),
VariantOrUnknownOrEmpty::Empty,
);
assert_eq!(
VariantOrUnknownOrEmpty::<StringFormat>::from(Some("date".to_string())),
VariantOrUnknownOrEmpty::Item(StringFormat::Date),
);
assert_eq!(
VariantOrUnknownOrEmpty::<StringFormat>::from(Some("yolo".to_string())),
VariantOrUnknownOrEmpty::Unknown("yolo".to_string()),
);
}
}

0 comments on commit 31230cb

Please sign in to comment.