json5format/options.rs
1// Copyright (c) 2020 Google LLC All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#![deny(missing_docs)]
6use {std::collections::HashMap, std::collections::HashSet, std::hash::Hash, std::hash::Hasher};
7
8/// Options that can be applied to specific objects or arrays in the target JSON5 schema, through
9/// [FormatOptions.options_by_path](struct.FormatOptions.html#structfield.options_by_path).
10/// Each option can be set at most once per unique path.
11#[derive(Clone, Debug)]
12pub enum PathOption {
13 /// For matched paths, overrides the FormatOption.trailing_comma provided default.
14 TrailingCommas(bool),
15
16 /// For matched paths, overrides the FormatOption.collapse_container_of_one provided default.
17 CollapseContainersOfOne(bool),
18
19 /// For matched paths, overrides the FormatOption.sort_array_items provided default.
20 SortArrayItems(bool),
21
22 /// Contains a vector of property names. When formatting an object matching the path in
23 /// `FormatOptions.options_by_path` a specified path, properties of the object will be sorted
24 /// to match the given order. Any properties not in this list will retain their original order,
25 /// and placed after the sorted properties.
26 PropertyNameOrder(Vec<&'static str>),
27}
28
29impl PartialEq for PathOption {
30 fn eq(&self, other: &Self) -> bool {
31 use PathOption::*;
32 match (self, other) {
33 (&TrailingCommas(..), &TrailingCommas(..)) => true,
34 (&CollapseContainersOfOne(..), &CollapseContainersOfOne(..)) => true,
35 (&SortArrayItems(..), &SortArrayItems(..)) => true,
36 (&PropertyNameOrder(..), &PropertyNameOrder(..)) => true,
37 _ => false,
38 }
39 }
40}
41
42impl Eq for PathOption {}
43
44impl Hash for PathOption {
45 fn hash<H: Hasher>(&self, state: &mut H) {
46 use PathOption::*;
47 state.write_u32(match self {
48 TrailingCommas(..) => 1,
49 CollapseContainersOfOne(..) => 2,
50 SortArrayItems(..) => 3,
51 PropertyNameOrder(..) => 4,
52 });
53 state.finish();
54 }
55}
56
57/// Options that change the style of the formatted JSON5 output.
58#[derive(Clone, Debug)]
59pub struct FormatOptions {
60 /// Indent the content of an object or array by this many spaces.
61 pub indent_by: usize,
62
63 /// Add a trailing comma after the last element in an array or object.
64 pub trailing_commas: bool,
65
66 /// If an array or object has only one item (or is empty), and no internal comments, collapse
67 /// the array or object to a single line.
68 pub collapse_containers_of_one: bool,
69
70 /// If true, sort array primitive values lexicographically. Be aware that the order may not
71 /// matter in some use cases, but can be very important in others. Consider setting this
72 /// option for specific property paths only, and otherwise use the default (false).
73 pub sort_array_items: bool,
74
75 /// A set of "paths", to identify elements of the JSON structure, mapped to a set of one or
76 /// more [PathOption](enum.PathOption.html) settings.
77 pub options_by_path: HashMap<&'static str, HashSet<PathOption>>,
78}
79
80impl Default for FormatOptions {
81 fn default() -> Self {
82 FormatOptions {
83 indent_by: 4,
84 trailing_commas: true,
85 collapse_containers_of_one: false,
86 sort_array_items: false,
87 options_by_path: HashMap::new(),
88 }
89 }
90}