Skip to content

Commit 5250d81

Browse files
authored
Merge pull request #31 from KSXGitHub/root-full-path
Make JSON more useful
2 parents cac4efd + 1383195 commit 5250d81

File tree

11 files changed

+113
-117
lines changed

11 files changed

+113
-117
lines changed

src/app.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use sub::Sub;
44

55
use crate::{
66
args::{Args, Quantity},
7-
json_data::JsonData,
7+
json_data::{JsonData, UnitAndTree},
88
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
99
runtime_error::RuntimeError,
1010
size::{Bytes, Size},
@@ -59,9 +59,10 @@ impl App {
5959
} = self.args;
6060
let direction = Direction::from_top_down(top_down);
6161

62-
let json_data = stdin()
62+
let unit_and_tree = stdin()
6363
.pipe(serde_json::from_reader::<_, JsonData>)
64-
.map_err(RuntimeError::DeserializationFailure)?;
64+
.map_err(RuntimeError::DeserializationFailure)?
65+
.unit_and_tree;
6566

6667
macro_rules! visualize {
6768
($reflection:expr, $bytes_format: expr) => {{
@@ -79,9 +80,9 @@ impl App {
7980
}};
8081
}
8182

82-
let visualization = match json_data {
83-
JsonData::Bytes(reflection) => visualize!(reflection, bytes_format),
84-
JsonData::Blocks(reflection) => visualize!(reflection, ()),
83+
let visualization = match unit_and_tree {
84+
UnitAndTree::Bytes(reflection) => visualize!(reflection, bytes_format),
85+
UnitAndTree::Blocks(reflection) => visualize!(reflection, ()),
8586
};
8687

8788
print!("{}", visualization); // it already ends with "\n", println! isn't needed here.

src/app/sub.rs

Lines changed: 9 additions & 4 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,
5+
json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree},
66
os_string_display::OsStringDisplay,
77
reporter::ParallelReporter,
88
runtime_error::RuntimeError,
@@ -19,7 +19,7 @@ where
1919
Data: Size + Into<u64> + Serialize + Send + Sync,
2020
Report: ParallelReporter<Data> + Sync,
2121
GetData: Fn(&Metadata) -> Data + Copy + Sync,
22-
DataTreeReflection<String, Data>: Into<JsonData>,
22+
DataTreeReflection<String, Data>: Into<UnitAndTree>,
2323
{
2424
/// List of files and/or directories.
2525
pub files: Vec<PathBuf>,
@@ -48,7 +48,7 @@ where
4848
Data: Size + Into<u64> + Serialize + Send + Sync,
4949
Report: ParallelReporter<Data> + Sync,
5050
GetData: Fn(&Metadata) -> Data + Copy + Sync,
51-
DataTreeReflection<String, Data>: Into<JsonData>,
51+
DataTreeReflection<String, Data>: Into<UnitAndTree>,
5252
{
5353
/// Run the sub program.
5454
pub fn run(self) -> Result<(), RuntimeError> {
@@ -116,11 +116,16 @@ where
116116
};
117117

118118
if json_output {
119-
let json_data: JsonData = data_tree
119+
let unit_and_tree: UnitAndTree = data_tree
120120
.into_reflection() // I really want to use std::mem::transmute here but can't.
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 {
125+
schema_version: SchemaVersion,
126+
binary_version: Some(BinaryVersion::current()),
127+
unit_and_tree,
128+
};
124129
return serde_json::to_writer(stdout(), &json_data)
125130
.map_err(RuntimeError::SerializationFailure);
126131
}

src/data_tree/reflection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use serde::{Deserialize, Serialize};
2727
/// **Serialization and deserialization:** Requires enabling the `json` feature to enable `serde`.
2828
#[derive(Debug, Clone, PartialEq, Eq)]
2929
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
30+
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
3031
pub struct Reflection<Name, Data: Size> {
3132
/// Name of the tree.
3233
pub name: Name,

src/fs_tree_builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::{
44
reporter::{error_report::Operation::*, ErrorReport, Event, Reporter},
55
size::Size,
66
tree_builder::{Info, TreeBuilder},
7-
utils::path_name,
87
};
98
use pipe_trait::Pipe;
109
use std::{
@@ -43,7 +42,7 @@ where
4342
} = builder;
4443

4544
TreeBuilder::<PathBuf, OsStringDisplay, Data, _, _> {
46-
name: path_name(&root),
45+
name: OsStringDisplay::os_string_from(&root),
4746

4847
path: root,
4948

src/json_data.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
pub mod binary_version;
2+
pub mod schema_version;
3+
4+
pub use binary_version::BinaryVersion;
5+
pub use schema_version::SchemaVersion;
6+
17
use crate::{
28
data_tree::Reflection,
39
size::{Blocks, Bytes},
@@ -7,14 +13,30 @@ use derive_more::{From, TryInto};
713
#[cfg(feature = "json")]
814
use serde::{Deserialize, Serialize};
915

10-
/// Output of the program with `--json-output` flag as well as
11-
/// input of the program with `--json-input` flag.
16+
/// The `"unit"` field and the `"tree"` field of [`JsonData`].
1217
#[derive(Debug, Clone, From, TryInto)]
1318
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
1419
#[cfg_attr(feature = "json", serde(tag = "unit", content = "tree"))]
15-
pub enum JsonData {
20+
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
21+
pub enum UnitAndTree {
1622
/// Tree where data is [bytes](Bytes).
1723
Bytes(Reflection<String, Bytes>),
1824
/// Tree where data is [blocks](Blocks).
1925
Blocks(Reflection<String, Blocks>),
2026
}
27+
28+
/// Output of the program with `--json-output` flag as well as
29+
/// input of the program with `--json-input` flag.
30+
#[derive(Debug, Clone)]
31+
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
32+
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
33+
pub struct JsonData {
34+
/// The `"schema-version"` field.
35+
pub schema_version: SchemaVersion,
36+
/// The `"pdu"` field.
37+
#[cfg_attr(feature = "json", serde(rename = "pdu"))]
38+
pub binary_version: Option<BinaryVersion>,
39+
/// The `"unit"` field and the `"tree"` field.
40+
#[cfg_attr(feature = "json", serde(flatten))]
41+
pub unit_and_tree: UnitAndTree,
42+
}

src/json_data/binary_version.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use derive_more::{AsMut, AsRef, From, FromStr, Into};
2+
3+
#[cfg(feature = "json")]
4+
use serde::{Deserialize, Serialize};
5+
6+
/// Version of the current `pdu` program.
7+
pub const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
8+
9+
/// Version of the `pdu` program that created the input JSON.
10+
#[derive(Debug, Clone, PartialEq, Eq, AsMut, AsRef, From, FromStr, Into)]
11+
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
12+
pub struct BinaryVersion(String);
13+
14+
impl BinaryVersion {
15+
/// Get version of the current `pdu` program as a `BinaryVersion`.
16+
pub fn current() -> Self {
17+
CURRENT_VERSION.to_string().into()
18+
}
19+
}

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+
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![deny(warnings)]
22

3-
mod utils;
4-
53
#[cfg(feature = "json")]
64
pub use serde;
75
#[cfg(feature = "json")]

src/utils.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/utils/test_path_name.rs

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)