use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;
use sbol_rdf::{Graph, RdfFormat, Resource};
use crate::error::WriteError;
use crate::object::Object;
pub trait ObjectStore {
fn objects(&self) -> &BTreeMap<Resource, Object>;
fn get(&self, identity: &Resource) -> Option<&Object>;
}
#[derive(Clone, Debug)]
pub struct RawDocument {
graph: Graph,
objects: BTreeMap<Resource, Object>,
}
impl RawDocument {
pub fn from_parts(graph: Graph, objects: BTreeMap<Resource, Object>) -> Self {
Self { graph, objects }
}
pub fn rdf_graph(&self) -> &Graph {
&self.graph
}
pub fn objects(&self) -> &BTreeMap<Resource, Object> {
&self.objects
}
pub fn get(&self, identity: &Resource) -> Option<&Object> {
self.objects.get(identity)
}
pub fn write(&self, format: RdfFormat) -> Result<String, WriteError> {
self.graph.write(format).map_err(WriteError::Rdf)
}
pub fn write_path(&self, path: impl AsRef<Path>, format: RdfFormat) -> Result<(), WriteError> {
let path = path.as_ref();
let serialized = self.write(format)?;
std::fs::write(path, serialized).map_err(|source| WriteError::Io {
path: path.to_path_buf(),
source,
})
}
pub fn write_turtle(&self) -> Result<String, WriteError> {
self.write(RdfFormat::Turtle)
}
}
impl ObjectStore for RawDocument {
fn objects(&self) -> &BTreeMap<Resource, Object> {
&self.objects
}
fn get(&self, identity: &Resource) -> Option<&Object> {
self.objects.get(identity)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct DocumentSet<'a, S: ObjectStore> {
documents: Vec<&'a S>,
objects: BTreeMap<Resource, &'a Object>,
}
impl<'a, S: ObjectStore> Default for DocumentSet<'a, S> {
fn default() -> Self {
Self {
documents: Vec::new(),
objects: BTreeMap::new(),
}
}
}
impl<'a, S: ObjectStore> DocumentSet<'a, S> {
pub fn new() -> Self {
Self::default()
}
pub fn from_documents(
documents: impl IntoIterator<Item = &'a S>,
) -> Result<Self, DocumentSetError> {
let mut set = Self::new();
for document in documents {
set.add_document(document)?;
}
Ok(set)
}
pub fn add_document(&mut self, document: &'a S) -> Result<(), DocumentSetError> {
for identity in document.objects().keys() {
if self.objects.contains_key(identity) {
return Err(DocumentSetError::duplicate(identity.clone()));
}
}
for (identity, object) in document.objects() {
self.objects.insert(identity.clone(), object);
}
self.documents.push(document);
Ok(())
}
pub fn documents(&self) -> &[&'a S] {
&self.documents
}
pub fn get(&self, identity: &Resource) -> Option<&'a Object> {
self.objects.get(identity).copied()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DocumentSetError {
identity: Resource,
}
impl DocumentSetError {
fn duplicate(identity: Resource) -> Self {
Self { identity }
}
pub fn identity(&self) -> &Resource {
&self.identity
}
}
impl fmt::Display for DocumentSetError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"duplicate SBOL object identity `{}` in document set",
self.identity
)
}
}
impl std::error::Error for DocumentSetError {}