diff --git a/Cargo.toml b/Cargo.toml index a2cefbd..6062782 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,11 @@ Simple and fast implementation of IRI validation and relative IRI resolution edition = "2021" rust-version = "1.60" +[features] +borsh = ["dep:borsh"] + [dependencies] +borsh = { version = "1.5.7", optional = true, features = ["derive"] } serde = { version = "1.0.166", optional = true } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index a82ceab..0f50218 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![deny(unsafe_code)] +#[cfg(feature = "borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::borrow::{Borrow, Cow}; @@ -37,6 +39,7 @@ use std::str::{Chars, FromStr}; /// # Result::<(), oxiri::IriParseError>::Ok(()) /// ``` #[derive(Clone, Copy)] +#[cfg_attr(feature = "borsh", derive(BorshDeserialize, BorshSerialize))] pub struct IriRef { iri: T, positions: IriElementsPositions, @@ -538,6 +541,7 @@ impl<'de, T: Deref + Deserialize<'de>> Deserialize<'de> for IriRef /// # Result::<(), oxiri::IriParseError>::Ok(()) /// ``` #[derive(Clone, Copy)] +#[cfg_attr(feature = "borsh", derive(BorshDeserialize, BorshSerialize))] pub struct Iri(IriRef); impl> Iri { @@ -1904,3 +1908,51 @@ fn is_unreserved_or_sub_delims(c: char) -> bool { | '~' ) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_iri_parse() { + let iri = Iri::parse("https://example.com/path?query=value#fragment"); + assert_eq!( + iri.unwrap().as_str(), + "https://example.com/path?query=value#fragment" + ); + } +} + +// Borsh tests +#[cfg(feature = "borsh")] +mod borsh_tests { + use super::*; + + #[test] + fn test_iri_borsh_serialize() { + let iri = Iri::parse("https://example.com/path?query=value#fragment"); + let serialized = iri.unwrap().serialize(); + let deserialized = Iri::deserialize(&serialized); + assert_eq!( + deserialized.unwrap().as_str(), + "https://example.com/path?query=value#fragment" + ); + } +} + +// Serde tests +#[cfg(feature = "serde")] +mod serde_tests { + use super::*; + + #[test] + fn test_iri_serde_serialize() { + let iri = Iri::parse("https://example.com/path?query=value#fragment"); + let serialized = serde_json::to_string(&iri.unwrap()).unwrap(); + let deserialized = serde_json::from_str(&serialized); + assert_eq!( + deserialized.unwrap().as_str(), + "https://example.com/path?query=value#fragment" + ); + } +}