use core::{
fmt::{self, Display},
str::FromStr,
};
use serde::{Deserialize, Serialize};
use super::error::Error;
use crate::prelude::*;
const SUPPORTED_VERSION: &str = "2.0";
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, PartialOrd, Ord, Serialize)]
pub struct Version(String);
impl Version {
pub fn current() -> Self {
Version(SUPPORTED_VERSION.to_owned())
}
pub fn is_supported(&self) -> bool {
self.0.eq(SUPPORTED_VERSION)
}
pub fn ensure_supported(&self) -> Result<(), Error> {
if self.is_supported() {
Ok(())
} else {
Err(Error::unsupported_rpc_version(
self.0.to_string(),
SUPPORTED_VERSION.to_string(),
))
}
}
}
impl Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Ok(Version(s.to_owned()))
}
}