croncat_mod_generic/
types.rs1use crate::value_ordering::ValueOrdering;
2use cosmwasm_schema::cw_serde;
3use cosmwasm_std::{Binary, StdError, StdResult, WasmQuery};
4use serde_cw_value::Value;
5
6#[cw_serde]
7pub struct GenericQuery {
8 pub contract_addr: String,
9 pub msg: Binary,
10 pub path_to_value: PathToValue,
11
12 pub ordering: ValueOrdering,
13 pub value: Binary,
14}
15
16#[cw_serde]
18pub struct CroncatQuery {
19 pub contract_addr: String,
22 pub msg: Binary,
23 pub check_result: bool,
26}
27
28#[cw_serde]
30pub enum CosmosQuery<T = WasmQuery> {
31 Croncat(CroncatQuery),
33
34 Wasm(T),
36}
37
38#[cw_serde]
40pub enum ValueIndex {
41 Key(String),
42 Index(u64),
43}
44
45impl From<u64> for ValueIndex {
46 fn from(i: u64) -> Self {
47 Self::Index(i)
48 }
49}
50
51impl From<String> for ValueIndex {
52 fn from(s: String) -> Self {
53 Self::Key(s)
54 }
55}
56
57#[cw_serde]
58pub struct PathToValue(pub Vec<ValueIndex>);
59
60impl From<Vec<ValueIndex>> for PathToValue {
61 fn from(path: Vec<ValueIndex>) -> Self {
62 PathToValue(path)
63 }
64}
65
66impl PathToValue {
67 pub fn find_value<'a>(&self, val: &'a mut Value) -> StdResult<&'a mut Value> {
69 if self.0.is_empty() {
71 return Ok(val);
72 }
73
74 let mut current_val = val;
76 for get in self.0.iter() {
77 match get {
78 ValueIndex::Key(s) => {
79 if let Value::Map(map) = current_val {
80 current_val = map
81 .get_mut(&Value::String(s.clone()))
82 .ok_or_else(|| StdError::generic_err("Invalid key for value"))?;
83 } else {
84 return Err(StdError::generic_err("Failed to get map from this value"));
85 }
86 }
87 ValueIndex::Index(n) => {
88 if let Value::Seq(seq) = current_val {
89 current_val = seq
90 .get_mut(*n as usize)
91 .ok_or_else(|| StdError::generic_err("Invalid index for value"))?;
92 } else {
93 return Err(StdError::generic_err(
94 "Failed to get sequence from this value",
95 ));
96 }
97 }
98 }
99 }
100 Ok(current_val)
101 }
102}