~kmaasrud/pimalaya-webfinger

743ff522b76e5a87b6d32866da74b04f5b35103b — Knut Magnus Aasrud 8 months ago 67da110
feat: create new Link from parsing string slice
1 files changed, 17 insertions(+), 4 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +17 -4
@@ 8,6 8,7 @@ mod uri;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uri::UriValidationError;

pub use rel::Rel;
pub use uri::Uri;


@@ 75,14 76,26 @@ pub struct Link {
}

impl Link {
    pub fn new(uri: Uri) -> Self {
        Self {
            rel: RelOrUri::Uri(uri),
    /// Creates a new `Link` with the given relation type.
    ///
    /// The relation type can be either a registered relation type or a URI.
    ///
    /// # Errors
    ///
    /// Returns an error if the relation type is neither a registered relation type
    /// nor a parseable URI.
    pub fn new(rel: &str) -> Result<Self, UriValidationError> {
        let rel = Rel::try_from(rel)
            .map(|rel| RelOrUri::Rel(rel))
            .or_else(|_| Uri::try_from(rel).map(|uri| RelOrUri::Uri(uri)))?;

        Ok(Self {
            rel,
            mime_type: None,
            href: None,
            titles: HashMap::new(),
            properties: HashMap::new(),
        }
        })
    }

    /// Insert a property to the link.