Skip to content

Commit

Permalink
feat(law-stone)!: match v7 logic module model
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 4, 2024
1 parent 32e114e commit c25128a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
8 changes: 4 additions & 4 deletions contracts/okp4-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,17 @@ mod tests {
height: 1,
gas_used: 1000,
answer: Some(Answer {
success: true,
error: None,
has_more: false,
variables: vec!["Files".to_string()],
results: vec![LogicResult {
error: None,
substitutions: vec![Substitution {
variable: "Files".to_string(),
expression: deps_name,
}],
}],
}),
user_output: None,
})
.into(),
)
Expand Down Expand Up @@ -459,17 +459,17 @@ mod tests {
height: 1,
gas_used: 1000,
answer: Some(Answer {
success: true,
error: None,
has_more: false,
variables: vec!["Foo".to_string()],
results: vec![LogicResult {
error: None,
substitutions: vec![Substitution {
variable: "Foo".to_string(),
expression: "bar".to_string(),
}],
}],
}),
user_output: None,
})
.into(),
),
Expand Down
6 changes: 6 additions & 0 deletions contracts/okp4-law-stone/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub enum LogicAskResponseError {
#[error("Could not parse term: {0}")]
Parse(TermParseError),

#[error("Substitution error: {0}")]
Substitution(String),

#[error("Unexpected response: {0}")]
Unexpected(String),

#[error("Invalid parsed term format.")]
UnexpectedTerm,
}
32 changes: 24 additions & 8 deletions contracts/okp4-law-stone/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{Event, StdError, StdResult};
use itertools::Itertools;
use okp4_logic_bindings::error::CosmwasmUriError;
use okp4_logic_bindings::uri::CosmwasmUri;
use okp4_logic_bindings::{AskResponse, Substitution, TermValue};
use okp4_logic_bindings::{AskResponse, TermValue};
use okp4_objectarium_client::ObjectRef;
use std::any::type_name;

Expand Down Expand Up @@ -46,13 +46,29 @@ pub fn ask_response_to_objects(
res: AskResponse,
variable: String,
) -> Result<Vec<ObjectRef>, ContractError> {
res.answer
let result = res
.answer
.map(|a| a.results)
.unwrap_or_default()
.iter()
.flat_map(|r: &okp4_logic_bindings::Result| r.substitutions.clone())
.into_iter()
.exactly_one()
.map_err(|_| {
ContractError::LogicAskResponse(LogicAskResponseError::Unexpected(
"expected exactly one result".to_string(),
))
})?;

if let Some(e) = result.error {
return Err(ContractError::LogicAskResponse(
LogicAskResponseError::Substitution(e),
));
}

result
.substitutions
.into_iter()
.filter(|s| s.variable == variable)
.map(|s: Substitution| {
.map(|s| {
s.parse_expression()
.map_err(|e| ContractError::LogicAskResponse(LogicAskResponseError::Parse(e)))
.and_then(term_as_vec)
Expand All @@ -71,7 +87,7 @@ pub fn ask_response_to_objects(
mod tests {
use super::*;
use okp4_logic_bindings::error::TermParseError;
use okp4_logic_bindings::Answer;
use okp4_logic_bindings::{Answer, Substitution};

#[test]
fn logic_to_objects() {
Expand Down Expand Up @@ -102,18 +118,18 @@ mod tests {
AskResponse {
answer: Some(Answer {
results: vec![okp4_logic_bindings::Result {
error: None,
substitutions: vec![Substitution {
variable: "X".to_string(),
expression: case.0,
}]
}],
has_more: false,
success: true,
variables: vec![],
error: None,
}),
height: 1,
gas_used: 1,
user_output: None,
},
"X".to_string()
),
Expand Down

0 comments on commit c25128a

Please sign in to comment.