Skip to content

Commit

Permalink
feat: better Literal handling in Turtle
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstonskj committed Oct 16, 2024
1 parent 6af01a7 commit cb166e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 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.2"
version = "0.3.3"
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,7 +29,7 @@ xml = ["xml-rs", "rdftk_names"]
itertools = "0.13"
lazy_static = "1.4"
objio = "0.1.1"
rdftk_core = { version = "0.5.0", path = "../rdftk_core" }
rdftk_core = { version = "0.5.4", path = "../rdftk_core" }
rdftk_iri = { version = "0.2.5", path = "../rdftk_iri" }
regex = "1.5"
tracing = "0.1.40"
Expand Down
6 changes: 6 additions & 0 deletions rdftk_io/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ let graph = reader.read(&mut file, graph_factory()).unwrap();

## Changes

### Version 0.3.3

* Feature: better `Literal` handling in Turtle;
* write only the lexical form for numeric and boolean literals,
* write prefixed-names instead of full IRI where possible.

### Version 0.3.2

* Feature: added `GraphWriter` and `DataSetWriter` traits so that clients need not
Expand Down
46 changes: 40 additions & 6 deletions rdftk_io/src/turtle/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use itertools::Itertools;
use objio::{impl_has_options, HasOptions, ObjectWriter};
use rdftk_core::error::{Error, Result};
use rdftk_core::model::graph::Graph;
use rdftk_core::model::literal::DataType;
use rdftk_core::model::statement::{ObjectNode, SubjectNode};
use rdftk_iri::Iri;
use std::cell::RefCell;
Expand Down Expand Up @@ -463,7 +464,7 @@ impl TurtleWriter {
} else if object.is_resource() {
self.write_iri(w, graph, object.as_resource().unwrap())?;
} else {
self.write_literal(w, object)?;
self.write_literal(w, graph, object)?;
}
Ok(())
}
Expand Down Expand Up @@ -560,13 +561,46 @@ impl TurtleWriter {
Ok(())
}

fn write_literal<W: Write>(&self, w: &mut W, literal: &ObjectNode) -> Result<()> {
// TODO: compress data type Iris
fn write_literal<W: Write>(
&self,
w: &mut W,
graph: &Graph,
literal: &ObjectNode,
) -> Result<()> {
Ok(if let Some(literal) = literal.as_literal() {
write!(w, "{}", literal)
match literal.data_type() {
Some(DataType::Iri) => {
let iri = Iri::parse(literal.lexical_form())?;
self.write_iri(w, graph, &iri)?
}
Some(DataType::Boolean)
| Some(DataType::Long)
| Some(DataType::Int)
| Some(DataType::Short)
| Some(DataType::Byte)
| Some(DataType::UnsignedLong)
| Some(DataType::UnsignedInt)
| Some(DataType::UnsignedShort)
| Some(DataType::UnsignedByte)
| Some(DataType::Float)
| Some(DataType::Double)
| Some(DataType::Decimal) => write!(w, "{}", literal.lexical_form())?,
_ => {
write!(w, "{:?}", literal.lexical_form())?;
match (literal.data_type(), literal.language()) {
(Some(data_type), None) => {
write!(w, "^^")?;
let iri = data_type.as_iri();
self.write_iri(w, graph, iri)?;
}
(None, Some(language)) => write!(w, "@{}", language)?,
_ => (),
}
}
}
} else {
write!(w, "ERROR: this is not a literal: {:?}", literal)
}?)
panic!("ERROR: this is not a literal: {:?}", literal)
})

Check failure on line 603 in rdftk_io/src/turtle/writer.rs

View workflow job for this annotation

GitHub Actions / clippy

passing a unit value to a function

error: passing a unit value to a function --> rdftk_io/src/turtle/writer.rs:570:9 | 570 | / Ok(if let Some(literal) = literal.as_literal() { 571 | | match literal.data_type() { 572 | | Some(DataType::Iri) => { 573 | | let iri = Iri::parse(literal.lexical_form())?; ... | 602 | | panic!("ERROR: this is not a literal: {:?}", literal) 603 | | }) | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg = note: `-D clippy::unit-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::unit_arg)]` help: move the expression in front of the call and replace it with the unit literal `()` | 570 ~ if let Some(literal) = literal.as_literal() { 571 + match literal.data_type() { 572 + Some(DataType::Iri) => { 573 + let iri = Iri::parse(literal.lexical_form())?; 574 + self.write_iri(w, graph, &iri)? 575 + } 576 + Some(DataType::Boolean) 577 + | Some(DataType::Long) 578 + | Some(DataType::Int) 579 + | Some(DataType::Short) 580 + | Some(DataType::Byte) 581 + | Some(DataType::UnsignedLong) 582 + | Some(DataType::UnsignedInt) 583 + | Some(DataType::UnsignedShort) 584 + | Some(DataType::UnsignedByte) 585 + | Some(DataType::Float) 586 + | Some(DataType::Double) 587 + | Some(DataType::Decimal) => write!(w, "{}", literal.lexical_form())?, 588 + _ => { 589 + write!(w, "{:?}", literal.lexical_form())?; 590 + match (literal.data_type(), literal.language()) { 591 + (Some(data_type), None) => { 592 + write!(w, "^^")?; 593 + let iri = data_type.as_iri(); 594 + self.write_iri(w, graph, iri)?; 595 + } 596 + (None, Some(language)) => write!(w, "@{}", language)?, 597 + _ => (), 598 + } 599 + } 600 + } 601 + } else { 602 + panic!("ERROR: this is not a literal: {:?}", literal) 603 + }; 604 + Ok(()) |

Check failure on line 603 in rdftk_io/src/turtle/writer.rs

View workflow job for this annotation

GitHub Actions / clippy

passing a unit value to a function

error: passing a unit value to a function --> rdftk_io/src/turtle/writer.rs:570:9 | 570 | / Ok(if let Some(literal) = literal.as_literal() { 571 | | match literal.data_type() { 572 | | Some(DataType::Iri) => { 573 | | let iri = Iri::parse(literal.lexical_form())?; ... | 602 | | panic!("ERROR: this is not a literal: {:?}", literal) 603 | | }) | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg = note: `-D clippy::unit-arg` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::unit_arg)]` help: move the expression in front of the call and replace it with the unit literal `()` | 570 ~ if let Some(literal) = literal.as_literal() { 571 + match literal.data_type() { 572 + Some(DataType::Iri) => { 573 + let iri = Iri::parse(literal.lexical_form())?; 574 + self.write_iri(w, graph, &iri)? 575 + } 576 + Some(DataType::Boolean) 577 + | Some(DataType::Long) 578 + | Some(DataType::Int) 579 + | Some(DataType::Short) 580 + | Some(DataType::Byte) 581 + | Some(DataType::UnsignedLong) 582 + | Some(DataType::UnsignedInt) 583 + | Some(DataType::UnsignedShort) 584 + | Some(DataType::UnsignedByte) 585 + | Some(DataType::Float) 586 + | Some(DataType::Double) 587 + | Some(DataType::Decimal) => write!(w, "{}", literal.lexical_form())?, 588 + _ => { 589 + write!(w, "{:?}", literal.lexical_form())?; 590 + match (literal.data_type(), literal.language()) { 591 + (Some(data_type), None) => { 592 + write!(w, "^^")?; 593 + let iri = data_type.as_iri(); 594 + self.write_iri(w, graph, iri)?; 595 + } 596 + (None, Some(language)) => write!(w, "@{}", language)?, 597 + _ => (), 598 + } 599 + } 600 + } 601 + } else { 602 + panic!("ERROR: this is not a literal: {:?}", literal) 603 + }; 604 + Ok(()) |
}

fn write_predicate<W: Write>(
Expand Down

0 comments on commit cb166e1

Please sign in to comment.