parallel_disk_usage/json_data/
schema_version.rs

1#[cfg(feature = "json")]
2use derive_more::{Display, Error};
3#[cfg(feature = "json")]
4use serde::{Deserialize, Serialize};
5#[cfg(feature = "json")]
6use std::convert::TryFrom;
7
8/// Content of [`SchemaVersion`].
9pub const SCHEMA_VERSION: &str = "2024-11-02";
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"))]
15pub struct SchemaVersion;
16
17/// Error when trying to parse [`SchemaVersion`].
18#[cfg(feature = "json")]
19#[derive(Debug, Display, Error)]
20#[display("InvalidSchema: {input:?}: input schema is not {SCHEMA_VERSION:?}")]
21pub struct InvalidSchema {
22    /// The input string.
23    pub input: String,
24}
25
26#[cfg(feature = "json")]
27impl TryFrom<String> for SchemaVersion {
28    type Error = InvalidSchema;
29    fn try_from(input: String) -> Result<Self, Self::Error> {
30        if input == SCHEMA_VERSION {
31            Ok(SchemaVersion)
32        } else {
33            Err(InvalidSchema { input })
34        }
35    }
36}
37
38impl From<SchemaVersion> for &str {
39    fn from(_: SchemaVersion) -> Self {
40        SCHEMA_VERSION
41    }
42}