use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FactorId(String);
impl FactorId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
pub fn is_valid(&self) -> bool {
!self.0.is_empty()
&& self.0.len() <= 64
&& self.0.chars().all(|c| c.is_alphanumeric() || c == '_')
}
}
impl fmt::Display for FactorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for FactorId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl From<String> for FactorId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for FactorId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<FactorId> for String {
fn from(factor_id: FactorId) -> Self {
factor_id.0
}
}
impl PartialEq<str> for FactorId {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for FactorId {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<String> for FactorId {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}