croncat_mod_generic/
types.rs

1use 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/// Query given module contract with a message
17#[cw_serde]
18pub struct CroncatQuery {
19    /// This is address of the queried module contract.
20    /// For the addr can use one of our croncat-mod-* contracts, or custom contracts
21    pub contract_addr: String,
22    pub msg: Binary,
23    /// For queries with `check_result`: query return value should be formatted as a:
24    /// [`QueryResponse`](mod_sdk::types::QueryResponse)
25    pub check_result: bool,
26}
27
28/// Query given module contract with a message
29#[cw_serde]
30pub enum CosmosQuery<T = WasmQuery> {
31    // For optionally checking results, esp for modules
32    Croncat(CroncatQuery),
33
34    // For covering native wasm query cases (Smart, Raw)
35    Wasm(T),
36}
37
38// TODO Implement Serialize Deserialize https://github.com/CosmWasm/serde-json-wasm/issues/43
39#[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    /// Find the value by the "key" path
68    pub fn find_value<'a>(&self, val: &'a mut Value) -> StdResult<&'a mut Value> {
69        // If empty pointer, return the entirety
70        if self.0.is_empty() {
71            return Ok(val);
72        }
73
74        // Follow the path of the pointer
75        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}