nostralink 0.1.9

Linked data library for nostr
Documentation
use crate::niri::random_urn_nn;
use serde::{Deserialize, Serialize};

/// JSON-LD attributes
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JLDAttr {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@context")]
    pub context: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@vocab")]
    pub vocab: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@id")]
    pub id: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@type")]
    // pub typ: Option<&str>,
    pub typ: Option<String>,
}

impl JLDAttr {
    pub fn for_schema_type(otype: &str) -> Self {
        Self {
            context: Some("https://schema.org".to_string()),
            typ: Some(otype.to_string()),
            id: None,
            vocab: None,
        }
    }
    pub fn for_localschema_type(otype: &str) -> Self {
        Self {
            context: Some(format!("http://local.schema.org/{otype}")),
            typ: Some(format!("schema:{otype}")),
            id: None,
            vocab: None,
            // vocab: Some(format!("http://schema.org/")),
        }
    }

    pub fn for_nostra_type(otype: &str) -> Self {
        Self {
            context: Some(format!("http://nostralink.org/{otype}")),
            id: Some(random_urn_nn().into_string()),
            typ: Some(format!("nostralink:{otype}")),
            vocab: None,
        }
    }

    pub fn id<S: Into<String>>(mut self, value: S) -> Self {
        self.id = Some(value.into());
        self
    }
}