Skip to content

Commit

Permalink
Merge pull request #274 from turbofish-org/lengthstring-deserialize-fix
Browse files Browse the repository at this point in the history
`LengthString` deserialize fix
  • Loading branch information
keppel authored Nov 15, 2024
2 parents 8bc8a4c + 296a691 commit 89e919b
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,11 @@ where
Eq,
Describe,
Serialize,
Deserialize,
)]
#[serde(transparent)]
pub struct LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static,
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone + 'static,
{
#[serde(skip)]
len: P,
Expand All @@ -214,14 +213,31 @@ where
inner: String,
}

impl<'de, P> Deserialize<'de> for LengthString<P>
where
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone + 'static,
{
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let inner = String::deserialize(deserializer)?;
let len = inner
.len()
.try_into()
.map_err(|_| serde::de::Error::custom("overflow"))?;
Ok(LengthString { len, inner })
}
}

impl<P> Migrate for LengthString<P> where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone + 'static
{
}

impl<P> LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone,
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone,
{
pub fn new(len: P, inner: String) -> Self {
LengthString { len, inner }
Expand All @@ -230,7 +246,7 @@ where

impl<P> Decode for LengthString<P>
where
P: Encode + Decode + Terminated + TryInto<usize> + Clone,
P: Encode + Decode + Terminated + TryInto<usize> + TryFrom<usize> + Clone,
{
fn decode<R: std::io::Read>(mut input: R) -> Result<Self> {
let len = P::decode(&mut input)?;
Expand All @@ -252,7 +268,7 @@ where

impl<P> Encode for LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone,
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone,
{
fn encode_into<W: std::io::Write>(&self, mut out: &mut W) -> Result<()> {
self.len.encode_into(&mut out)?;
Expand All @@ -273,12 +289,14 @@ where
}
}

impl<P> Terminated for LengthString<P> where P: Encode + Decode + TryInto<usize> + Terminated + Clone
{}
impl<P> Terminated for LengthString<P> where
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone
{
}

impl<P> State for LengthString<P>
where
P: Encode + Decode + TryInto<usize> + Terminated + Clone + 'static,
P: Encode + Decode + TryInto<usize> + TryFrom<usize> + Terminated + Clone + 'static,
{
fn attach(&mut self, _store: Store) -> crate::Result<()> {
Ok(())
Expand Down

0 comments on commit 89e919b

Please sign in to comment.