Skip to content

Commit

Permalink
chore: impl Ser + Deser + Hash for SizedAsciiString (#1077)
Browse files Browse the repository at this point in the history
SizedAsciiString is now serde serializable/deserializable

Co-authored-by: Rashad Alston <rashad@Rashads-Air.lan>
Co-authored-by: hal3e <git@hal3e.io>
  • Loading branch information
3 people authored Aug 9, 2023
1 parent c64aeca commit 7634e40
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion packages/fuels-core/src/types/core/sized_ascii_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{Debug, Display, Formatter};

use crate::types::errors::{error, Error, Result};
use serde::{Deserialize, Serialize};

// To be used when interacting with contracts which have string slices in their ABI.
// The FuelVM strings only support ascii characters.
Expand Down Expand Up @@ -72,7 +73,7 @@ impl PartialEq<AsciiString> for &str {
// To be used when interacting with contracts which have strings in their ABI.
// The length of a string is part of its type -- i.e. str[2] is a
// different type from str[3]. The FuelVM strings only support ascii characters.
#[derive(Debug, PartialEq, Clone, Eq)]
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub struct SizedAsciiString<const LEN: usize> {
data: String,
}
Expand Down Expand Up @@ -151,12 +152,31 @@ impl<const LEN: usize> PartialEq<&str> for SizedAsciiString<LEN> {
self.data == *other
}
}

impl<const LEN: usize> PartialEq<SizedAsciiString<LEN>> for &str {
fn eq(&self, other: &SizedAsciiString<LEN>) -> bool {
*self == other.data
}
}

impl<const LEN: usize> Serialize for SizedAsciiString<LEN> {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> core::result::Result<S::Ok, S::Error> {
self.data.serialize(serializer)
}
}

impl<'de, const LEN: usize> Deserialize<'de> for SizedAsciiString<LEN> {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> core::result::Result<Self, D::Error> {
let data = String::deserialize(deserializer)?;
Self::new(data).map_err(serde::de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -245,4 +265,23 @@ mod tests {

Ok(())
}

#[test]
fn test_can_serialize_sized_ascii() {
let sized_str = SizedAsciiString::<3>::new("abc".to_string()).unwrap();

let serialized = serde_json::to_string(&sized_str).unwrap();
assert_eq!(serialized, "\"abc\"");
}

#[test]
fn test_can_deserialize_sized_ascii() {
let serialized = "\"abc\"";

let deserialized: SizedAsciiString<3> = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized,
SizedAsciiString::<3>::new("abc".to_string()).unwrap()
);
}
}

0 comments on commit 7634e40

Please sign in to comment.