1use serde::{Deserialize, Serialize};
2use serde_json::Value as JsonValue;
3
4use crate::error::Error;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(untagged)]
8pub enum Params {
9 None,
10 Array(Vec<JsonValue>),
11 Map(serde_json::Map<String, JsonValue>),
12}
13
14impl Params {
15 pub fn parse<D: serde::de::DeserializeOwned>(self) -> Result<D, Error> {
16 let value: JsonValue = self.into();
17 serde_json::from_value(value).map_err(Error::InvalidParams)
18 }
19
20 pub fn to_value(&self) -> JsonValue {
21 match self {
22 Params::None => JsonValue::Null,
23 Params::Array(a) => JsonValue::Array(a.clone()),
24 Params::Map(a) => JsonValue::Object(a.clone()),
25 }
26 }
27}
28
29impl From<Params> for JsonValue {
30 fn from(params: Params) -> Self {
31 match params {
32 Params::None => JsonValue::Null,
33 Params::Array(vec) => JsonValue::Array(vec),
34 Params::Map(map) => JsonValue::Object(map),
35 }
36 }
37}