~kmaasrud/pimalaya-webfinger

68042342cbc3e3dc487c291dcc0b62d5e743efd2 — Knut Magnus Aasrud 10 months ago f8e7047
docs: improve public API
1 files changed, 53 insertions(+), 8 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +53 -8
@@ 1,3 1,8 @@
//! # WebFinger
//!
//! This crate provides a Rust implementation of the [WebFinger](https://tools.ietf.org/html/rfc7033)
//! protocol.

mod rel;
mod uri;



@@ 7,26 12,66 @@ use std::collections::HashMap;
pub use rel::Rel;
pub use uri::Uri;

/// Represents a resource descriptor as described by the WebFinger protocol.
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct ResourceDescriptor {
    subject: String,
    /// The subject of the resource.
    pub subject: String,
    /// Aliases associated with the resource.
    ///
    /// Aliases provide alternative identifiers for the resource.
    #[serde(default)]
    aliases: Vec<String>,
    pub aliases: Vec<String>,
    /// Properties associated with the resource.
    #[serde(default)]
    properties: HashMap<Uri, Option<String>>,
    pub properties: HashMap<Uri, Option<String>>,
    /// Links associated with the resource.
    ///
    /// Links represent relationships to other resources.
    #[serde(default)]
    links: Vec<Link>,
    pub links: Vec<Link>,
}

impl ResourceDescriptor {
    /// Creates a new `ResourceDescriptor` with the given subject.
    pub fn new(subject: String) -> Self {
        Self {
            subject,
            aliases: vec![],
            properties: HashMap::new(),
            links: vec![],
        }
    }

    /// Adds an alias to the resource descriptor.
    pub fn alias(&mut self, alias: String) -> &mut Self {
        self.aliases.push(alias);
        self
    }

    /// Adds a property to the resource descriptor.
    pub fn property(&mut self, key: Uri, value: Option<String>) -> &mut Self {
        self.properties.insert(key, value);
        self
    }

    /// Adds a link to the resource descriptor.
    pub fn link(&mut self, link: Link) -> &mut Self {
        self.links.push(link);
        self
    }
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Link {
    mime_type: Option<String>,
    href: Option<Uri>,
    rel: RelOrUri,
    #[serde(rename = "type")]
    pub mime_type: Option<String>,
    pub href: Option<Uri>,
    #[serde(default)]
    titles: HashMap<String, String>,
    pub titles: HashMap<String, String>,
    #[serde(default)]
    properties: HashMap<Uri, Option<String>>,
    pub properties: HashMap<Uri, Option<String>>,
}

impl Link {