Skip to content

Commit

Permalink
Add command line interface as main package.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Apr 4, 2024
1 parent 9313354 commit 0be606a
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 212 deletions.
24 changes: 22 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
[package]
name = "tldr"
description = "TreeLDR Command Line Interface"
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
version.workspace = true

[workspace]
members = [
"layouts",
"layouts/cli",
"generators/rust/treeldr-rs",
"generators/rust/treeldr-rs-macros",
"generators/rust/generator"
Expand Down Expand Up @@ -44,4 +51,17 @@ stderrlog = "0.6"

syn = "2.0.29"
proc-macro2 = "1.0.66"
quote = "1.0.33"
quote = "1.0.33"

[dependencies]
treeldr-layouts.workspace = true
clap = { workspace = true, features = ["derive"] }
stderrlog.workspace = true
nquads-syntax.workspace = true
json-syntax.workspace = true
codespan-reporting.workspace = true
thiserror.workspace = true
iref.workspace = true
rdf-types.workspace = true
locspan.workspace = true
utf8-decode = "1.0.1"
14 changes: 0 additions & 14 deletions layouts/cli/Cargo.toml

This file was deleted.

94 changes: 0 additions & 94 deletions layouts/cli/README.md

This file was deleted.

102 changes: 0 additions & 102 deletions layouts/cli/src/main.rs

This file was deleted.

2 changes: 2 additions & 0 deletions layouts/src/distill/hy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum Error<R = Term> {
LayoutNotFound(Ref<LayoutType, R>),
}

static_assertions::assert_impl_all!(Error: ToString);

#[derive(Debug, thiserror::Error)]
pub enum DataFragment<R> {
#[error("layout discriminant")]
Expand Down
7 changes: 7 additions & 0 deletions layouts/src/ref.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use std::marker::PhantomData;

use educe::Educe;
Expand Down Expand Up @@ -128,6 +129,12 @@ impl<T, R> Ref<T, R> {
}
}

impl<T, R: fmt::Display> fmt::Display for Ref<T, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

/// Context able to fetch (dereference) the definition of a resource for the
/// given type `T`.
///
Expand Down
69 changes: 69 additions & 0 deletions layouts/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ impl<'a> From<&'a json_syntax::Number> for Number {
#[error("not a JSON number: {0}")]
pub struct NonJsonNumber(pub Number);

impl TryFrom<Number> for json_syntax::NumberBuf {
type Error = NonJsonNumber;

fn try_from(value: Number) -> Result<Self, Self::Error> {
match value.decimal_representation() {
Some(decimal) => match json_syntax::NumberBuf::from_str(&decimal) {
Ok(n) => Ok(n),
Err(_) => Err(NonJsonNumber(value)),
},
None => Err(NonJsonNumber(value)),
}
}
}

impl TryFrom<Number> for serde_json::Number {
type Error = NonJsonNumber;

Expand Down Expand Up @@ -321,6 +335,23 @@ impl TryFromJson for Value {
}
}

impl From<json_syntax::Value> for Value {
fn from(value: json_syntax::Value) -> Self {
match value {
json_syntax::Value::Null => Self::Literal(Literal::Unit),
json_syntax::Value::Boolean(b) => Self::Literal(Literal::Boolean(b)),
json_syntax::Value::Number(n) => Self::Literal(Literal::Number(n.into())),
json_syntax::Value::String(s) => Self::Literal(Literal::TextString(s.to_string())),
json_syntax::Value::Array(a) => Self::List(a.into_iter().map(Into::into).collect()),
json_syntax::Value::Object(o) => Self::Record(
o.into_iter()
.map(|entry| (entry.key.into_string(), entry.value.into()))
.collect(),
),
}
}
}

impl From<serde_json::Value> for Value {
fn from(value: serde_json::Value) -> Self {
match value {
Expand Down Expand Up @@ -360,6 +391,20 @@ impl From<NonJsonNumber> for NonJsonValue {
}
}

impl TryFrom<Literal> for json_syntax::Value {
type Error = NonJsonValue;

fn try_from(value: Literal) -> Result<Self, Self::Error> {
match value {
Literal::Unit => Ok(json_syntax::Value::Null),
Literal::Boolean(b) => Ok(json_syntax::Value::Boolean(b)),
Literal::Number(n) => Ok(json_syntax::Value::Number(n.try_into()?)),
Literal::TextString(s) => Ok(json_syntax::Value::String(s.into())),
Literal::ByteString(s) => Err(NonJsonValue::ByteString(s)),
}
}
}

impl TryFrom<Literal> for serde_json::Value {
type Error = NonJsonValue;

Expand Down Expand Up @@ -389,6 +434,30 @@ impl TryFrom<TypedLiteral> for serde_json::Value {
}
}

impl TryFrom<Value> for json_syntax::Value {
type Error = NonJsonValue;

fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Literal(l) => l.try_into(),
Value::Record(r) => {
let mut object = json_syntax::Object::new();

for (key, value) in r {
object.insert(key.into(), value.try_into()?);
}

Ok(json_syntax::Value::Object(object))
}
Value::List(list) => list
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()
.map(json_syntax::Value::Array),
}
}
}

impl TryFrom<Value> for serde_json::Value {
type Error = NonJsonValue;

Expand Down
5 changes: 5 additions & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod rdf;
pub mod tree;

pub use rdf::RDFFormat;
pub use tree::TreeFormat;
Loading

0 comments on commit 0be606a

Please sign in to comment.