use std::fmt;
use crate::PluginError;
use crate::manager::validate_plugin_name;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
#[serde(transparent)]
pub struct PluginName(String);
impl<'de> serde::Deserialize<'de> for PluginName {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
PluginName::try_from(s).map_err(serde::de::Error::custom)
}
}
impl PluginName {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for PluginName {
type Error = PluginError;
fn try_from(value: String) -> Result<Self, Self::Error> {
validate_plugin_name(&value)?;
Ok(Self(value))
}
}
impl TryFrom<&str> for PluginName {
type Error = PluginError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
validate_plugin_name(value)?;
Ok(Self(value.to_owned()))
}
}
impl fmt::Display for PluginName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for PluginName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl PartialEq<str> for PluginName {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for PluginName {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<String> for PluginName {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}