use crate::{Error, Result};
use serde::de::DeserializeOwned;
use serde_json::Value;
pub trait IntoParams: DeserializeOwned + Send {
fn into_params(value: Option<Value>) -> Result<Self> {
match value {
Some(value) => Ok(serde_json::from_value(value).map_err(Error::ParamsParsing)?),
None => Err(Error::ParamsMissingButRequested),
}
}
}
pub trait IntoDefaultRpcParams: DeserializeOwned + Send + Default {}
impl<P> IntoParams for P
where
P: IntoDefaultRpcParams,
{
fn into_params(value: Option<Value>) -> Result<Self> {
match value {
Some(value) => Ok(serde_json::from_value(value).map_err(Error::ParamsParsing)?),
None => Ok(Self::default()),
}
}
}
impl<D> IntoParams for Option<D>
where
D: DeserializeOwned + Send,
D: IntoParams,
{
fn into_params(value: Option<Value>) -> Result<Self> {
let value = value
.map(|v| serde_json::from_value(v))
.transpose()
.map_err(Error::ParamsParsing)?;
Ok(value)
}
}
impl IntoParams for Value {}