forked from informalsystems/tendermint-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.rs
57 lines (46 loc) · 1.33 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! JSON-RPC versions
use core::{
fmt::{self, Display},
str::FromStr,
};
use serde::{Deserialize, Serialize};
use super::error::Error;
use crate::prelude::*;
/// Supported JSON-RPC version
const SUPPORTED_VERSION: &str = "2.0";
/// JSON-RPC version
// TODO(tarcieri): add restrictions/validations on these formats? Use an `enum`?
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, PartialOrd, Ord, Serialize)]
pub struct Version(String);
impl Version {
/// Get the currently supported JSON-RPC version
pub fn current() -> Self {
Version(SUPPORTED_VERSION.to_owned())
}
/// Is this JSON-RPC version supported?
pub fn is_supported(&self) -> bool {
self.0.eq(SUPPORTED_VERSION)
}
/// Ensure we have a supported version or return an error
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()))
}
}