1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//! Assertions for strings.

#![no_std]

extern crate alloc;

use alloc::string::ToString;
use core::fmt::Debug;
use core::str::FromStr;

/// Asserts that a value that implements both [`FromStr`] and [`ToString`] may be marshalled
/// to a string and back again to an equivalent (by a [`PartialEq`] comparison) value.
///
/// # Panics
/// If the assertion fails.
pub fn assert_loopback<T>(value: &T)
    where
        T::Err: Debug,
        T: Debug + FromStr + ToString + PartialEq,
{
    let encoded = value.to_string();
    let decoded = T::from_str(&encoded);
    assert!(decoded.is_ok(), "error: '{:?}'", decoded.err().unwrap());
    assert_eq!(value, &decoded.unwrap());
}

#[cfg(test)]
mod tests;