|
| 1 | +#[cfg(feature = "json")] |
| 2 | +use derive_more::Display; |
| 3 | +#[cfg(feature = "json")] |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +#[cfg(feature = "json")] |
| 6 | +use std::convert::TryFrom; |
| 7 | + |
| 8 | +/// Content of [`SchemaVersion`]. |
| 9 | +pub const SCHEMA_VERSION: &str = "2021-06-05"; |
| 10 | + |
| 11 | +/// Verifying schema version. |
| 12 | +#[derive(Debug, Clone, Copy)] |
| 13 | +#[cfg_attr(feature = "json", derive(Deserialize, Serialize))] |
| 14 | +#[cfg_attr(feature = "json", serde(try_from = "String", into = "&str"))] |
| 15 | +pub struct SchemaVersion; |
| 16 | + |
| 17 | +/// Error when trying to parse [`SchemaVersion`]. |
| 18 | +#[cfg(feature = "json")] |
| 19 | +#[derive(Debug, Display)] |
| 20 | +#[display( |
| 21 | + fmt = "InvalidSchema: {:?}: input schema is not {:?}", |
| 22 | + input, |
| 23 | + SCHEMA_VERSION |
| 24 | +)] |
| 25 | +pub struct InvalidSchema { |
| 26 | + /// The input string. |
| 27 | + pub input: String, |
| 28 | +} |
| 29 | + |
| 30 | +#[cfg(feature = "json")] |
| 31 | +impl TryFrom<String> for SchemaVersion { |
| 32 | + type Error = InvalidSchema; |
| 33 | + fn try_from(input: String) -> Result<Self, Self::Error> { |
| 34 | + if input == SCHEMA_VERSION { |
| 35 | + Ok(SchemaVersion) |
| 36 | + } else { |
| 37 | + Err(InvalidSchema { input }) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<'a> From<SchemaVersion> for &'a str { |
| 43 | + fn from(_: SchemaVersion) -> Self { |
| 44 | + SCHEMA_VERSION |
| 45 | + } |
| 46 | +} |
0 commit comments