use super::entity::{EntityType, RoCrateEntity};
use serde::{Deserialize, Serialize};
use serde_json::json;
pub const RO_CRATE_CONTEXT: &str = "https://w3id.org/ro/crate/1.1/context";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoCrateDescriptor {
#[serde(rename = "@context")]
pub context: String,
#[serde(rename = "@graph")]
pub graph: Vec<RoCrateEntity>,
}
impl Default for RoCrateDescriptor {
fn default() -> Self {
Self::new()
}
}
impl RoCrateDescriptor {
pub fn new() -> Self {
let metadata_entity =
RoCrateEntity::new("ro-crate-metadata.json", EntityType::CreativeWork)
.with_property("conformsTo", json!({ "@id": "https://w3id.org/ro/crate/1.1" }))
.with_reference("about", "./");
Self { context: RO_CRATE_CONTEXT.to_string(), graph: vec![metadata_entity] }
}
pub fn add_entity(&mut self, entity: RoCrateEntity) {
self.graph.push(entity);
}
pub fn root_dataset(&self) -> Option<&RoCrateEntity> {
self.graph.iter().find(|e| e.id == "./")
}
pub fn root_dataset_mut(&mut self) -> Option<&mut RoCrateEntity> {
self.graph.iter_mut().find(|e| e.id == "./")
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).unwrap_or_else(|_err| "{}".to_string())
}
}