use std::fmt::{Debug, Display, Formatter};
use serde::{Deserialize, Serialize};
use crate::{
FMRI,
helpers::{check_character_collision, remove_first_and_last_characters},
};
#[derive(PartialEq, Serialize, Deserialize, Clone, Ord, Eq, PartialOrd, Hash)]
pub struct Publisher(String);
impl Publisher {
pub fn new(mut publisher: String) -> Result<Self, String> {
check_character_collision(&publisher)?;
publisher = remove_first_and_last_characters(&publisher, '/').to_owned();
Ok(Self(publisher))
}
pub fn parse_publisher_from_raw_fmri(raw_fmri: String) -> Result<Option<Self>, String> {
let raw_fmri = raw_fmri.trim_start_matches("fmri=").to_owned();
return match raw_fmri.find("pkg://") {
None => Ok(None),
Some(position) => {
if position != 0 {
panic!(
"wrong position of starting \"pkg://\" pattern ({})",
position
)
}
let (publisher, _) = raw_fmri
.trim_start_matches("pkg://")
.split_once('/')
.expect("Fmri must contain \"/package_name\"");
Ok(Some(Self::new(publisher.to_owned())?))
}
};
}
pub fn get_as_string(self) -> String {
self.0
}
pub fn get_as_ref_string(&self) -> &String {
&self.0
}
pub fn get_as_ref_mut_string(&mut self) -> &mut String {
&mut self.0
}
}
impl Display for Publisher {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "pkg://{}/", self.get_as_ref_string())
}
}
impl Debug for Publisher {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}