-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rdf): created module
rdf
and moved some source files into …
…that
- Loading branch information
Showing
9 changed files
with
199 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. | ||
//--------------------------------------------------------------- | ||
mod data_type; | ||
mod lexical_value; | ||
mod resource_value; | ||
mod term; | ||
|
||
pub use data_type::DataType; | ||
pub use lexical_value::LexicalValue; | ||
pub use resource_value::ResourceValue; | ||
pub use term::Term; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. | ||
//--------------------------------------------------------------- | ||
use super::DataType; | ||
use crate::{cursor, Error, Error::Unknown}; | ||
|
||
#[derive(Debug)] | ||
pub struct ResourceValue { | ||
pub data_type: DataType, | ||
pub namespace: Option<&'static str>, | ||
pub value: &'static str, | ||
} | ||
|
||
impl ResourceValue { | ||
pub fn from( | ||
data_type: DataType, | ||
namespace: *const u8, | ||
namespace_len: usize, | ||
data: *const u8, | ||
data_len: usize, | ||
) -> Result<Self, Error> { | ||
let ns = if namespace_len == 0 { | ||
None | ||
} else { | ||
Some( | ||
cursor::ptr_to_cstr(namespace, namespace_len + 1)? | ||
.to_str() | ||
.map_err(|err| { | ||
log::error!("Couldn't convert namespace due to UTF-8 error: {err:?}"); | ||
Unknown | ||
})?, | ||
) | ||
}; | ||
Ok(Self { | ||
data_type, | ||
namespace: ns, | ||
value: cursor::ptr_to_cstr(data, data_len)?.to_str().unwrap(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::str::FromStr; | ||
|
||
// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved. | ||
//--------------------------------------------------------------- | ||
use iref::Iri; | ||
|
||
use super::LexicalValue; | ||
use crate::Error; | ||
|
||
/// An RDF Term is either an IRI, a literal or a blank node. | ||
/// See https://www.w3.org/TR/rdf11-concepts/#section-triples | ||
pub enum Term { | ||
Iri(LexicalValue), | ||
Literal(LexicalValue), | ||
BlankNode(LexicalValue), | ||
} | ||
|
||
impl Term { | ||
pub fn new_iri(iri: &Iri) -> Result<Self, Error> { Ok(Term::Iri(LexicalValue::from_iri(iri)?)) } | ||
|
||
pub fn new_str(str: &str) -> Result<Self, Error> { | ||
Ok(Term::Literal(LexicalValue::from_str(str)?)) | ||
} | ||
|
||
pub fn display_turtle<'a, 'b>(&'a self) -> impl std::fmt::Display + 'a + 'b | ||
where 'a: 'b { | ||
struct TurtleTerm<'b>(&'b Term); | ||
impl<'b> std::fmt::Display for TurtleTerm<'b> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
let value = match self.0 { | ||
Term::Iri(value) => value, | ||
Term::Literal(value) => value, | ||
Term::BlankNode(value) => value, | ||
}; | ||
write!(f, "{:?}", value) | ||
} | ||
} | ||
TurtleTerm(self) | ||
} | ||
} | ||
|
||
impl FromStr for Term { | ||
type Err = Error; | ||
|
||
fn from_str(str: &str) -> Result<Self, Self::Err> { Term::new_str(str) } | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use iref::Iri; | ||
|
||
use crate::{rdf::Term, Error}; | ||
|
||
#[test_log::test] | ||
fn test_term_01() { | ||
let term = Term::new_iri(&Iri::new("https://whatever.url").unwrap()).unwrap(); | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "<https://whatever.url>"); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_term_02() { | ||
let term = Term::new_iri(&Iri::new("unknown-protocol://whatever.url").unwrap()).unwrap(); | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "<unknown-protocol://whatever.url>"); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_term_03() { | ||
// At the moment, we're even accepting wrongly formatted IRIs, we may want to | ||
// validate each IRI | ||
let term = Term::new_iri(&Iri::new("https:/x/whatever.url").unwrap()).unwrap(); | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "<https:/x/whatever.url>"); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_term_04() { | ||
let term = Term::new_str("some string").unwrap(); | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "\"some string\""); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_term_05() -> Result<(), Error> { | ||
let term: Term = "some string".parse()?; | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "\"some string\""); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test_log::test] | ||
fn test_term_06() -> Result<(), Error> { | ||
let term: Term = "\"some string\"^^xsd:string".parse()?; | ||
|
||
let turtle = format!("{}", term.display_turtle()); | ||
|
||
assert_eq!(turtle, "\"\\\"some string\\\"^^xsd:string\""); // TODO: This is incorrect, recognise the XSD data type suffix and process it | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters