use std::convert::TryFrom;
use crate::chain::ergo_box::box_value::BoxValue;
#[cfg(feature = "json")]
#[serde_with::serde_as]
#[derive(serde::Serialize, serde::Deserialize)]
pub(crate) struct BoxValueJson(
#[serde_as(as = "serde_with::PickFirst<(_, serde_with::DisplayFromStr)>")]
pub(crate) serde_json::Number,
);
impl TryFrom<BoxValueJson> for BoxValue {
type Error = String;
fn try_from(value: BoxValueJson) -> Result<Self, Self::Error> {
if let Some(n) = value.0.as_u64() {
Ok(BoxValue(n))
} else {
Err(String::from("can't convert `BoxValueJson` into `BoxValue`"))
}
}
}
impl From<BoxValue> for BoxValueJson {
fn from(value: BoxValue) -> Self {
BoxValueJson(serde_json::Number::from(value.0))
}
}