use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[serde(transparent)]
pub struct Str(String);
impl Str {
pub const fn new(value: String) -> Self {
Self(value)
}
pub fn into_inner(self) -> String {
self.0
}
}
impl From<String> for Str {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for Str {
fn from(value: &str) -> Self {
Self::new(value.to_owned())
}
}
impl From<Str> for String {
fn from(value: Str) -> Self {
value.into_inner()
}
}
impl std::ops::Deref for Str {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl std::ops::DerefMut for Str {
fn deref_mut(&mut self) -> &mut String {
&mut self.0
}
}
#[cfg(test)]
mod tests {
use crate::{decode, encode};
use super::*;
#[test]
fn wire_shape_is_identical_to_a_bare_string() {
let value = Str::from("hello");
let raw = String::from("hello");
assert_eq!(encode(&value).unwrap(), encode(&raw).unwrap());
assert_eq!(decode::<String>(&encode(&value).unwrap()).unwrap(), raw);
assert_eq!(decode::<Str>(&encode(&raw).unwrap()).unwrap(), value);
}
}