mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
simplify ServerAddress serde implementations
This commit is contained in:
parent
87c2bc2e5d
commit
af818b3b58
1 changed files with 14 additions and 18 deletions
|
@ -76,30 +76,26 @@ impl Display for ServerAddress {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
/// Serde deserialization for ServerAddress. This is useful for config file
|
||||||
/// Serde Deserialization for ServerAddress
|
/// usage.
|
||||||
/// This is necessary for config file usage
|
|
||||||
/// We are not using TryFrom because we want to use the serde error system
|
|
||||||
///
|
|
||||||
impl<'de> serde::Deserialize<'de> for ServerAddress {
|
impl<'de> serde::Deserialize<'de> for ServerAddress {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
let string = String::deserialize(deserializer)?;
|
let string = String::deserialize(deserializer)?;
|
||||||
let mut parts = string.split(':');
|
ServerAddress::try_from(string.as_str()).map_err(serde::de::Error::custom)
|
||||||
let host = parts.next().ok_or(serde::de::Error::custom("No host specified"))?.to_string();
|
|
||||||
// default the port to 25565
|
|
||||||
let port = parts.next().unwrap_or("25565");
|
|
||||||
let port = u16::from_str(port).map_err(|_| serde::de::Error::custom("Invalid port specified"))?;
|
|
||||||
Ok(ServerAddress { host, port })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
/// Serde serialization for ServerAddress. This uses the Display impl, so it
|
||||||
/// Serde Serialization for ServerAddress
|
/// will serialize to a string.
|
||||||
/// Pretty much like impl Display
|
|
||||||
///
|
|
||||||
impl serde::Serialize for ServerAddress {
|
impl serde::Serialize for ServerAddress {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
serializer.serialize_str(&format!("{}:{}", self.host, self.port))
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(&self.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue