Skip to content

Commit

Permalink
XRPC query support.
Browse files Browse the repository at this point in the history
Generate a type for each input and output.
  • Loading branch information
timothee-haudebourg committed May 8, 2023
1 parent 76c76ad commit 4e2cf43
Show file tree
Hide file tree
Showing 6 changed files with 1,098 additions and 163 deletions.
7 changes: 2 additions & 5 deletions load/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum DeclaredDocument {
#[cfg(feature = "turtle")]
Turtle(Dataset),

Json(Box<json::Document>),
Json(Box<json::DeclaredDocument>),
}

impl Document {
Expand Down Expand Up @@ -143,10 +143,7 @@ impl Document {
.map_err(LangError::NQuads)?;
Ok(DeclaredDocument::NQuads(dataset))
}
Self::Json(d) => {
d.declare(context, vocabulary, generator)?;
Ok(DeclaredDocument::Json(d))
}
Self::Json(d) => Ok(d.declare(context, vocabulary, generator)?),
}
}
}
Expand Down
72 changes: 61 additions & 11 deletions load/src/document/json.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use json_syntax::Parse;
use locspan::Location;
use rdf_types::{Generator, VocabularyMut};
use locspan::{Location, Meta};
use rdf_types::{Generator, Id, VocabularyMut};
use treeldr::{BlankIdIndex, IriIndex};

use crate::{source, BuildContext, LangError, LoadError};
use crate::{source, BuildContext, Dataset, LangError, LoadError};

#[cfg(feature = "json-schema")]
pub mod schema;
Expand Down Expand Up @@ -53,27 +53,77 @@ pub enum Document {

impl Document {
pub fn declare<V: VocabularyMut<Iri = IriIndex, BlankId = BlankIdIndex>>(
&self,
self,
context: &mut BuildContext,
vocabulary: &mut V,
generator: &mut impl Generator<V>,
) -> Result<(), LangError> {
) -> Result<crate::document::DeclaredDocument, LangError> {
match self {
#[cfg(feature = "json-schema")]
Self::Schema(s) => {
treeldr_json_schema::import_schema(s, None, context, vocabulary, generator)?;
Ok(())
treeldr_json_schema::import_schema(&s, None, context, vocabulary, generator)?;
Ok(crate::document::DeclaredDocument::Json(Box::new(
DeclaredDocument::Schema(s),
)))
}
#[cfg(feature = "lexicon")]
Self::Lexicon(_d) => {
// treeldr_lexicon::import(&s, None, context, vocabulary, generator)?;
// ...
unimplemented!()
Self::Lexicon(d) => {
let dataset: Dataset = d
.into_triples(vocabulary, &mut *generator)
.map(|triple| {
Meta(
triple
.map_subject(|s| Meta(s, source::Metadata::default()))
.map_predicate(|p| Meta(Id::Iri(p), source::Metadata::default()))
.map_object(|o| Meta(label_object(o), source::Metadata::default()))
.into_quad(None),
source::Metadata::default(),
)
})
.collect();

use treeldr_build::Document;
dataset
.declare(&mut (), context, vocabulary, generator)
.map_err(LangError::NQuads)?;
Ok(crate::document::DeclaredDocument::NQuads(dataset))
}
}
}
}

fn label_object(
object: rdf_types::Object<treeldr::Id, rdf_types::Literal<String, treeldr::IriIndex>>,
) -> rdf_types::meta::Object<source::Metadata, treeldr::Id, String, treeldr::IriIndex> {
match object {
rdf_types::Object::Id(id) => rdf_types::meta::Object::Id(id),
rdf_types::Object::Literal(l) => rdf_types::meta::Object::Literal(label_literal(l)),
}
}

fn label_literal(
literal: rdf_types::Literal<String, treeldr::IriIndex>,
) -> rdf_types::meta::Literal<source::Metadata, String, treeldr::IriIndex> {
match literal {
rdf_types::Literal::String(s) => {
rdf_types::meta::Literal::String(Meta(s, source::Metadata::default()))
}
rdf_types::Literal::TypedString(s, t) => rdf_types::meta::Literal::TypedString(
Meta(s, source::Metadata::default()),
Meta(t, source::Metadata::default()),
),
rdf_types::Literal::LangString(s, t) => rdf_types::meta::Literal::LangString(
Meta(s, source::Metadata::default()),
Meta(t, source::Metadata::default()),
),
}
}

pub enum DeclaredDocument {
#[cfg(feature = "json-schema")]
Schema(treeldr_json_schema::Schema),
}

pub fn import<P>(
files: &source::Files<P>,
source_id: source::FileId,
Expand Down
1 change: 1 addition & 0 deletions modules/lexicon/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hard_tabs = true
5 changes: 4 additions & 1 deletion modules/lexicon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ edition.workspace = true
treeldr.workspace = true
log.workspace = true
iref.workspace = true
static-iref.workspace = true
contextual.workspace = true
clap = { workspace = true, features = ["derive"] }
derivative.workspace = true
thiserror.workspace = true
json-syntax = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_repr = "0.1"
serde_repr = "0.1"
rdf-types.workspace = true
Loading

0 comments on commit 4e2cf43

Please sign in to comment.