use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize};
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Id {
Number(i64),
String(String),
}
impl From<i64> for Id {
fn from(value: i64) -> Self {
Id::Number(value)
}
}
impl From<String> for Id {
fn from(value: String) -> Self {
Id::String(value)
}
}
impl From<&Id> for jsonrpc_lite::Id {
fn from(rpc_id: &Id) -> Self {
match rpc_id {
Id::String(string_id) => jsonrpc_lite::Id::Str(string_id.clone()),
Id::Number(number_id) => jsonrpc_lite::Id::Num(*number_id),
}
}
}
impl Display for Id {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Id::Number(number) => write!(formatter, "{}", number),
Id::String(string) => write!(formatter, "{}", string),
}
}
}