annotate rust/hg-core/src/config/layer.rs @ 46435:2e2033081274

rust: replace trivial `impl From …` with `#[derive(derive_more::From)]` Crate docs: https://jeltef.github.io/derive_more/derive_more/from.html Differential Revision: https://phab.mercurial-scm.org/D9875
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 26 Jan 2021 20:05:37 +0100
parents 95d6f31e88db
children 0cb1b02228a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1 // layer.rs
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
2 //
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2020
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
4 // Valentin Gatien-Baron,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5 // Raphaël Gomès <rgomes@octobus.net>
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 //
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7 // This software may be used and distributed according to the terms of the
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
8 // GNU General Public License version 2 or any later version.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
10 use crate::utils::files::{
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
11 get_bytes_from_path, get_path_from_bytes, read_whole_file,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
12 };
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
13 use format_bytes::format_bytes;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
14 use lazy_static::lazy_static;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
15 use regex::bytes::Regex;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
16 use std::collections::HashMap;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
17 use std::io;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
18 use std::path::{Path, PathBuf};
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
19
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
20 lazy_static! {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
21 static ref SECTION_RE: Regex = make_regex(r"^\[([^\[]+)\]");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22 static ref ITEM_RE: Regex = make_regex(r"^([^=\s][^=]*?)\s*=\s*((.*\S)?)");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
23 /// Continuation whitespace
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
24 static ref CONT_RE: Regex = make_regex(r"^\s+(\S|\S.*\S)\s*$");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
25 static ref EMPTY_RE: Regex = make_regex(r"^(;|#|\s*$)");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
26 static ref COMMENT_RE: Regex = make_regex(r"^(;|#)");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
27 /// A directive that allows for removing previous entries
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
28 static ref UNSET_RE: Regex = make_regex(r"^%unset\s+(\S+)");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
29 /// A directive that allows for including other config files
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
30 static ref INCLUDE_RE: Regex = make_regex(r"^%include\s+(\S|\S.*\S)\s*$");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
31 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
32
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
33 /// All config values separated by layers of precedence.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
34 /// Each config source may be split in multiple layers if `%include` directives
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
35 /// are used.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
36 /// TODO detail the general precedence
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
37 #[derive(Clone)]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
38 pub struct ConfigLayer {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
39 /// Mapping of the sections to their items
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
40 sections: HashMap<Vec<u8>, ConfigItem>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
41 /// All sections (and their items/values) in a layer share the same origin
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
42 pub origin: ConfigOrigin,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
43 /// Whether this layer comes from a trusted user or group
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
44 pub trusted: bool,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
45 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
46
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
47 impl ConfigLayer {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
48 pub fn new(origin: ConfigOrigin) -> Self {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
49 ConfigLayer {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
50 sections: HashMap::new(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
51 trusted: true, // TODO check
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
52 origin,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
53 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
54 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
55
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
56 /// Add an entry to the config, overwriting the old one if already present.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
57 pub fn add(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
58 &mut self,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
59 section: Vec<u8>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
60 item: Vec<u8>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
61 value: Vec<u8>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
62 line: Option<usize>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
63 ) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
64 self.sections
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
65 .entry(section)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
66 .or_insert_with(|| HashMap::new())
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
67 .insert(item, ConfigValue { bytes: value, line });
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
68 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
69
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
70 /// Returns the config value in `<section>.<item>` if it exists
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
71 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&ConfigValue> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
72 Some(self.sections.get(section)?.get(item)?)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
73 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
74
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
75 pub fn is_empty(&self) -> bool {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
76 self.sections.is_empty()
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
77 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
78
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
79 /// Returns a `Vec` of layers in order of precedence (so, in read order),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
80 /// recursively parsing the `%include` directives if any.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
81 pub fn parse(src: &Path, data: &[u8]) -> Result<Vec<Self>, ConfigError> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
82 let mut layers = vec![];
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
83
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
84 // Discard byte order mark if any
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
85 let data = if data.starts_with(b"\xef\xbb\xbf") {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
86 &data[3..]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
87 } else {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
88 data
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
89 };
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
90
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
91 // TODO check if it's trusted
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
92 let mut current_layer = Self::new(ConfigOrigin::File(src.to_owned()));
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
93
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
94 let mut lines_iter =
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
95 data.split(|b| *b == b'\n').enumerate().peekable();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
96 let mut section = b"".to_vec();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
97
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
98 while let Some((index, bytes)) = lines_iter.next() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
99 if let Some(m) = INCLUDE_RE.captures(&bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
100 let filename_bytes = &m[1];
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
101 let filename_to_include = get_path_from_bytes(&filename_bytes);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
102 match read_include(&src, &filename_to_include) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
103 (include_src, Ok(data)) => {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
104 layers.push(current_layer);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
105 layers.extend(Self::parse(&include_src, &data)?);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
106 current_layer =
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
107 Self::new(ConfigOrigin::File(src.to_owned()));
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
108 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
109 (_, Err(e)) => {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
110 return Err(ConfigError::IncludeError {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
111 path: filename_to_include.to_owned(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
112 io_error: e,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
113 })
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
114 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
115 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
116 } else if let Some(_) = EMPTY_RE.captures(&bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
117 } else if let Some(m) = SECTION_RE.captures(&bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
118 section = m[1].to_vec();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
119 } else if let Some(m) = ITEM_RE.captures(&bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
120 let item = m[1].to_vec();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
121 let mut value = m[2].to_vec();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
122 loop {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
123 match lines_iter.peek() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
124 None => break,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
125 Some((_, v)) => {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
126 if let Some(_) = COMMENT_RE.captures(&v) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
127 } else if let Some(_) = CONT_RE.captures(&v) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
128 value.extend(b"\n");
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
129 value.extend(&m[1]);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
130 } else {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
131 break;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
132 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
133 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
134 };
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
135 lines_iter.next();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
136 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
137 current_layer.add(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
138 section.clone(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
139 item,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
140 value,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
141 Some(index + 1),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
142 );
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
143 } else if let Some(m) = UNSET_RE.captures(&bytes) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
144 if let Some(map) = current_layer.sections.get_mut(&section) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
145 map.remove(&m[1]);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
146 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
147 } else {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
148 return Err(ConfigError::Parse {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
149 origin: ConfigOrigin::File(src.to_owned()),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
150 line: Some(index + 1),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
151 bytes: bytes.to_owned(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
152 });
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
153 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
154 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
155 if !current_layer.is_empty() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
156 layers.push(current_layer);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
157 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
158 Ok(layers)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
159 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
160 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
161
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
162 impl std::fmt::Debug for ConfigLayer {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
164 let mut sections: Vec<_> = self.sections.iter().collect();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
165 sections.sort_by(|e0, e1| e0.0.cmp(e1.0));
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
166
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
167 for (section, items) in sections.into_iter() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
168 let mut items: Vec<_> = items.into_iter().collect();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
169 items.sort_by(|e0, e1| e0.0.cmp(e1.0));
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
170
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
171 for (item, config_entry) in items {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
172 writeln!(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
173 f,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
174 "{}",
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
175 String::from_utf8_lossy(&format_bytes!(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
176 b"{}.{}={} # {}",
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
177 section,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
178 item,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
179 &config_entry.bytes,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
180 &self.origin.to_bytes(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
181 ))
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
182 )?
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
183 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
184 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
185 Ok(())
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
186 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
187 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
188
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
189 /// Mapping of section item to value.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
190 /// In the following:
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
191 /// ```text
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
192 /// [ui]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
193 /// paginate=no
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
194 /// ```
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
195 /// "paginate" is the section item and "no" the value.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
196 pub type ConfigItem = HashMap<Vec<u8>, ConfigValue>;
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
197
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
198 #[derive(Clone, Debug, PartialEq)]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
199 pub struct ConfigValue {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
200 /// The raw bytes of the value (be it from the CLI, env or from a file)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
201 pub bytes: Vec<u8>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
202 /// Only present if the value comes from a file, 1-indexed.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
203 pub line: Option<usize>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
204 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
205
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
206 #[derive(Clone, Debug)]
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
207 pub enum ConfigOrigin {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
208 /// The value comes from a configuration file
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
209 File(PathBuf),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
210 /// The value comes from the environment like `$PAGER` or `$EDITOR`
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
211 Environment(Vec<u8>),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
212 /* TODO cli
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
213 * TODO defaults (configitems.py)
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
214 * TODO extensions
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
215 * TODO Python resources?
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
216 * Others? */
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
217 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
218
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
219 impl ConfigOrigin {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
220 /// TODO use some kind of dedicated trait?
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
221 pub fn to_bytes(&self) -> Vec<u8> {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
222 match self {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
223 ConfigOrigin::File(p) => get_bytes_from_path(p),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
224 ConfigOrigin::Environment(e) => e.to_owned(),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
225 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
226 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
227 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
228
46435
2e2033081274 rust: replace trivial `impl From …` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents: 46187
diff changeset
229 #[derive(Debug, derive_more::From)]
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
230 pub enum ConfigError {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
231 Parse {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
232 origin: ConfigOrigin,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
233 line: Option<usize>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
234 bytes: Vec<u8>,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
235 },
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
236 /// Failed to include a sub config file
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
237 IncludeError {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
238 path: PathBuf,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
239 io_error: std::io::Error,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
240 },
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
241 /// Any IO error that isn't expected
46435
2e2033081274 rust: replace trivial `impl From …` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents: 46187
diff changeset
242 #[from]
46187
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
243 IO(std::io::Error),
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
244 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
245
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
246 fn make_regex(pattern: &'static str) -> Regex {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
247 Regex::new(pattern).expect("expected a valid regex")
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
248 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
249
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
250 /// Includes are relative to the file they're defined in, unless they're
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
251 /// absolute.
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
252 fn read_include(
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
253 old_src: &Path,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
254 new_src: &Path,
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
255 ) -> (PathBuf, io::Result<Vec<u8>>) {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
256 if new_src.is_absolute() {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
257 (new_src.to_path_buf(), read_whole_file(&new_src))
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
258 } else {
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
259 let dir = old_src.parent().unwrap();
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
260 let new_src = dir.join(&new_src);
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
261 (new_src.to_owned(), read_whole_file(&new_src))
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
262 }
95d6f31e88db hg-core: add basic config module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
263 }