rudof_lib 0.2.14

RDF data shapes implementation in Rust
use shacl::rdf::ShaclWriter;

use crate::{Result, Rudof, errors::ShaclError, formats::ShaclFormat};
use rudof_rdf::rdf_impl::InMemoryGraph;
use shacl::error::IRError;
use std::io;

pub fn serialize_shacl_schema<W: io::Write>(
    rudof: &Rudof,
    shacl_format: Option<&ShaclFormat>,
    writer: &mut W,
) -> Result<()> {
    let shacl_format = shacl_format.copied().unwrap_or_default();

    let shacl_shapes = rudof.shacl_shapes.as_ref().ok_or(ShaclError::NoShaclShapesLoaded)?;

    match shacl_format {
        ShaclFormat::Internal => {
            write!(writer, "{shacl_shapes}").map_err(|e| ShaclError::FailedIoOperation { error: e.to_string() })?;
        },
        _ => {
            let rdf_format = shacl_format.try_into()?;
            let mut shacl_writer: ShaclWriter<InMemoryGraph> = ShaclWriter::new();

            shacl_writer
                .register(
                    &shacl_shapes
                        .try_into()
                        .map_err(|e: IRError| ShaclError::FailedCompilingShaclSchema { error: e.to_string() })?,
                )
                .map_err(|e| ShaclError::FailedIoOperation { error: e.to_string() })?;

            shacl_writer
                .serialize(&rdf_format, writer)
                .map_err(|e| ShaclError::FailedIoOperation { error: e.to_string() })?;
        },
    }

    Ok(())
}