adding serde to the data model

main
Niko PLP 7 months ago
parent a4e2847810
commit 7b0b60cda0
  1. 2
      Cargo.lock
  2. 1
      Cargo.toml
  3. 1
      lib/oxrdf/Cargo.toml
  4. 7
      lib/oxrdf/src/blank_node.rs
  5. 5
      lib/oxrdf/src/literal.rs
  6. 3
      lib/oxrdf/src/named_node.rs
  7. 9
      lib/oxrdf/src/triple.rs
  8. 1
      lib/oxsdatatypes/Cargo.toml
  9. 5
      lib/oxsdatatypes/src/boolean.rs
  10. 9
      lib/oxsdatatypes/src/date_time.rs
  11. 5
      lib/oxsdatatypes/src/decimal.rs
  12. 3
      lib/oxsdatatypes/src/double.rs
  13. 11
      lib/oxsdatatypes/src/duration.rs
  14. 3
      lib/oxsdatatypes/src/float.rs
  15. 5
      lib/oxsdatatypes/src/integer.rs

2
Cargo.lock generated

@ -867,6 +867,7 @@ dependencies = [
"oxiri", "oxiri",
"oxsdatatypes", "oxsdatatypes",
"rand", "rand",
"serde",
"thiserror", "thiserror",
] ]
@ -898,6 +899,7 @@ name = "oxsdatatypes"
version = "0.2.0-alpha.1" version = "0.2.0-alpha.1"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"serde",
"thiserror", "thiserror",
] ]

@ -24,6 +24,7 @@ rust-version = "1.70"
[workspace.dependencies] [workspace.dependencies]
rocksdb = {git = "https://git.nextgraph.org/NextGraph/rust-rocksdb.git", branch = "master", features = [ ] } rocksdb = {git = "https://git.nextgraph.org/NextGraph/rust-rocksdb.git", branch = "master", features = [ ] }
serde = { version = "1.0.142", features = ["derive"] }
anyhow = "1.0.72" anyhow = "1.0.72"
arbitrary = "1.3" arbitrary = "1.3"
assert_cmd = "2.0" assert_cmd = "2.0"

@ -23,6 +23,7 @@ oxiri.workspace = true
oxsdatatypes = { workspace = true, optional = true } oxsdatatypes = { workspace = true, optional = true }
rand.workspace = true rand.workspace = true
thiserror.workspace = true thiserror.workspace = true
serde.workspace = true
[lints] [lints]
workspace = true workspace = true

@ -1,4 +1,5 @@
use rand::random; use rand::random;
use serde::{Deserialize, Serialize};
use std::io::Write; use std::io::Write;
use std::{fmt, str}; use std::{fmt, str};
@ -16,10 +17,10 @@ use std::{fmt, str};
/// assert_eq!("_:a122", BlankNode::new("a122")?.to_string()); /// assert_eq!("_:a122", BlankNode::new("a122")?.to_string());
/// # Result::<_,oxrdf::BlankNodeIdParseError>::Ok(()) /// # Result::<_,oxrdf::BlankNodeIdParseError>::Ok(())
/// ``` /// ```
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct BlankNode(BlankNodeContent); pub struct BlankNode(BlankNodeContent);
#[derive(PartialEq, Eq, Debug, Clone, Hash)] #[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
enum BlankNodeContent { enum BlankNodeContent {
Named(String), Named(String),
Anonymous { id: u128, str: IdStr }, Anonymous { id: u128, str: IdStr },
@ -246,7 +247,7 @@ impl PartialEq<BlankNodeRef<'_>> for BlankNode {
} }
} }
#[derive(PartialEq, Eq, Debug, Clone, Hash)] #[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
struct IdStr([u8; 32]); struct IdStr([u8; 32]);
impl IdStr { impl IdStr {

@ -3,6 +3,7 @@ use crate::vocab::{rdf, xsd};
use oxilangtag::{LanguageTag, LanguageTagParseError}; use oxilangtag::{LanguageTag, LanguageTagParseError};
#[cfg(feature = "oxsdatatypes")] #[cfg(feature = "oxsdatatypes")]
use oxsdatatypes::*; use oxsdatatypes::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::fmt::Write; use std::fmt::Write;
@ -31,10 +32,10 @@ use std::fmt::Write;
/// ); /// );
/// # Result::<(), LanguageTagParseError>::Ok(()) /// # Result::<(), LanguageTagParseError>::Ok(())
/// ``` /// ```
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct Literal(LiteralContent); pub struct Literal(LiteralContent);
#[derive(PartialEq, Eq, Debug, Clone, Hash)] #[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
enum LiteralContent { enum LiteralContent {
String(String), String(String),
LanguageTaggedString { value: String, language: String }, LanguageTaggedString { value: String, language: String },

@ -1,4 +1,5 @@
use oxiri::{Iri, IriParseError}; use oxiri::{Iri, IriParseError};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
@ -14,7 +15,7 @@ use std::fmt;
/// ); /// );
/// # Result::<_,oxrdf::IriParseError>::Ok(()) /// # Result::<_,oxrdf::IriParseError>::Ok(())
/// ``` /// ```
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct NamedNode { pub struct NamedNode {
iri: String, iri: String,
} }

@ -2,10 +2,11 @@ use crate::blank_node::BlankNode;
use crate::literal::Literal; use crate::literal::Literal;
use crate::named_node::NamedNode; use crate::named_node::NamedNode;
use crate::{BlankNodeRef, LiteralRef, NamedNodeRef}; use crate::{BlankNodeRef, LiteralRef, NamedNodeRef};
use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
/// The owned union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node). /// The owned union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub enum NamedOrBlankNode { pub enum NamedOrBlankNode {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), BlankNode(BlankNode),
@ -152,7 +153,7 @@ impl<'a> From<NamedOrBlankNodeRef<'a>> for NamedOrBlankNode {
} }
/// The owned union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) (if the `rdf-star` feature is enabled). /// The owned union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) (if the `rdf-star` feature is enabled).
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub enum Subject { pub enum Subject {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), BlankNode(BlankNode),
@ -382,7 +383,7 @@ impl<'a> From<&'a NamedOrBlankNode> for SubjectRef<'a> {
/// An owned RDF [term](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-term) /// An owned RDF [term](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-term)
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node), [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) (if the `rdf-star` feature is enabled). /// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node), [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) (if the `rdf-star` feature is enabled).
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)]
pub enum Term { pub enum Term {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), BlankNode(BlankNode),
@ -937,7 +938,7 @@ impl<'a> From<TripleRef<'a>> for Triple {
/// A possible owned graph name. /// A possible owned graph name.
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node), and the [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph). /// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node), and the [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph).
#[derive(Eq, PartialEq, Debug, Clone, Hash, Default)] #[derive(Eq, PartialEq, Debug, Clone, Hash, Default, Serialize, Deserialize)]
pub enum GraphName { pub enum GraphName {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), BlankNode(BlankNode),

@ -19,6 +19,7 @@ custom-now = []
[dependencies] [dependencies]
thiserror.workspace = true thiserror.workspace = true
serde.workspace = true
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies] [target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
js-sys = { workspace = true, optional = true } js-sys = { workspace = true, optional = true }

@ -1,11 +1,14 @@
use crate::{Decimal, Double, Float, Integer}; use crate::{Decimal, Double, Float, Integer};
use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::str::{FromStr, ParseBoolError}; use std::str::{FromStr, ParseBoolError};
/// [XML Schema `boolean` datatype](https://www.w3.org/TR/xmlschema11-2/#boolean) /// [XML Schema `boolean` datatype](https://www.w3.org/TR/xmlschema11-2/#boolean)
/// ///
/// Uses internally a [`bool`]. /// Uses internally a [`bool`].
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(
Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
#[repr(transparent)] #[repr(transparent)]
pub struct Boolean { pub struct Boolean {
value: bool, value: bool,

@ -1,6 +1,7 @@
#![allow(clippy::expect_used)] #![allow(clippy::expect_used)]
use crate::{DayTimeDuration, Decimal, Duration, YearMonthDuration}; use crate::{DayTimeDuration, Decimal, Duration, YearMonthDuration};
use serde::{Deserialize, Serialize};
use std::cmp::{min, Ordering}; use std::cmp::{min, Ordering};
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -10,7 +11,7 @@ use std::str::FromStr;
/// ///
/// It encodes the value using a number of seconds from the Gregorian calendar era using a [`Decimal`] /// It encodes the value using a number of seconds from the Gregorian calendar era using a [`Decimal`]
/// and an optional timezone offset in minutes. /// and an optional timezone offset in minutes.
#[derive(Eq, PartialEq, PartialOrd, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, PartialOrd, Debug, Clone, Copy, Hash, Serialize, Deserialize)]
pub struct DateTime { pub struct DateTime {
timestamp: Timestamp, timestamp: Timestamp,
} }
@ -1452,7 +1453,7 @@ impl fmt::Display for GDay {
/// A timezone offset with respect to UTC. /// A timezone offset with respect to UTC.
/// ///
/// It is encoded as a number of minutes between -PT14H and PT14H. /// It is encoded as a number of minutes between -PT14H and PT14H.
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)] #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Serialize, Deserialize)]
pub struct TimezoneOffset { pub struct TimezoneOffset {
offset: i16, // in minute with respect to UTC offset: i16, // in minute with respect to UTC
} }
@ -1562,7 +1563,7 @@ struct DateTimeSevenPropertyModel {
timezone_offset: Option<TimezoneOffset>, timezone_offset: Option<TimezoneOffset>,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct Timestamp { struct Timestamp {
value: Decimal, value: Decimal,
timezone_offset: Option<TimezoneOffset>, timezone_offset: Option<TimezoneOffset>,
@ -3140,7 +3141,7 @@ mod tests {
fn custom_now() { fn custom_now() {
#[allow(unsafe_code)] #[allow(unsafe_code)]
#[no_mangle] #[no_mangle]
extern "Rust" fn custom_ox_now() -> Duration { fn custom_ox_now() -> Duration {
Duration::default() Duration::default()
} }
DateTime::now(); DateTime::now();

@ -1,4 +1,5 @@
use crate::{Boolean, Double, Float, Integer, TooLargeForIntegerError}; use crate::{Boolean, Double, Float, Integer, TooLargeForIntegerError};
use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::fmt::Write; use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
@ -12,7 +13,9 @@ const DECIMAL_PART_POW_MINUS_ONE: i128 = 100_000_000_000_000_000;
/// It stores the decimal in a fix point encoding allowing nearly 18 digits before and 18 digits after ".". /// It stores the decimal in a fix point encoding allowing nearly 18 digits before and 18 digits after ".".
/// ///
/// It stores the value in a [`i128`] integer after multiplying it by 10¹⁸. /// It stores the value in a [`i128`] integer after multiplying it by 10¹⁸.
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default)] #[derive(
Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default, Serialize, Deserialize,
)]
pub struct Decimal { pub struct Decimal {
value: i128, // value * 10^18 value: i128, // value * 10^18
} }

@ -1,4 +1,5 @@
use crate::{Boolean, Float, Integer}; use crate::{Boolean, Float, Integer};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::num::ParseFloatError; use std::num::ParseFloatError;
@ -10,7 +11,7 @@ use std::str::FromStr;
/// Uses internally a [`f64`]. /// Uses internally a [`f64`].
/// ///
/// <div class="warning">Serialization does not follow the canonical mapping.</div> /// <div class="warning">Serialization does not follow the canonical mapping.</div>
#[derive(Debug, Clone, Copy, Default, PartialEq)] #[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
#[repr(transparent)] #[repr(transparent)]
pub struct Double { pub struct Double {
value: f64, value: f64,

@ -1,4 +1,5 @@
use crate::{DateTime, Decimal}; use crate::{DateTime, Decimal};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
@ -7,7 +8,7 @@ use std::time::Duration as StdDuration;
/// [XML Schema `duration` datatype](https://www.w3.org/TR/xmlschema11-2/#duration) /// [XML Schema `duration` datatype](https://www.w3.org/TR/xmlschema11-2/#duration)
/// ///
/// It stores the duration using a pair of a [`YearMonthDuration`] and a [`DayTimeDuration`]. /// It stores the duration using a pair of a [`YearMonthDuration`] and a [`DayTimeDuration`].
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Default)] #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Default, Serialize, Deserialize)]
pub struct Duration { pub struct Duration {
year_month: YearMonthDuration, year_month: YearMonthDuration,
day_time: DayTimeDuration, day_time: DayTimeDuration,
@ -293,7 +294,9 @@ impl PartialOrd for Duration {
/// [XML Schema `yearMonthDuration` datatype](https://www.w3.org/TR/xmlschema11-2/#yearMonthDuration) /// [XML Schema `yearMonthDuration` datatype](https://www.w3.org/TR/xmlschema11-2/#yearMonthDuration)
/// ///
/// It stores the duration as a number of months encoded using a [`i64`]. /// It stores the duration as a number of months encoded using a [`i64`].
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default)] #[derive(
Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default, Serialize, Deserialize,
)]
pub struct YearMonthDuration { pub struct YearMonthDuration {
months: i64, months: i64,
} }
@ -460,7 +463,9 @@ impl PartialOrd<YearMonthDuration> for Duration {
/// [XML Schema `dayTimeDuration` datatype](https://www.w3.org/TR/xmlschema11-2/#dayTimeDuration) /// [XML Schema `dayTimeDuration` datatype](https://www.w3.org/TR/xmlschema11-2/#dayTimeDuration)
/// ///
/// It stores the duration as a number of seconds encoded using a [`Decimal`]. /// It stores the duration as a number of seconds encoded using a [`Decimal`].
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default)] #[derive(
Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash, Default, Serialize, Deserialize,
)]
pub struct DayTimeDuration { pub struct DayTimeDuration {
seconds: Decimal, seconds: Decimal,
} }

@ -1,4 +1,5 @@
use crate::{Boolean, Double, Integer}; use crate::{Boolean, Double, Integer};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::num::ParseFloatError; use std::num::ParseFloatError;
@ -10,7 +11,7 @@ use std::str::FromStr;
/// Uses internally a [`f32`]. /// Uses internally a [`f32`].
/// ///
/// <div class="warning">Serialization does not follow the canonical mapping.</div> /// <div class="warning">Serialization does not follow the canonical mapping.</div>
#[derive(Debug, Clone, Copy, Default, PartialEq)] #[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
#[repr(transparent)] #[repr(transparent)]
pub struct Float { pub struct Float {
value: f32, value: f32,

@ -1,4 +1,5 @@
use crate::{Boolean, Decimal, Double, Float}; use crate::{Boolean, Decimal, Double, Float};
use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::num::ParseIntError; use std::num::ParseIntError;
use std::str::FromStr; use std::str::FromStr;
@ -6,7 +7,9 @@ use std::str::FromStr;
/// [XML Schema `integer` datatype](https://www.w3.org/TR/xmlschema11-2/#integer) /// [XML Schema `integer` datatype](https://www.w3.org/TR/xmlschema11-2/#integer)
/// ///
/// Uses internally a [`i64`]. /// Uses internally a [`i64`].
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(
Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
#[repr(transparent)] #[repr(transparent)]
pub struct Integer { pub struct Integer {
value: i64, value: i64,

Loading…
Cancel
Save