use std::borrow::Cow;
use serde::{Deserialize, Serialize};
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
pub struct RelateTo<'a> {
relates: Cow<'a, str>,
}
impl<'a> RelateTo<'a> {
#[must_use]
pub const fn new(relates: Cow<'a, str>) -> Self {
Self { relates }
}
#[must_use]
pub fn to(&self) -> &str {
&self.relates
}
}
impl<'a> From<&'a str> for RelateTo<'a> {
fn from(input: &'a str) -> Self {
RelateTo {
relates: Cow::Borrowed(input),
}
}
}
impl From<String> for RelateTo<'_> {
fn from(input: String) -> Self {
RelateTo {
relates: Cow::Owned(input),
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::wildcard_imports)]
use crate::relates::RelateTo;
#[test]
fn test_convert_string_to_relate_to() {
let relate = RelateTo::from("[#12343567]");
assert_eq!(
relate.to(),
"[#12343567]",
"Expected the relates-to value to match the string used to construct it"
);
}
}