diff options
Diffstat (limited to 'src/status.rs')
-rw-r--r-- | src/status.rs | 73 |
1 files changed, 46 insertions, 27 deletions
diff --git a/src/status.rs b/src/status.rs index 7d254aa..22f75bf 100644 --- a/src/status.rs +++ b/src/status.rs @@ -1,9 +1,12 @@ +use crate::protocol::TestRandom; use crate::types::Chat; -use crate::{SerializeResult, SerializeErr, Serialize as McSerialize, Deserialize as McDeserialize, DeserializeResult, DeserializeErr}; use crate::uuid::UUID4; -use serde::{Serialize, Serializer, Deserialize, Deserializer}; +use crate::{ + Deserialize as McDeserialize, DeserializeErr, DeserializeResult, Serialize as McSerialize, + SerializeErr, SerializeResult, +}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; -use crate::protocol::TestRandom; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct StatusSpec { @@ -16,9 +19,11 @@ pub struct StatusSpec { impl McSerialize for StatusSpec { fn mc_serialize<S: crate::Serializer>(&self, to: &mut S) -> SerializeResult { - serde_json::to_string(self).map_err(move |err| { - SerializeErr::FailedJsonEncode(format!("failed to serialize json status {}", err)) - })?.mc_serialize(to) + serde_json::to_string(self) + .map_err(move |err| { + SerializeErr::FailedJsonEncode(format!("failed to serialize json status {}", err)) + })? + .mc_serialize(to) } } @@ -26,22 +31,24 @@ impl McDeserialize for StatusSpec { fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> { String::mc_deserialize(data)?.try_map(move |v| { serde_json::from_str(v.as_str()).map_err(move |err| { - DeserializeErr::CannotUnderstandValue(format!("failed to deserialize json status {}", err)) + DeserializeErr::CannotUnderstandValue(format!( + "failed to deserialize json status {}", + err + )) }) }) } } - #[cfg(test)] impl TestRandom for StatusSpec { fn test_gen_random() -> Self { Self { - version: StatusVersionSpec{ + version: StatusVersionSpec { protocol: rand::random(), name: String::test_gen_random(), }, - players: StatusPlayersSpec{ + players: StatusPlayersSpec { sample: Vec::default(), max: rand::random(), online: rand::random(), @@ -80,8 +87,9 @@ pub struct StatusFaviconSpec { } impl Serialize for StatusFaviconSpec { - fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where - S: Serializer + fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> + where + S: Serializer, { let data_base64 = base64::encode(self.data.as_slice()); let content = format!("data:{};base64,{}", self.content_type, data_base64); @@ -90,8 +98,9 @@ impl Serialize for StatusFaviconSpec { } impl<'de> Deserialize<'de> for StatusFaviconSpec { - fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error> where - D: Deserializer<'de> + fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error> + where + D: Deserializer<'de>, { struct Visitor; impl serde::de::Visitor<'_> for Visitor { @@ -107,8 +116,8 @@ impl<'de> Deserialize<'de> for StatusFaviconSpec { // regex to parse valid base64 content const PATTERN: &str = r"^data:([A-Za-z/]+);base64,([-A-Za-z0-9+/]*={0,3})$"; lazy_static! { - static ref RE: Regex = Regex::new(PATTERN).expect("regex is valid"); - } + static ref RE: Regex = Regex::new(PATTERN).expect("regex is valid"); + } // try to use regex on the input // RE.captures_iter(v).next() means "try to get the first capture iterator" @@ -117,20 +126,30 @@ impl<'de> Deserialize<'de> for StatusFaviconSpec { // wrap the content_type and parsed data in StatusFaviconSpec // then we convert the option to a result using map and unwrap_or_else let mut captures: regex::CaptureMatches<'_, '_> = RE.captures_iter(v); - captures.next().and_then(move |captures| - captures.get(1).and_then(move |content_type| - captures.get(2).and_then(move |raw_base64| - base64::decode(raw_base64.as_str().as_bytes()).map(move |data| { - StatusFaviconSpec { - content_type: content_type.as_str().to_owned(), - data, - } - }).ok()))) + captures + .next() + .and_then(move |captures| { + captures.get(1).and_then(move |content_type| { + captures.get(2).and_then(move |raw_base64| { + base64::decode(raw_base64.as_str().as_bytes()) + .map(move |data| StatusFaviconSpec { + content_type: content_type.as_str().to_owned(), + data, + }) + .ok() + }) + }) + }) .map(move |result| Ok(result)) - .unwrap_or_else(|| Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self))) + .unwrap_or_else(|| { + Err(serde::de::Error::invalid_value( + serde::de::Unexpected::Str(v), + &self, + )) + }) } } deserializer.deserialize_str(Visitor {}) } -}
\ No newline at end of file +} |