-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{ | ||
any, | ||
fmt::{self, Formatter}, | ||
}; | ||
|
||
use serde::{ | ||
de::{Error, Visitor}, | ||
Deserialize, Deserializer, Serialize, Serializer, | ||
}; | ||
|
||
use super::name::Name; | ||
|
||
impl Serialize for Name { | ||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { | ||
serializer.serialize_str(self.as_str()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Name { | ||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { | ||
deserializer.deserialize_str(EntityVisitor) | ||
} | ||
} | ||
|
||
struct EntityVisitor; | ||
|
||
impl<'de> Visitor<'de> for EntityVisitor { | ||
type Value = Name; | ||
|
||
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { | ||
formatter.write_str(any::type_name::<Name>()) | ||
} | ||
|
||
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> { | ||
Ok(Name::new(v.to_string())) | ||
} | ||
|
||
fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> { | ||
Ok(Name::new(v)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters