cfxstore/json/
version.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17use super::Error;
18use serde::{
19    de::{Error as SerdeError, Visitor},
20    Deserialize, Deserializer, Serialize, Serializer,
21};
22use std::fmt;
23
24#[derive(Debug, PartialEq)]
25pub enum Version {
26    V3,
27}
28
29impl Serialize for Version {
30    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31    where S: Serializer {
32        match *self {
33            Version::V3 => serializer.serialize_u64(3),
34        }
35    }
36}
37
38impl<'a> Deserialize<'a> for Version {
39    fn deserialize<D>(deserializer: D) -> Result<Version, D::Error>
40    where D: Deserializer<'a> {
41        deserializer.deserialize_any(VersionVisitor)
42    }
43}
44
45struct VersionVisitor;
46
47impl<'a> Visitor<'a> for VersionVisitor {
48    type Value = Version;
49
50    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
51        write!(formatter, "a valid key version identifier")
52    }
53
54    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
55    where E: SerdeError {
56        match value {
57            3 => Ok(Version::V3),
58            _ => Err(SerdeError::custom(Error::UnsupportedVersion)),
59        }
60    }
61}