use std::fmt;
use std::ops::Deref;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
#[serde(transparent)]
pub struct MediaRef(String);
impl MediaRef {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
impl Deref for MediaRef {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<str> for MediaRef {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for MediaRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<String> for MediaRef {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for MediaRef {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl PartialEq<str> for MediaRef {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for MediaRef {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serializes_as_string() {
let media = MediaRef::new("warning");
let json = serde_json::to_string(&media).expect("serialize");
assert_eq!(json, "\"warning\"");
}
}