mod properties;
pub use properties::*;
mod type_dir;
pub use type_dir::*;
#[cfg(feature = "google-type-dir")]
mod type_gdir;
#[cfg(feature = "google-type-dir")]
pub use type_gdir::*;
use crate::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct JsonLD {
#[serde(rename = "@context")]
pub context : String,
#[serde(rename = "@graph")]
pub graph : Vec<GraphEntity>,
#[serde(rename = "@id")]
pub id : String,
#[serde(rename = "@type")]
pub typedir : TypeDirectory,
}
impl Default for JsonLD {
fn default() -> Self {
Self {
context: "https://schema.org".to_string(),
graph: vec![],
id: "http://example.com/#default".to_string(),
typedir: TypeDirectory::default(),
}
}
}
impl JsonLD {
pub fn new_graph(graph: Vec<GraphEntity>, id : &str, typedir : TypeDirectory) -> Self {
Self {
graph,
typedir,
id: id.to_string(),
..Default::default()
}
}
pub fn set_grapth(&mut self, graph: Vec<GraphEntity>) {
self.graph = graph;
}
pub fn push_grapth(&mut self, graph: GraphEntity) {
self.graph.push(graph);
}
pub fn to_string(&self, script_tag: bool) -> Result<String, Error> {
Ok(match script_tag {
false => serde_json::to_string(self)?,
true => format!(r#"<script type="application/ld+json">{}</script>"#, serde_json::to_string(self)?),
})
}
pub fn to_string_pretty(&self, script_tag: bool) -> Result<String, Error> {
Ok(match script_tag {
false => serde_json::to_string_pretty(self)?,
true => format!(r#"<script type="application/ld+json">{}</script>"#, serde_json::to_string_pretty(self)?),
})
}
}