From a38aa79752c4e1162eff5fd3a23c9c65c6612ced Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 17 Apr 2023 13:43:11 +0200 Subject: [PATCH] feat(cognitarium): design triples state --- contracts/okp4-cognitarium/src/state/mod.rs | 2 + .../okp4-cognitarium/src/state/triples.rs | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 contracts/okp4-cognitarium/src/state/triples.rs diff --git a/contracts/okp4-cognitarium/src/state/mod.rs b/contracts/okp4-cognitarium/src/state/mod.rs index fca730d2..779733ec 100644 --- a/contracts/okp4-cognitarium/src/state/mod.rs +++ b/contracts/okp4-cognitarium/src/state/mod.rs @@ -1,3 +1,5 @@ mod store; +mod triples; pub use store::*; +pub use triples::*; diff --git a/contracts/okp4-cognitarium/src/state/triples.rs b/contracts/okp4-cognitarium/src/state/triples.rs new file mode 100644 index 00000000..6e589cb7 --- /dev/null +++ b/contracts/okp4-cognitarium/src/state/triples.rs @@ -0,0 +1,75 @@ +use cosmwasm_std::{StdError, Uint128}; +use cw_storage_plus::{Index, IndexList, IndexedMap, MultiIndex}; +use serde::{Deserialize, Serialize}; + +pub struct TripleIndexes<'a> { + subject_and_predicate: MultiIndex<'a, (Subject, Predicate), Triple, Uint128>, + predicate_and_object: MultiIndex<'a, (Predicate, Object), Triple, Uint128>, +} + +impl IndexList for TripleIndexes<'_> { + fn get_indexes(&self) -> Box> + '_> { + Box::new( + vec![ + &self.subject_and_predicate as &dyn Index, + &self.predicate_and_object, + ] + .into_iter(), + ) + } +} + +pub fn triples<'a>() -> IndexedMap<'a, u128, Triple, TripleIndexes<'a>> { + IndexedMap::new( + "TRIPLE", + TripleIndexes { + subject_and_predicate: MultiIndex::new( + |_pk, triple| (triple.subject.clone(), triple.predicate.clone()), + "TRIPLE", + "TRIPLE__SUBJECT_PREDICATE", + ), + predicate_and_object: MultiIndex::new( + |_pk, triple| (triple.predicate.clone(), triple.object.clone()), + "TRIPLE", + "TRIPLE__PREDICATE_OBJECT", + ), + }, + ) +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct Triple { + subject: Subject, + predicate: Predicate, + object: Object, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub enum Subject { + Named(Node), + Blank(BlankNode), +} + +pub type Predicate = Node; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub enum Object { + Named(Node), + Blank(BlankNode), + Literal(Literal), +} + +pub type BlankNode = String; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct Node { + pub namespace: String, + pub value: String, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub enum Literal { + Simple { value: String }, + I18NString { value: String, language: String }, + Typed { value: String, datatype: Node }, +}