Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,080 changes: 1,080 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
resolver = "2"

members = [
"packages/tlm-rust"
"packages/tlm-rust",
"packages/tlm-tests-rust"
]
1 change: 1 addition & 0 deletions packages/tlm-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static = "1.5.0"
5 changes: 5 additions & 0 deletions packages/tlm-rust/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod schema;
mod meta;

pub use schema::*;
pub use meta::*;
100 changes: 100 additions & 0 deletions packages/tlm-rust/src/core/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use lazy_static::lazy_static;
use crate::TlmNamespace;

lazy_static! {
pub static ref TLM_CORE_NAMESPACES: Vec<TlmNamespace> = vec![
TlmNamespace::new(
1,
String::from("tlm"),
String::from("https://type.link.model.tools/ns/tlm/")
).describe(String::from("The Core TLM namespace.")),
TlmNamespace::new(
2,
String::from("xs"),
String::from("http://www.w3.org/2001/XMLSchema")
).describe(String::from("Namespaces for XML Schema DataTypes.")),
];

// pub static ref TLM_CORE_TYPES: Vec<TlmType> = vec![
// TlmType::new(3, 1, String::from("Namespace"), 4).describe(String::from("The special Type for Namespaces.")),
// TlmType::new(4, 1, String::from("Type"), 4).describe(String::from("The special Type for Types (the meta-Type).")),
// TlmType::new(5, 1, String::from("ValueType"), 4).describe(String::from("Simple kind of Object that is a simple primitive value.")),
// TlmType::new(6, 2, String::from("DataType"), 5).describe(String::from("XML Schema compatible value.")),
// TlmType::new(7, 2, String::from("boolean"), 6).describe(String::from("XML Schema compatible boolean.")),
// TlmType::new(8, 2, String::from("float"), 6).describe(String::from("XML Schema compatible float.")),
// TlmType::new(9, 2, String::from("double"), 6).describe(String::from("XML Schema compatible double.")),
// TlmType::new(10, 2, String::from("decimal"), 6).describe(String::from("XML Schema compatible decimal.")),
// TlmType::new(11, 2, String::from("integer"), 10).describe(String::from("XML Schema compatible integer.")),
// TlmType::new(12, 2, String::from("long"), 11).describe(String::from("XML Schema compatible long.")),
// TlmType::new(13, 2, String::from("int"), 12).describe(String::from("XML Schema compatible int.")),
// TlmType::new(14, 2, String::from("short"), 13).describe(String::from("XML Schema compatible short.")),
// TlmType::new(15, 2, String::from("byte"), 14).describe(String::from("XML Schema compatible byte.")),
// TlmType::new(16, 2, String::from("string"), 6).describe(String::from("XML Schema compatible string.")),
// TlmType::new(17, 2, String::from("anyURI"), 6).describe(String::from("XML Schema compatible anyURI.")),
// TlmType::new(18, 2, String::from("duration"), 6).describe(String::from("XML Schema compatible duration.")),
// TlmType::new(19, 2, String::from("dateTime"), 6).describe(String::from("XML Schema compatible dateTime.")),
// TlmType::new(20, 2, String::from("time"), 6).describe(String::from("XML Schema compatible time.")),
// TlmType::new(21, 2, String::from("date"), 6).describe(String::from("XML Schema compatible date.")),
// TlmType::new(22, 2, String::from("gYearMonth"), 6).describe(String::from("XML Schema compatible date.")),
// TlmType::new(23, 2, String::from("gYear"), 6).describe(String::from("XML Schema compatible gYear.")),
// TlmType::new(24, 2, String::from("gMonthDay"), 6).describe(String::from("XML Schema compatible gMonthDay.")),
// TlmType::new(25, 2, String::from("gDay"), 6).describe(String::from("XML Schema compatible gDay.")),
// TlmType::new(26, 2, String::from("gMonth"), 6).describe(String::from("XML Schema compatible gMonth.")),
// TlmType::new(27, 2, String::from("dateTimeStamp"), 19).describe(String::from("XML Schema compatible dateTimeStamp.")),
// TlmType::new(28, 2, String::from("dayTimeDuration"), 18).describe(String::from("XML Schema compatible dayTimeDuration.")),
// TlmType::new(29, 2, String::from("yearMonthDuration"), 18).describe(String::from("XML Schema compatible yearMonthDuration.")),
// TlmType::new(30, 2, String::from("base64Binary"), 6).describe(String::from("XML Schema compatible base64Binary.")),
// TlmType::new(31, 2, String::from("hexBinary"), 6).describe(String::from("XML Schema compatible hexBinary.")),
// TlmType::new(32, 2, String::from("nonPositiveInteger"), 11).describe(String::from("XML Schema compatible nonPositiveInteger.")),
// TlmType::new(33, 2, String::from("negativeInteger"), 32).describe(String::from("XML Schema compatible negativeInteger.")),
// TlmType::new(34, 2, String::from("nonNegativeInteger"), 11).describe(String::from("XML Schema compatible nonNegativeInteger.")),
// TlmType::new(35, 2, String::from("positiveInteger"), 34).describe(String::from("XML Schema compatible positiveInteger.")),
// TlmType::new(36, 2, String::from("unsignedLong"), 34).describe(String::from("XML Schema compatible unsignedLong.")),
// TlmType::new(37, 2, String::from("unsignedInt"), 36).describe(String::from("XML Schema compatible unsignedInt.")),
// TlmType::new(38, 2, String::from("unsignedShort"), 37).describe(String::from("XML Schema compatible unsignedShort.")),
// TlmType::new(39, 2, String::from("unsignedByte"), 38).describe(String::from("XML Schema compatible unsignedByte.")),
// TlmType::new(40, 2, String::from("normalizedString"), 16).describe(String::from("XML Schema compatible normalizedString.")),
// TlmType::new(41, 2, String::from("token"), 40).describe(String::from("XML Schema compatible token.")),
// TlmType::new(42, 2, String::from("language"), 41).describe(String::from("XML Schema compatible language.")),
// TlmType::new(43, 2, String::from("NMTOKEN"), 41).describe(String::from("XML Schema compatible NMTOKEN.")),
// TlmType::new(44, 2, String::from("Name"), 41).describe(String::from("XML Schema compatible Name.")),
// TlmType::new(45, 2, String::from("NCName"), 44).describe(String::from("XML Schema compatible NCName.")),
// TlmType::new(46, 2, String::from("IDREF"), 45).describe(String::from("XML Schema compatible IDREF.")),
// TlmType::new(47, 2, String::from("ID"), 45).describe(String::from("XML Schema compatible ID.")),
// TlmType::new(48, 2, String::from("ENTITY"), 45).describe(String::from("XML Schema compatible ENTITY.")),
// TlmType::new(49, 1, String::from("URI"), 17).describe(String::from("An ASCII uniform resource identifier per RFC3986.")),
// TlmType::new(50, 1, String::from("URL"), 49).describe(String::from("An ASCII uniform resource locator per WHATWG URL Standard.")),
// TlmType::new(51, 1, String::from("URN"), 49).describe(String::from("An ASCII uniform resource name per RFC8141.")),
// TlmType::new(52, 1, String::from("UUID"), 51).describe(String::from("An ASCII universally unique identifier per RFC4122.")),
// TlmType::new(53, 1, String::from("Link"), 4).describe(String::from("A relation between types.")),
// TlmType::new(71, 1, String::from("Fact"), 4).describe(String::from("A statement about an identified Object in the world considered to be true.")),
// TlmType::new(74, 1, String::from("LinkFact"), 71).describe(String::from("A fact about an object its relation to another object.")),
// TlmType::new(76, 1, String::from("ToggleFact"), 71).describe(String::from("A fact about an object that is either true (defined) or false (absent).")),
// TlmType::new(78, 1, String::from("ValueFact"), 74).describe(String::from("A fact about an object that has as its target a primitive value.")),
// ];

// pub static ref TLM_CORE_LINKS: Vec<TlmLink> = vec![
// vec![
// TlmLink::new(54, 53, 4, String::from("from type"), true, false, true, false, false, false, false).describe(String::from("The type this link belongs to.")),
// TlmLink::new(55, 53, 4, String::from("to type"), true, false, true, false, false, false, false).describe(String::from("The type this link points to.")),
// TlmLink::new(56, 53, 44, String::from("name"), true, false, true, false, false, true, false).describe(String::from("The name of the link.")),
// TlmLink::new(57, 53, 16, String::from("from name"), true, false, false, false, false, true, false).describe(String::from("The name for the link for the "from" type.")),
// TlmLink::new(58, 53, 16, String::from("to name"), true, false, false, false, false, true, false).describe(String::from("The name for the link for the "to" type.")),
// TlmLink::new(59, 53, 7, String::from("is singular"), true, false, true, false, true, true, false).describe(String::from("Whether there can be a single link like this for the "from" type.")),
// TlmLink::new(60, 53, 7, String::from("is mandatory"), true, false, true, false, true, true, false).describe(String::from("Whether there has to be a value for the "from" type.")),
// TlmLink::new(61, 53, 7, String::from("is toggle"), true, false, true, false, true, true, false).describe(String::from("Whether this is a simple boolean link.")),
// TlmLink::new(62, 53, 7, String::from("is value"), true, false, true, false, true, true, false).describe(String::from("Whether this link is to a ValueType.")),
// TlmLink::new(63, 53, 7, String::from("is primary id"), true, false, true, false, true, true, false).describe(String::from("Whether this link is to a primary identifier.")),
// TlmLink::new(64, 3, 45, String::from("prefix"), true, false, true, false, false, true, false).describe(String::from("Shorthand identifier for the Namespace. Must be valid XML namespace prefix, i.e. match [a-z0-9]+.")),
// TlmLink::new(65, 3, 16, String::from("description"), true, false, false, false, false, true, false).describe(String::from("Friendly human-readable description of the Namespace.")),
// TlmLink::new(66, 4, 4, String::from("type"), true, false, true, false, false, false, false).describe(String::from("The Type of the Object itself.")),
// TlmLink::new(67, 4, 3, String::from("namespace"), true, false, true, false, false, false, false).describe(String::from("The namespace this Type belongs to.")),
// TlmLink::new(68, 4, 4, String::from("super"), true, false, true, false, false, false, false).describe(String::from("The supertype of this Type. It inherits all possible Links from its supertype.")),
// TlmLink::new(69, 4, 44, String::from("name"), true, false, true, false, false, false, false).describe(String::from("The name of the Type. Should be unique within a namespace.")),
// TlmLink::new(70, 4, 16, String::from("description"), true, false, false, false, false, true, false).describe(String::from("Friendly human-readable description of the Type.")),
// TlmLink::new(72, 71, 4, String::from("subject"), true, false, true, false, false, false, false).describe(String::from("The object this fact is about.")),
// TlmLink::new(73, 71, 53, String::from("link"), true, false, true, false, false, false, false).describe(String::from("The kind of fact this is, defined by the kind of Link.")),
// TlmLink::new(75, 74, 4, String::from("target"), false, false, true, false, false, false, false).describe(String::from("The target object of this link fact.")),
// TlmLink::new(77, 76, 7, String::from("toggle"), true, false, true, false, true, true, false).describe(String::from("Whether the specific toggle is set or not for this Object.")),
// ];
}
82 changes: 82 additions & 0 deletions packages/tlm-rust/src/core/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

pub type TlmOid = i32; // psql serial goes from 1 to 2147483647

pub const TLM_TYPE_NAMESPACE: TlmOid = 3;
// const TLM_TYPE_TYPE: TlmOid = 4;
// const TLM_TYPE_LINK: TlmOid = 53;

pub trait TlmObject : Clone + Eq + Default {
fn oid(&self) -> TlmOid;
fn tlm_type(&self) -> TlmOid;
}

#[derive(Debug,Clone,Hash,PartialEq,Eq,Default)]
pub struct TlmNamespace {
pub oid: TlmOid,
pub prefix: String,
pub uri: String,
pub description: Option<String>
}

impl TlmObject for TlmNamespace {
fn oid(&self) -> TlmOid {
self.oid
}

fn tlm_type(&self) -> TlmOid {
TLM_TYPE_NAMESPACE
}
}

impl TlmNamespace {
pub fn new(oid: TlmOid, prefix: String, uri: String) -> TlmNamespace {
TlmNamespace {
oid,
prefix,
uri,
..TlmNamespace::default()
}
}

pub fn describe(self, description: String) -> TlmNamespace {
TlmNamespace {
description: Some(description),
..self
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tlm_namespace() {
let ns = TlmNamespace::default();
assert_eq!(0, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!(String::from(""), ns.prefix);
assert_eq!(String::from(""), ns.uri);

let ns = TlmNamespace::new(
1,
String::from("foo"),
String::from("https://foo.example.com/"));
assert_eq!(1, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!("foo", ns.prefix);
assert_eq!("https://foo.example.com/", ns.uri);
assert_eq!(None, ns.description);

let ns = ns.describe(String::from("A namespace"));
assert_eq!(1, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!("A namespace", ns.description.as_ref().unwrap());

let ns2 = ns.clone();
assert_eq!(ns.oid, ns2.oid);
assert_eq!(ns.prefix, ns2.prefix);
assert_eq!(ns.uri, ns2.uri);
assert_eq!(ns.description.as_ref(), ns2.description.as_ref());
}
}
84 changes: 4 additions & 80 deletions packages/tlm-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,5 @@
type TlmOid = i32; // psql serial goes from 1 to 2147483647
mod core;
mod modeler;

const TLM_TYPE_NAMESPACE: TlmOid = 3;
// const TLM_TYPE_TYPE: TlmOid = 4;
// const TLM_TYPE_LINK: TlmOid = 53;

pub trait TlmObject : Clone + Eq + Default {
fn oid(&self) -> TlmOid;
fn tlm_type(&self) -> TlmOid;
}

#[derive(Debug,Clone,Hash,PartialEq,Eq,Default)]
pub struct TlmNamespace {
pub oid: TlmOid,
pub prefix: String,
pub uri: String,
pub description: Option<String>
}

impl TlmObject for TlmNamespace {
fn oid(&self) -> TlmOid {
self.oid
}

fn tlm_type(&self) -> TlmOid {
TLM_TYPE_NAMESPACE
}
}

impl TlmNamespace {
pub fn new(oid: TlmOid, prefix: String, uri: String) -> TlmNamespace {
TlmNamespace {
oid,
prefix,
uri,
..TlmNamespace::default()
}
}

pub fn describe(self, description: String) -> TlmNamespace {
TlmNamespace {
description: Some(description),
..self
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tlm_namespace() {
let ns = TlmNamespace::default();
assert_eq!(0, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!(String::from(""), ns.prefix);
assert_eq!(String::from(""), ns.uri);

let ns = TlmNamespace::new(
1,
String::from("foo"),
String::from("https://foo.example.com/"));
assert_eq!(1, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!("foo", ns.prefix);
assert_eq!("https://foo.example.com/", ns.uri);
assert_eq!(None, ns.description);

let ns = ns.describe(String::from("A namespace"));
assert_eq!(1, ns.oid());
assert_eq!(TLM_TYPE_NAMESPACE, ns.tlm_type());
assert_eq!("A namespace", ns.description.as_ref().unwrap());

let ns2 = ns.clone();
assert_eq!(ns.oid, ns2.oid);
assert_eq!(ns.prefix, ns2.prefix);
assert_eq!(ns.uri, ns2.uri);
assert_eq!(ns.description.as_ref(), ns2.description.as_ref());
}
}
pub use core::*;
pub use modeler::*;
42 changes: 42 additions & 0 deletions packages/tlm-rust/src/modeler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
mod namespace;
pub use namespace::*;

#[derive(Debug,Clone,Default)]
pub struct Modeler {
pub namespace_model: NamespaceModel,
}

impl Modeler {
pub fn new() -> Modeler {
Modeler {
namespace_model: NamespaceModel::new()
}
}

pub fn initialize(&mut self) {
self.namespace_model.initialize();
}

pub fn add_namespace(&mut self, prefix: String, uri: String, description: Option<String>) {
self.namespace_model.add_namespace(prefix, uri, description);
}

pub fn set_active_namespace(&mut self, ns: Option<String>) {
if let Some(ns) = ns {
self.namespace_model.set_active_namespace(ns);
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn modeler() {
let mut m = Modeler::new();
m.add_namespace(String::from("foo"), String::from("https://foo.example.com/"), None);
assert_eq!(1, m.namespace_model.namespaces.len());
assert_eq!("foo", m.namespace_model.namespaces[0].prefix);
}
}
Loading