Skip to content

Commit

Permalink
add prefixes api
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Dec 1, 2024
1 parent 5b372ab commit 6cf5597
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# turtle parser

<i>transforms a turtle string to a TurtleDoc</i>

## Work in progress
Expand All @@ -8,14 +9,16 @@
- Comments are filtered

## Todo

- error handling
- iri as described in the spec
- <s>iri as described in the spec</s>
- cleanup
- Jena like api

### Example

#### Input

```turtle
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
Expand All @@ -26,7 +29,9 @@
foaf:mbox <[email protected]>] .
```

#### Output

```turtle
<http://example.org/.well-known/genid#e162c9a7-52cf-4240-9359-b1b1f977f642> <http://foaf.com/name> "Alice"^^<http://www.w3.org/2001/XMLSchema#string>.
<http://example.org/.well-known/genid#6955800d-16db-49f8-a614-b0cbea3d7fb2> <http://foaf.com/name> "Bob"^^<http://www.w3.org/2001/XMLSchema#string>.
Expand All @@ -39,11 +44,14 @@
```

#### Input

```turtle
@prefix : <http://example.com/>.
:a :b ( "apple" "banana" ) .
```

#### Output

```turtle
<http://example.org/.well-known/genid#018a16a1-9f82-4acd-930e-2be1ea090e83> <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "apple"^^<http://www.w3.org/2001/XMLSchema#string>.
<http://example.org/.well-known/genid#4ef6beb7-5e21-4d77-9459-2662b6845375> <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "banana"^^<http://www.w3.org/2001/XMLSchema#string>.
Expand Down
21 changes: 20 additions & 1 deletion src/turtle/turtle_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,26 @@ impl<'a> TurtleDoc<'a> {
})?;
(buf.as_str(), well_known_prefix).try_into()
}

pub fn add_prefixes(
&mut self,
prefixes: BTreeMap<String, String>,
) -> Result<(), TurtleDocError> {
let base = self.base.unwrap_or("");
let mut prefixes: BTreeMap<Cow<str>, Cow<str>> = prefixes
.into_iter()
.map(|(k, v)| (Cow::Owned(k), Cow::Owned(v)))
.collect();
for (_, prefix) in prefixes.iter_mut() {
let iri = IRI::try_from(prefix.as_ref()).map_err(|e| TurtleDocError {
message: e.to_string(),
})?;
if iri.is_relative() {
*prefix = Cow::Owned(format!("{base}{prefix}"));
}
}
self.prefixes.extend(prefixes);
Ok(())
}
pub fn add_statement(&mut self, subject: Node<'a>, predicate: Node<'a>, object: Node<'a>) {
let stmt = Statement {
subject,
Expand Down

0 comments on commit 6cf5597

Please sign in to comment.