mod generated;
pub use generated::{LinkRelation, LINK_RELATIONS};
#[cfg(feature = "serde")]
mod serde;
use std::error::Error;
use std::fmt;
use std::str::FromStr;
#[derive(Clone, Debug)]
pub struct LinkRelationDetails {
pub name: String,
pub description: String,
pub reference: String,
pub notes: Option<String>,
}
#[derive(Copy, Clone, Debug)]
pub enum LinkRelationParsingError {
NotFound,
}
impl fmt::Display for LinkRelationParsingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Error while parsing LinkRelation: {}",
match self {
LinkRelationParsingError::NotFound => "Not found",
}
)
}
}
impl Error for LinkRelationParsingError {}
impl LinkRelation {
pub fn get_link_relation(&self) -> &LinkRelationDetails {
LINK_RELATIONS.get(self).unwrap()
}
pub fn get_name(&self) -> &str {
&self.get_link_relation().name
}
pub fn get_description(&self) -> &str {
&self.get_link_relation().description
}
pub fn get_reference(&self) -> &str {
&self.get_link_relation().reference
}
pub fn get_notes(&self) -> Option<&str> {
let link_relation = self.get_link_relation();
link_relation.notes.as_deref()
}
}
impl FromStr for LinkRelation {
type Err = LinkRelationParsingError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let value_str = value.trim().to_lowercase();
LINK_RELATIONS
.iter()
.filter_map(|(k, v)| if value_str == v.name { Some(*k) } else { None })
.next()
.ok_or(LinkRelationParsingError::NotFound)
}
}
impl fmt::Display for LinkRelation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get_name())
}
}
#[cfg(test)]
mod tests {
use super::LinkRelation;
#[test]
fn test_to_string() {
assert_eq!(LinkRelation::ACL.to_string(), String::from("acl"));
}
}