Skip to content

Commit

Permalink
style(cognitarium): make linters happy
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 1, 2023
1 parent c10cf6a commit 3ebeefe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
14 changes: 6 additions & 8 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod tests {
use super::*;
use crate::error::StoreError;
use crate::msg::ExecuteMsg::InsertData;
use crate::msg::{DataInput, StoreLimitsInput, StoreLimitsInputBuilder};
use crate::msg::{StoreLimitsInput, StoreLimitsInputBuilder};
use crate::state;
use crate::state::{namespaces, triples, Namespace};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
Expand Down Expand Up @@ -240,9 +240,7 @@ mod tests {
.max_triple_count(30u128)
.build()
.unwrap(),
Some(ContractError::from(StoreError::MaxTriplesLimitExceeded(
30u128.into(),
))),
Some(ContractError::from(StoreError::TripleCount(30u128.into()))),
),
(
StoreLimitsInputBuilder::default()
Expand All @@ -256,7 +254,7 @@ mod tests {
.max_byte_size(50u128)
.build()
.unwrap(),
Some(ContractError::from(StoreError::MaxByteSize(50u128.into()))),
Some(ContractError::from(StoreError::ByteSize(50u128.into()))),
),
(
StoreLimitsInputBuilder::default()
Expand All @@ -270,7 +268,7 @@ mod tests {
.max_insert_data_byte_size(500u128)
.build()
.unwrap(),
Some(ContractError::from(StoreError::MaxInsertDataByteSize(
Some(ContractError::from(StoreError::InsertDataByteSize(
500u128.into(),
))),
),
Expand All @@ -286,7 +284,7 @@ mod tests {
.max_triple_byte_size(150u128)
.build()
.unwrap(),
Some(ContractError::from(StoreError::MaxTripleByteSize(
Some(ContractError::from(StoreError::TripleByteSize(
176u128.into(),
150u128.into(),
))),
Expand All @@ -303,7 +301,7 @@ mod tests {
.max_insert_data_triple_count(30u128)
.build()
.unwrap(),
Some(ContractError::from(StoreError::MaxInsertDataTripleCount(
Some(ContractError::from(StoreError::InsertDataTripleCount(
30u128.into(),
))),
),
Expand Down
22 changes: 8 additions & 14 deletions contracts/okp4-cognitarium/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,38 @@ impl From<TurtleError> for ContractError {
#[derive(Error, Debug, PartialEq)]
pub enum StoreError {
#[error("Maximum triples number exceeded: {0}")]
MaxTriplesLimitExceeded(Uint128),
TripleCount(Uint128),

#[error("Maximum byte size exceeded: {0}")]
MaxByteSize(Uint128),
ByteSize(Uint128),

#[error("Maximum triple byte size exceeded: {0} / {1}")]
MaxTripleByteSize(Uint128, Uint128),

#[error("Maximum query limit exceeded: {0} / {1}")]
MaxQueryLimit(Uint128, Uint128),

#[error("Maximum query variable count exceeded: {0} / {1}")]
MaxQueryVariableCount(Uint128, Uint128),
TripleByteSize(Uint128, Uint128),

#[error("Maximum insert byte size exceeded: {0}")]
MaxInsertDataByteSize(Uint128),
InsertDataByteSize(Uint128),

#[error("Maximum insert triple count exceeded: {0}")]
MaxInsertDataTripleCount(Uint128),
InsertDataTripleCount(Uint128),
}

#[derive(Error, Debug, PartialEq)]
pub enum RDFParseError {
#[error("Error parsing XML RDF: {0}")]
XML(String),
Xml(String),

#[error("Error parsing Turtle RDF: {0}")]
Turtle(String),
}

impl From<RdfXmlError> for RDFParseError {
fn from(value: RdfXmlError) -> Self {
RDFParseError::XML(value.to_string())
RDFParseError::Xml(value.to_string())
}
}

impl From<TurtleError> for RDFParseError {
fn from(value: TurtleError) -> Self {
RDFParseError::XML(value.to_string())
RDFParseError::Xml(value.to_string())
}
}
4 changes: 2 additions & 2 deletions contracts/okp4-cognitarium/src/state/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl KeyDeserialize for Subject {
fn from_vec(mut value: Vec<u8>) -> StdResult<Self::Output> {
let val = value.split_off(3);
match val[2] {
b'n' => Node::from_vec(value).map(|n| Subject::Named(n)),
b'b' => String::from_vec(value).map(|n| Subject::Blank(n)),
b'n' => Node::from_vec(value).map(Subject::Named),
b'b' => String::from_vec(value).map(Subject::Blank),
_ => Err(StdError::generic_err("Could not deserialize subject key")),
}
}
Expand Down
18 changes: 8 additions & 10 deletions contracts/okp4-cognitarium/src/state/storer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,32 @@ impl<'a> TripleStorer<'a> {
pub fn store_triple(&mut self, t: model::Triple) -> Result<(), ContractError> {
self.store.stat.triple_count += Uint128::one();
if self.store.stat.triple_count > self.store.limits.max_triple_count {
Err(StoreError::MaxTriplesLimitExceeded(
self.store.limits.max_triple_count,
))?
Err(StoreError::TripleCount(self.store.limits.max_triple_count))?
}
if self.store.stat.triple_count - self.initial_triple_count
> self.store.limits.max_insert_data_triple_count
{
Err(StoreError::MaxInsertDataTripleCount(
Err(StoreError::InsertDataTripleCount(
self.store.limits.max_insert_data_triple_count,
))?
}

let t_size = Uint128::from(Self::triple_size(t) as u128);
if t_size > self.store.limits.max_triple_byte_size {
Err(StoreError::MaxTripleByteSize(
Err(StoreError::TripleByteSize(
t_size,
self.store.limits.max_triple_byte_size,
))?
}

self.store.stat.byte_size += t_size;
if self.store.stat.byte_size > self.store.limits.max_byte_size {
Err(StoreError::MaxByteSize(self.store.limits.max_byte_size))?
Err(StoreError::ByteSize(self.store.limits.max_byte_size))?
}
if self.store.stat.byte_size - self.initial_byte_size
> self.store.limits.max_insert_data_byte_size
{
Err(StoreError::MaxInsertDataByteSize(
Err(StoreError::InsertDataByteSize(
self.store.limits.max_insert_data_byte_size,
))?
}
Expand Down Expand Up @@ -140,7 +138,7 @@ impl<'a> TripleStorer<'a> {

fn subject(&mut self, subject: model::Subject) -> StdResult<Subject> {
match subject {
model::Subject::NamedNode(node) => self.node(node).map(|n| Subject::Named(n)),
model::Subject::NamedNode(node) => self.node(node).map(Subject::Named),
model::Subject::BlankNode(node) => Ok(Subject::Blank(node.id.to_string())),
_ => Err(StdError::generic_err("RDF star syntax unsupported")),
}
Expand All @@ -157,8 +155,8 @@ impl<'a> TripleStorer<'a> {
fn object(&mut self, object: model::Term) -> StdResult<Object> {
match object {
Term::BlankNode(node) => Ok(Object::Blank(node.id.to_string())),
Term::NamedNode(node) => self.node(node).map(|n| Object::Named(n)),
Term::Literal(literal) => self.literal(literal).map(|l| Object::Literal(l)),
Term::NamedNode(node) => self.node(node).map(Object::Named),
Term::Literal(literal) => self.literal(literal).map(Object::Literal),
_ => Err(StdError::generic_err("RDF star syntax unsupported")),
}
}
Expand Down
7 changes: 4 additions & 3 deletions contracts/okp4-cognitarium/src/state/triples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use blake3::Hash;
use cw_storage_plus::{Index, IndexList, IndexedMap, MultiIndex};
use serde::{Deserialize, Serialize};

pub type TriplePK<'a> = (&'a [u8], Predicate, Subject);

pub struct TripleIndexes<'a> {
subject_and_predicate:
MultiIndex<'a, (Subject, Predicate), Triple, (&'a [u8], Predicate, Subject)>,
subject_and_predicate: MultiIndex<'a, (Subject, Predicate), Triple, TriplePK<'a>>,
}

impl IndexList<Triple> for TripleIndexes<'_> {
Expand All @@ -13,7 +14,7 @@ impl IndexList<Triple> for TripleIndexes<'_> {
}
}

pub fn triples<'a>() -> IndexedMap<'a, (&'a [u8], Predicate, Subject), Triple, TripleIndexes<'a>> {
pub fn triples<'a>() -> IndexedMap<'a, TriplePK<'a>, Triple, TripleIndexes<'a>> {
IndexedMap::new(
"TRIPLE",
TripleIndexes {
Expand Down

0 comments on commit 3ebeefe

Please sign in to comment.