Skip to content

Commit 3243d2d

Browse files
committed
api+json: Create schema-version field
resolve #27
1 parent 0f4620b commit 3243d2d

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/app/sub.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
args::Fraction,
33
data_tree::{DataTree, DataTreeReflection},
44
fs_tree_builder::FsTreeBuilder,
5-
json_data::{JsonData, UnitAndTree},
5+
json_data::{JsonData, SchemaVersion, UnitAndTree},
66
os_string_display::OsStringDisplay,
77
reporter::ParallelReporter,
88
runtime_error::RuntimeError,
@@ -121,7 +121,10 @@ where
121121
.par_convert_names_to_utf8() // TODO: allow non-UTF8 somehow.
122122
.expect("convert all names from raw string to UTF-8")
123123
.into();
124-
let json_data = JsonData { unit_and_tree };
124+
let json_data = JsonData {
125+
schema_version: SchemaVersion,
126+
unit_and_tree,
127+
};
125128
return serde_json::to_writer(stdout(), &json_data)
126129
.map_err(RuntimeError::SerializationFailure);
127130
}

src/json_data.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
pub mod schema_version;
2+
3+
pub use schema_version::SchemaVersion;
4+
15
use crate::{
26
data_tree::Reflection,
37
size::{Blocks, Bytes},
@@ -25,6 +29,8 @@ pub enum UnitAndTree {
2529
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
2630
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
2731
pub struct JsonData {
32+
/// The `"schema-version"` field.
33+
pub schema_version: SchemaVersion,
2834
/// The `"unit"` field and the `"tree"` field.
2935
#[cfg_attr(feature = "json", serde(flatten))]
3036
pub unit_and_tree: UnitAndTree,

src/json_data/schema_version.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)