Skip to content

Commit

Permalink
feat(Rust): Add trait for type name and id
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jun 4, 2021
1 parent 43c0554 commit fd6f0a3
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 103 deletions.
19 changes: 19 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["schema", "executable", "documents"]
[dependencies]
chrono = "0.4.19"
defaults = "0.2.0"
enum_dispatch = "0.3.7"
serde = {version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64"
serde_with = "1.9.1"
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![recursion_limit = "256"]

mod prelude;
pub use prelude::NodeTrait;
pub use prelude::Primitive;

#[rustfmt::skip]
Expand Down
68 changes: 51 additions & 17 deletions rust/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
pub use defaults::Defaults;
pub use enum_dispatch::enum_dispatch;
pub use serde::{de, Deserialize, Deserializer, Serialize};
pub use serde_json::Value;
pub use serde_with::skip_serializing_none;
use std::collections::BTreeMap;
pub use std::sync::Arc;

/// A trait for methods that can be called on all types of nodes
#[enum_dispatch]
pub trait NodeTrait {
/// Retrieve the `type` of an entity
/// Needs to be called `type_name` because `type` is a reserved word
fn type_name(&self) -> String;

/// Retrieve the `id` of an entity
fn id(&self) -> Option<String>;
}

macro_rules! impl_primitive {
($type:ident) => {
impl NodeTrait for $type {
fn type_name(&self) -> String {
stringify!($type).into()
}

fn id(&self) -> Option<String> {
None
}
}
};
}

/// The set of primitive (non-Entity) node types
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
Expand All @@ -20,39 +46,33 @@ pub enum Primitive {

/// A boolean value
pub type Boolean = bool;
impl_primitive!(Boolean);

/// An integer value
///
/// Uses `i64` for maximum precision.
pub type Integer = i64;
impl_primitive!(Integer);

/// A floating point value (a.k.a real number)
///
/// Uses `i64` for maximum precision.
pub type Number = f64;
impl_primitive!(Number);

/// An array value (a.k.a. vector)
pub type Array = Vec<Primitive>;
impl_primitive!(Array);

/// An object value (a.k.a map, dictionary)
///
/// Uses `BTreeMap` to preserve order.
pub type Object = BTreeMap<String, Primitive>;
impl_primitive!(Object);

/// A trait to retrieve the `type` of entities
/// Needs to be called `type_name` because `type` is a reserved word
pub trait TypeName {
fn type_name(&self) -> String;
}

/// A trait to retrieve the `id` of entities
pub trait Id {
fn id(&self) -> Option<String>;
}

/// Macro to implement functions and types for a schema type
/// Macro to implement functions for struct schemas
#[macro_export]
macro_rules! impl_type {
macro_rules! impl_struct {
($type:ident) => {
impl $type {
/// Deserialize the `type` property
Expand All @@ -74,15 +94,29 @@ macro_rules! impl_type {
}
}

impl TypeName for $type {
impl NodeTrait for $type {
fn type_name(&self) -> String {
return self.type_.clone();
stringify!($type).into()
}

fn id(&self) -> Option<String> {
self.id.clone()
}
}
};
}

/// Macro to implement functions for enum schemas
#[macro_export]
macro_rules! impl_enum {
($type:ident) => {
impl NodeTrait for $type {
fn type_name(&self) -> String {
stringify!($type).into()
}

impl Id for $type {
fn id(&self) -> Option<String> {
return self.id.clone();
None
}
}
};
Expand Down
Loading

0 comments on commit fd6f0a3

Please sign in to comment.