flanker_assert_str/lib.rs
1//! Assertions for strings.
2
3#![no_std]
4
5extern crate alloc;
6
7use alloc::string::ToString;
8use core::fmt::Debug;
9use core::str::FromStr;
10
11/// Asserts that a value that implements both [`FromStr`] and [`ToString`] may be marshalled
12/// to a string and back again to an equivalent (by a [`PartialEq`] comparison) value.
13///
14/// # Panics
15/// If the assertion fails.
16pub fn assert_loopback<T>(value: &T)
17 where
18 T::Err: Debug,
19 T: Debug + FromStr + ToString + PartialEq,
20{
21 let encoded = value.to_string();
22 let decoded = T::from_str(&encoded);
23 assert!(decoded.is_ok(), "error: '{:?}'", decoded.err().unwrap());
24 assert_eq!(value, &decoded.unwrap());
25}
26
27#[cfg(test)]
28mod tests;