dialtone_common 0.1.0

Dialtone Common Code
Documentation
use serde::{Deserialize, Serialize};

use super::SELF;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Jrd {
    pub subject: String,
    pub links: Vec<JrdLink>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JrdLink {
    #[serde(default = "default_rel")]
    pub rel: String,

    pub href: String,

    #[serde(rename = "type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media_type: Option<String>,
}

pub fn create_acct(host_name: &str, preferred_user_name: &str) -> String {
    format!("acct:{}@{}", preferred_user_name, host_name)
}

fn default_rel() -> String {
    SELF.to_string()
}

#[cfg(test)]
mod jrd_tests {
    use super::Jrd;
    use super::SELF;
    use crate::webfinger::create_acct;

    #[test]
    fn deserialize_without_rel() {
        let j = r#"
            {
              "subject" : "acct:bob@example.com",
              "links" :
              [
                {
                  "rel" : "http://webfinger.example/rel/profile-page",
                  "href" : "https://www.example.com/~bob/",
                  "type": "text/html"
                },
                {
                  "href" : "https://www.example.com/~bob/bob.vcf",
                  "type": "application/vcard"
                }
              ]
            }
        "#;
        let d: Jrd = serde_json::from_str(&j).unwrap();
        assert_eq!(2, d.links.len());
        assert_eq!(
            super::super::PROFILE_PAGE_URL_ID.to_string(),
            d.links[0].rel
        );
        assert_eq!(SELF.to_string(), d.links[1].rel);
    }

    #[test]
    fn create_acct_test() {
        let acct = create_acct("example.com", "test");
        assert_eq!("acct:test@example.com", acct)
    }
}