tendermint_rpc/
version.rs

1//! JSON-RPC versions
2
3use core::{
4    fmt::{self, Display},
5    str::FromStr,
6};
7
8use serde::{Deserialize, Serialize};
9
10use super::error::Error;
11use crate::prelude::*;
12
13/// Supported JSON-RPC version
14const SUPPORTED_VERSION: &str = "2.0";
15
16/// JSON-RPC version
17// TODO(tarcieri): add restrictions/validations on these formats? Use an `enum`?
18#[derive(Clone, Debug, Deserialize, Eq, PartialEq, PartialOrd, Ord, Serialize)]
19pub struct Version(String);
20
21impl Version {
22    /// Get the currently supported JSON-RPC version
23    pub fn current() -> Self {
24        Version(SUPPORTED_VERSION.to_owned())
25    }
26
27    /// Is this JSON-RPC version supported?
28    pub fn is_supported(&self) -> bool {
29        self.0.eq(SUPPORTED_VERSION)
30    }
31
32    /// Ensure we have a supported version or return an error
33    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}