use crate::{
commands::contract::invoke, config::token::ResolvedToken, get_spec, output::Format, rpc,
};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum, Default)]
pub enum OutputFormat {
#[default]
Text,
Json,
JsonFormatted,
}
impl From<OutputFormat> for Format {
fn from(value: OutputFormat) -> Self {
match value {
OutputFormat::Text => Format::Readable,
OutputFormat::Json => Format::Json,
OutputFormat::JsonFormatted => Format::JsonFormatted,
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(
"the Stellar Asset Contract {0} is not deployed on this network.\n\
Deploy it first with `stellar contract asset deploy --asset <ASSET>`, then retry."
)]
SacNotDeployed(String),
#[error("contract {0} was not found on this network")]
ContractNotFound(String),
}
impl Error {
#[must_use]
pub fn error_type(&self) -> &'static str {
match self {
Error::SacNotDeployed(_) => "sac_not_deployed",
Error::ContractNotFound(_) => "contract_not_found",
}
}
}
#[must_use]
pub fn not_deployed_error(token: &ResolvedToken, err: &invoke::Error) -> Option<Error> {
let invoke::Error::GetSpecError(get_spec::Error::Rpc(rpc::Error::NotFound(kind, _))) = err
else {
return None;
};
if kind != "Contract" {
return None;
}
let contract_id = token.contract_id;
Some(if token.is_sac() {
Error::SacNotDeployed(format!("{contract_id}"))
} else {
Error::ContractNotFound(format!("{contract_id}"))
})
}