tendermint_rpc/
version.rs1use core::{
4 fmt::{self, Display},
5 str::FromStr,
6};
7
8use serde::{Deserialize, Serialize};
9
10use super::error::Error;
11use crate::prelude::*;
12
13const SUPPORTED_VERSION: &str = "2.0";
15
16#[derive(Clone, Debug, Deserialize, Eq, PartialEq, PartialOrd, Ord, Serialize)]
19pub struct Version(String);
20
21impl Version {
22 pub fn current() -> Self {
24 Version(SUPPORTED_VERSION.to_owned())
25 }
26
27 pub fn is_supported(&self) -> bool {
29 self.0.eq(SUPPORTED_VERSION)
30 }
31
32 pub fn ensure_supported(&self) -> Result<(), Error> {
34 if self.is_supported() {
35 Ok(())
36 } else {
37 Err(Error::unsupported_rpc_version(
38 self.0.to_string(),
39 SUPPORTED_VERSION.to_string(),
40 ))
41 }
42 }
43}
44
45impl Display for Version {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(f, "{}", self.0)
48 }
49}
50
51impl FromStr for Version {
52 type Err = Error;
53
54 fn from_str(s: &str) -> Result<Self, Error> {
55 Ok(Version(s.to_owned()))
56 }
57}