Skip to content

Commit

Permalink
refactor: simplify and move to core v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstonskj committed Oct 8, 2024
1 parent 1fcbaa5 commit 36a51b5
Show file tree
Hide file tree
Showing 41 changed files with 1,230 additions and 1,563 deletions.
8 changes: 4 additions & 4 deletions rdftk_io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rdftk_io"
version = "0.3.0"
version = "0.3.1"
authors = ["Simon Johnston <[email protected]>"]
edition = "2021"
description = "This crate provides traits for reading and writing Statements and Graphs as well as implementations of these for common representations."
Expand Down Expand Up @@ -29,15 +29,15 @@ xml = ["xml-rs", "rdftk_names"]
itertools = "0.13"
lazy_static = "1.4"
objio = "0.1.1"
rdftk_core = { version = "0.4.2", path = "../rdftk_core" }
rdftk_iri = { version = "0.2.2", path = "../rdftk_iri" }
rdftk_core = { version = "0.5.0", path = "../rdftk_core" }
rdftk_iri = { version = "0.2.5", path = "../rdftk_iri" }
regex = "1.5"
tracing = "0.1.40"

# feature-dependencies
pest = { version = "2.7", optional = true }
pest_derive = { version = "2.7", optional = true }
rdftk_names = { version = "0.2.1", path = "../rdftk_names", optional = true }
rdftk_names = { version = "0.2.3", path = "../rdftk_names", optional = true }
serde_json = { version = "1.0", optional = true }
xml-rs = { version = "0.8", optional = true }

Expand Down
5 changes: 5 additions & 0 deletions rdftk_io/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ let graph = reader.read(&mut file, graph_factory()).unwrap();

## Changes

### Version 0.3.1

* Feature: moved to new v0.5 core package.
* Tests: all tests now passing.

### Version 0.3.0

* Feature: moved to new `rdftk_core` package.
Expand Down
73 changes: 46 additions & 27 deletions rdftk_io/src/common/indenter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::fmt::{Display, Formatter};

// ------------------------------------------------------------------------------------------------
Expand All @@ -7,7 +8,7 @@ use std::fmt::{Display, Formatter};
#[derive(Clone, Debug)]
pub(crate) struct Indenter {
width: usize,
depth: usize,
depth: RefCell<usize>,
}

// ------------------------------------------------------------------------------------------------
Expand All @@ -16,56 +17,74 @@ pub(crate) struct Indenter {

impl Default for Indenter {
fn default() -> Self {
Self { width: 2, depth: 0 }
Self {
width: 2,
depth: RefCell::new(0),
}
}
}

impl Display for Indenter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:width$}", "", width = self.width * self.depth)
write!(
f,
"{:width$}",
"",
width = self.width * (*self.depth.borrow())
)
}
}

impl Indenter {
pub(crate) fn with_width(self, width: usize) -> Self {
Self { width, ..self }
pub(crate) fn with_default_indent_width(self, width: usize) -> Self {
let mut self_mut = self;
self_mut.width = width;
self_mut
}

pub(crate) fn with_depth(self, depth: usize) -> Self {
Self { depth, ..self }
#[allow(dead_code)]
pub(crate) fn with_initial_depth(self, depth: usize) -> Self {
let _ = self.depth.replace(depth);
self
}

pub(crate) fn depth(&self) -> usize {
self.depth
*self.depth.borrow()
}

pub(crate) fn reset_depth(&mut self) {
self.depth = 0
#[allow(dead_code)]
pub(crate) fn default_width(&self) -> usize {
self.width
}

pub(crate) fn indent(&self) -> Self {
self.indent_by(1)
pub(crate) fn reset_depth(&self) {
self.depth.replace(0);
}

pub(crate) fn indent_by(&self, by: usize) -> Self {
Self {
width: self.width,
depth: self.depth + by,
}
pub(crate) fn indent(&self) {
self.indent_by(self.width);
}

pub(crate) fn outdent(&self) -> Self {
self.outdent_by(1)
pub(crate) fn indent_by(&self, by: usize) {
self.depth.replace(self.depth() + by);
}

pub(crate) fn outdent_by(&self, by: usize) -> Self {
Self {
width: self.width,
depth: self.depth - by,
}
#[allow(dead_code)]
pub(crate) fn indent_for<T: Into<usize>>(&self, for_: T) {
self.indent_by(for_.into())
}

//pub(crate) fn one(&self) -> String {
// format!("{:width$}", "", width = self.width)
//}
pub(crate) fn outdent(&self) {
self.indent_by(self.width);
}

#[allow(dead_code)]
pub(crate) fn outdent_by(&self, by: usize) {
self.depth.replace(self.depth() - by);
}

#[allow(dead_code)]
pub(crate) fn outdent_for<T: Into<usize>>(&self, for_: T) {
self.outdent_by(for_.into())
}
}
21 changes: 9 additions & 12 deletions rdftk_io/src/common/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use pest::Parser as _;
use pest_derive::Parser;
use rdftk_core::error::Error;
use rdftk_core::model::data_set::{DataSetFactoryRef, DataSetRef};
use rdftk_core::model::graph::{GraphFactoryRef, GraphRef};
use rdftk_core::model::data_set::DataSet;
use rdftk_core::model::graph::Graph;
use tracing::{span, Level};

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -66,7 +66,7 @@ macro_rules! pest_error {
// Public Functions
// ------------------------------------------------------------------------------------------------

pub(crate) fn parse_ntriple_doc<S>(source: S, factory: GraphFactoryRef) -> Result<GraphRef, Error>
pub(crate) fn parse_ntriple_doc<S>(source: S) -> Result<Graph, Error>
where
S: AsRef<str>,
{
Expand All @@ -78,10 +78,10 @@ where
source: Box::new(e),
})?;
let top_node = parsed.next().unwrap();
ntriples::parse_doc(top_node, factory)
ntriples::parse_doc(top_node)
}

pub(crate) fn parse_nquad_doc<S>(source: S, factory: DataSetFactoryRef) -> Result<DataSetRef, Error>
pub(crate) fn parse_nquad_doc<S>(source: S) -> Result<DataSet, Error>
where
S: AsRef<str>,
{
Expand All @@ -93,10 +93,10 @@ where
source: Box::new(e),
})?;
let top_node = parsed.next().unwrap();
nquads::parse_doc(top_node, factory)
nquads::parse_doc(top_node)
}

pub(crate) fn parse_turtle_doc<S>(_source: S, _factory: GraphFactoryRef) -> Result<GraphRef, Error>
pub(crate) fn parse_turtle_doc<S>(_source: S) -> Result<Graph, Error>
where
S: AsRef<str>,
{
Expand All @@ -105,10 +105,7 @@ where
todo!()
}

pub(crate) fn parse_trig_doc<S>(
_source: S,
_factory: DataSetFactoryRef,
) -> Result<DataSetRef, Error>
pub(crate) fn parse_trig_doc<S>(_source: S) -> Result<DataSet, Error>
where
S: AsRef<str>,
{
Expand All @@ -117,7 +114,7 @@ where
todo!()
}

pub(crate) fn parse_n3_doc<S>(_source: S, _factory: GraphFactoryRef) -> Result<GraphRef, Error>
pub(crate) fn parse_n3_doc<S>(_source: S) -> Result<Graph, Error>
where
S: AsRef<str>,
{
Expand Down
54 changes: 19 additions & 35 deletions rdftk_io/src/common/parser/nquads.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
use super::ntriples::{object as nt_object, predicate as nt_predicate, subject as nt_subject};
use super::Rule;
use pest::iterators::Pair;
use rdftk_core::error::Error;
use rdftk_core::model::data_set::DataSet;
use rdftk_core::model::graph::named::{GraphName, GraphNameRef};
use rdftk_core::model::graph::NamedGraphRef;
use rdftk_core::model::statement::{ObjectNodeRef, StatementRef, SubjectNodeRef};
use rdftk_core::{
error::Error,
model::data_set::{DataSetFactoryRef, DataSetRef},
};
use rdftk_iri::IriRef;
use std::cell::RefMut;
use rdftk_core::model::graph::{Graph, GraphName};
use rdftk_core::model::statement::{ObjectNode, Statement, SubjectNode};
use rdftk_iri::Iri;

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

pub(super) fn parse_doc(
input_pair: Pair<'_, Rule>,
factory: DataSetFactoryRef,
) -> Result<DataSetRef, Error> {
pub(super) fn parse_doc(input_pair: Pair<'_, Rule>) -> Result<DataSet, Error> {
parse_rule!("nquadDoc" entry input_pair);

let data_set = factory.data_set();
let mut data_set = DataSet::default();

if input_pair.as_rule() == Rule::nquadDoc {
for inner_pair in input_pair.into_inner() {
match inner_pair.as_rule() {
Rule::nquad => {
nquad(inner_pair, data_set.borrow_mut())?;
nquad(inner_pair, &mut data_set)?;
}
Rule::EOI => {}
_ => {
Expand All @@ -51,42 +43,34 @@ pub(super) fn parse_doc(
// Private Functions
// ------------------------------------------------------------------------------------------------

fn subject_to_name(subject: SubjectNodeRef) -> GraphNameRef {
let name: GraphName = subject.into();
name.into()
fn subject_to_name(subject: SubjectNode) -> GraphName {
subject.into()
}

fn nquad(input_pair: Pair<'_, Rule>, data_set: RefMut<'_, dyn DataSet>) -> Result<(), Error> {
fn nquad(input_pair: Pair<'_, Rule>, data_set: &mut DataSet) -> Result<(), Error> {
parse_rule!("nquad" entry input_pair);

let mut data_set = data_set;

let graphs = data_set.graph_factory();
let statements = graphs.statement_factory();
let literals = statements.literal_factory();

if input_pair.as_rule() == Rule::nquad {
let mut inner_pairs = input_pair.into_inner();
let subject: SubjectNodeRef = nt_subject(inner_pairs.next().unwrap(), &statements)?;
let predicate: IriRef = nt_predicate(inner_pairs.next().unwrap())?;
let object: ObjectNodeRef = nt_object(inner_pairs.next().unwrap(), &statements, &literals)?;
let statement: StatementRef = statements.statement(subject, predicate, object)?;
let graph: &mut NamedGraphRef = if let Some(new_inner_pair) = inner_pairs.next() {
let graph_name = subject_to_name(nt_subject(new_inner_pair, &statements)?);
let subject: SubjectNode = nt_subject(inner_pairs.next().unwrap())?;
let predicate: Iri = nt_predicate(inner_pairs.next().unwrap())?;
let object: ObjectNode = nt_object(inner_pairs.next().unwrap())?;
let statement: Statement = Statement::new(subject, predicate, object);
let graph: &mut Graph = if let Some(new_inner_pair) = inner_pairs.next() {
let graph_name = subject_to_name(nt_subject(new_inner_pair)?);
if let Some(graph) = data_set.graph_mut(&Some(graph_name.clone())) {
graph
} else {
data_set.insert(graphs.named_graph(Some(graph_name.clone())));
data_set.insert(Graph::named(graph_name.clone()));
data_set.graph_mut(&Some(graph_name)).unwrap()
}
} else if let Some(graph) = data_set.graph_mut(&None) {
graph
} else {
data_set.insert(graphs.named_graph(None));
data_set.insert(Graph::default());
data_set.graph_mut(&None).unwrap()
};
let mut graph_mut = graph.borrow_mut();
graph_mut.insert(statement);
graph.insert(statement);
Ok(())
} else {
Err(pest_error!(unexpected RULE_FN, &input_pair, [Rule::nquad]))
Expand Down
Loading

0 comments on commit 36a51b5

Please sign in to comment.