use crate::commands::contract_view;
use crate::contracts::RetrievedContract;
use crate::render::ContractView;
const MAX_READ_CLAIMS: usize = 12;
pub(super) const REASON_PRIVACY: &str = "database context is disabled for this provider";
pub(super) const REASON_NO_IDENTITY: &str =
"no profile identity is associated with this connection";
pub(super) const REASON_STORE: &str = "local contract store is unavailable";
pub(super) const REASON_NO_MATCH: &str = "no contract matches the given terms";
pub(super) const REASON_NO_CONTRACT: &str = "no contract is stored for this object";
pub(super) const REASON_STALE: &str = "every matching contract is stale; refresh the schema";
fn to_json(view: &ContractView) -> serde_json::Value {
serde_json::to_value(view).expect("ContractView serializes to JSON")
}
pub(super) fn contract_payload(
contract: &RetrievedContract,
profile_name: &str,
) -> serde_json::Value {
to_json(&contract_view(contract, profile_name))
}
pub(super) fn read_payload(contract: &RetrievedContract, profile_name: &str) -> serde_json::Value {
let mut view = contract_view(contract, profile_name);
if view.claims.len() > MAX_READ_CLAIMS {
view.claims.truncate(MAX_READ_CLAIMS);
view.truncated = true;
}
to_json(&view)
}
pub(super) fn empty_for(name: &str) -> fn(&str) -> serde_json::Value {
match name {
"contract_read" => empty_read,
_ => empty_search,
}
}
pub(super) fn contracts(contracts: Vec<serde_json::Value>) -> serde_json::Value {
serde_json::json!({ "contracts": contracts })
}
pub(super) fn contract(payload: serde_json::Value) -> serde_json::Value {
serde_json::json!({ "contract": payload })
}
fn empty_search(reason: &str) -> serde_json::Value {
serde_json::json!({ "contracts": [], "reason": reason })
}
fn empty_read(reason: &str) -> serde_json::Value {
serde_json::json!({ "reason": reason })
}