generated from okp4/template-rust
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cognitarium): move query variables elements
- Loading branch information
Showing
3 changed files
with
108 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
mod engine; | ||
mod plan; | ||
mod plan_builder; | ||
mod variable; | ||
|
||
pub use engine::*; | ||
pub use plan::*; | ||
pub use plan_builder::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::msg::Value; | ||
use crate::state::{Literal, Object, Predicate, Subject}; | ||
use cosmwasm_std::{StdError, StdResult}; | ||
|
||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub enum ResolvedVariable { | ||
Subject(Subject), | ||
Predicate(Predicate), | ||
Object(Object), | ||
} | ||
|
||
impl ResolvedVariable { | ||
pub fn as_subject(&self) -> Option<Subject> { | ||
Some(match self { | ||
ResolvedVariable::Subject(s) => s.clone(), | ||
ResolvedVariable::Predicate(p) => Subject::Named(p.clone()), | ||
ResolvedVariable::Object(o) => match o { | ||
Object::Named(node) => Subject::Named(node.clone()), | ||
Object::Blank(node) => Subject::Blank(node.clone()), | ||
Object::Literal(_) => None?, | ||
}, | ||
}) | ||
} | ||
|
||
pub fn as_predicate(&self) -> Option<Predicate> { | ||
Some(match self { | ||
ResolvedVariable::Subject(s) => match s { | ||
Subject::Named(node) => node.clone(), | ||
Subject::Blank(_) => None?, | ||
}, | ||
ResolvedVariable::Predicate(p) => p.clone(), | ||
ResolvedVariable::Object(o) => match o { | ||
Object::Named(node) => node.clone(), | ||
Object::Blank(_) => None?, | ||
Object::Literal(_) => None?, | ||
}, | ||
}) | ||
} | ||
|
||
pub fn as_object(&self) -> Option<Object> { | ||
Some(match self { | ||
ResolvedVariable::Subject(s) => match s { | ||
Subject::Named(node) => Object::Named(node.clone()), | ||
Subject::Blank(node) => Object::Blank(node.clone()), | ||
}, | ||
ResolvedVariable::Predicate(p) => Object::Named(p.clone()), | ||
ResolvedVariable::Object(o) => o.clone(), | ||
}) | ||
} | ||
|
||
pub fn as_value<F>(&self, ns_fn: F) -> StdResult<Value> | ||
where | ||
F: n(u128) -> StdResult<String>, | ||
{ | ||
Err(StdError::generic_err("mescouilles")) | ||
} | ||
} | ||
|
||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub struct ResolvedVariables { | ||
variables: Vec<Option<ResolvedVariable>>, | ||
} | ||
|
||
impl ResolvedVariables { | ||
pub fn with_capacity(cap: usize) -> Self { | ||
let mut variables = Vec::with_capacity(cap); | ||
for i in 0..cap { | ||
variables.insert(i, None); | ||
} | ||
|
||
Self { variables } | ||
} | ||
|
||
/// Merge with another set of resolved variables, returns None if a variable is set on both side | ||
/// with different values. | ||
pub fn merge_with(&self, other: &Self) -> Option<Self> { | ||
let mut merged = other.variables.clone(); | ||
|
||
for (key, var) in self.variables.iter().enumerate() { | ||
if let Some(val) = var { | ||
match &other.variables[key] { | ||
Some(other_val) => { | ||
if val != other_val { | ||
return None; | ||
} | ||
} | ||
None => merged[key] = Some(val.clone()), | ||
} | ||
} | ||
} | ||
|
||
Some(Self { variables: merged }) | ||
} | ||
|
||
pub fn set(&mut self, index: usize, var: ResolvedVariable) { | ||
self.variables[index] = Some(var) | ||
} | ||
|
||
pub fn get(&self, index: usize) -> &Option<ResolvedVariable> { | ||
self.variables.get(index).unwrap_or(&None) | ||
} | ||
} |