Skip to content

Commit

Permalink
Add support for types from ascii ccrate
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Oct 30, 2023
1 parent 73f7d7f commit dd179a8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion borsh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ required-features = ["std", "unstable__schema"]
cfg_aliases = "0.1.0"

[dependencies]
ascii = { version = "1.1", optional = true }
borsh-derive = { path = "../borsh-derive", version = "~1.1.1", optional = true }

# hashbrown can be used in no-std context.
# NOTE: There is no reason to restrict use of older versions, but we don't want to get
# NOTE: There is no reason to restrict use of older versions, but we don't want to get
# sudden breaking changes with an open range of versions, so we limit the range by not yet released 0.15.0 version:
hashbrown = { version = ">=0.11,<0.15.0", optional = true }
bytes = { version = "1", optional = true }
Expand Down
22 changes: 22 additions & 0 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,28 @@ impl BorshDeserialize for String {
}
}

#[cfg(feature = "ascii")]
impl BorshDeserialize for ascii::AsciiString {
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let bytes = Vec::<u8>::deserialize_reader(reader)?;
ascii::AsciiString::from_ascii(bytes)
.map_err(|err| Error::new(ErrorKind::InvalidData, err.to_string()))
}
}

#[cfg(feature = "ascii")]
#[test]
fn test_ascii() {
let encoded = borsh::to_vec("foo").unwrap();

let got = ascii::AsciiString::deserialize_reader(&mut &encoded[..]).unwrap();
assert_eq!("foo", got.as_str());

let encoded = borsh::to_vec("żółw").unwrap();
ascii::AsciiString::deserialize_reader(&mut &encoded[..]).unwrap_err();
}

impl<T> BorshDeserialize for Vec<T>
where
T: BorshDeserialize,
Expand Down
25 changes: 25 additions & 0 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,19 @@ impl BorshSchema for String {
str::declaration()
}
}

#[cfg(feature = "ascii")]
impl BorshSchema for ascii::AsciiString {
#[inline]
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
str::add_definitions_recursively(definitions);
}
#[inline]
fn declaration() -> Declaration {
str::declaration()
}
}

impl BorshSchema for str {
#[inline]
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
Expand All @@ -401,6 +414,18 @@ impl BorshSchema for str {
}
}

#[cfg(feature = "ascii")]
impl BorshSchema for ascii::AsciiStr {
#[inline]
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
str::add_definitions_recursively(definitions);
}
#[inline]
fn declaration() -> Declaration {
str::declaration()
}
}

impl BorshSchema for core::ops::RangeFull {
#[inline]
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
Expand Down
26 changes: 26 additions & 0 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ impl BorshSerialize for String {
}
}

#[cfg(feature = "ascii")]
impl BorshSerialize for ascii::AsciiStr {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.as_bytes().serialize(writer)
}
}

#[cfg(feature = "ascii")]
impl BorshSerialize for ascii::AsciiString {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.as_bytes().serialize(writer)
}
}

#[cfg(feature = "ascii")]
#[test]
fn test_ascii() {
let val = ascii::AsciiStr::from_ascii("foo").unwrap();
let encoded = borsh::to_vec(val).unwrap();

let got: String = borsh::BorshDeserialize::deserialize_reader(&mut &encoded[..]).unwrap();
assert_eq!("foo", got);
}

/// Helper method that is used to serialize a slice of data (without the length marker).
#[inline]
fn serialize_slice<T: BorshSerialize, W: Write>(data: &[T], writer: &mut W) -> Result<()> {
Expand Down

0 comments on commit dd179a8

Please sign in to comment.