use serde::Serialize;
use serde_json::{Map, Value};
use crate::error::ClientError;
#[derive(Debug, Default)]
pub struct Params {
map: Map<String, Value>,
error: Option<ClientError>,
}
impl Params {
pub fn new() -> Self {
Self::default()
}
pub fn param<V: Serialize>(mut self, name: &'static str, value: V) -> Self {
if self.error.is_none() {
match serde_json::to_value(value) {
Ok(json) => {
self.map.insert(name.to_string(), json);
}
Err(e) => {
self.error = Some(ClientError::Encode {
param: name,
message: e.to_string(),
})
}
}
}
self
}
pub fn optional<V: Serialize>(self, name: &'static str, value: Option<V>) -> Self {
match value {
Some(value) => self.param(name, value),
None => self,
}
}
pub fn build(self) -> Result<Value, ClientError> {
match self.error {
Some(e) => Err(e),
None => Ok(Value::Object(self.map)),
}
}
}