##// END OF EJS Templates
rust: use HgError in ConfigError...
Simon Sapin -
r47176:0cb1b022 default
parent child Browse files
Show More
@@ -8,7 +8,9 b''
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 use super::layer;
10 use super::layer;
11 use crate::config::layer::{ConfigError, ConfigLayer, ConfigValue};
11 use crate::config::layer::{
12 ConfigError, ConfigLayer, ConfigParseError, ConfigValue,
13 };
12 use std::path::PathBuf;
14 use std::path::PathBuf;
13
15
14 use crate::repo::Repo;
16 use crate::repo::Repo;
@@ -89,11 +91,11 b' impl Config {'
89 &self,
91 &self,
90 section: &[u8],
92 section: &[u8],
91 item: &[u8],
93 item: &[u8],
92 ) -> Result<Option<bool>, ConfigError> {
94 ) -> Result<Option<bool>, ConfigParseError> {
93 match self.get_inner(&section, &item) {
95 match self.get_inner(&section, &item) {
94 Some((layer, v)) => match parse_bool(&v.bytes) {
96 Some((layer, v)) => match parse_bool(&v.bytes) {
95 Some(b) => Ok(Some(b)),
97 Some(b) => Ok(Some(b)),
96 None => Err(ConfigError::Parse {
98 None => Err(ConfigParseError {
97 origin: layer.origin.to_owned(),
99 origin: layer.origin.to_owned(),
98 line: v.line,
100 line: v.line,
99 bytes: v.bytes.to_owned(),
101 bytes: v.bytes.to_owned(),
@@ -7,6 +7,7 b''
7 // This software may be used and distributed according to the terms of the
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 use crate::errors::{HgError, IoResultExt};
10 use crate::utils::files::{
11 use crate::utils::files::{
11 get_bytes_from_path, get_path_from_bytes, read_whole_file,
12 get_bytes_from_path, get_path_from_bytes, read_whole_file,
12 };
13 };
@@ -99,20 +100,12 b' impl ConfigLayer {'
99 if let Some(m) = INCLUDE_RE.captures(&bytes) {
100 if let Some(m) = INCLUDE_RE.captures(&bytes) {
100 let filename_bytes = &m[1];
101 let filename_bytes = &m[1];
101 let filename_to_include = get_path_from_bytes(&filename_bytes);
102 let filename_to_include = get_path_from_bytes(&filename_bytes);
102 match read_include(&src, &filename_to_include) {
103 let (include_src, result) =
103 (include_src, Ok(data)) => {
104 read_include(&src, &filename_to_include);
104 layers.push(current_layer);
105 let data = result.for_file(filename_to_include)?;
105 layers.extend(Self::parse(&include_src, &data)?);
106 layers.push(current_layer);
106 current_layer =
107 layers.extend(Self::parse(&include_src, &data)?);
107 Self::new(ConfigOrigin::File(src.to_owned()));
108 current_layer = Self::new(ConfigOrigin::File(src.to_owned()));
108 }
109 (_, Err(e)) => {
110 return Err(ConfigError::IncludeError {
111 path: filename_to_include.to_owned(),
112 io_error: e,
113 })
114 }
115 }
116 } else if let Some(_) = EMPTY_RE.captures(&bytes) {
109 } else if let Some(_) = EMPTY_RE.captures(&bytes) {
117 } else if let Some(m) = SECTION_RE.captures(&bytes) {
110 } else if let Some(m) = SECTION_RE.captures(&bytes) {
118 section = m[1].to_vec();
111 section = m[1].to_vec();
@@ -145,11 +138,12 b' impl ConfigLayer {'
145 map.remove(&m[1]);
138 map.remove(&m[1]);
146 }
139 }
147 } else {
140 } else {
148 return Err(ConfigError::Parse {
141 return Err(ConfigParseError {
149 origin: ConfigOrigin::File(src.to_owned()),
142 origin: ConfigOrigin::File(src.to_owned()),
150 line: Some(index + 1),
143 line: Some(index + 1),
151 bytes: bytes.to_owned(),
144 bytes: bytes.to_owned(),
152 });
145 }
146 .into());
153 }
147 }
154 }
148 }
155 if !current_layer.is_empty() {
149 if !current_layer.is_empty() {
@@ -226,21 +220,17 b' impl ConfigOrigin {'
226 }
220 }
227 }
221 }
228
222
223 #[derive(Debug)]
224 pub struct ConfigParseError {
225 pub origin: ConfigOrigin,
226 pub line: Option<usize>,
227 pub bytes: Vec<u8>,
228 }
229
229 #[derive(Debug, derive_more::From)]
230 #[derive(Debug, derive_more::From)]
230 pub enum ConfigError {
231 pub enum ConfigError {
231 Parse {
232 Parse(ConfigParseError),
232 origin: ConfigOrigin,
233 Other(HgError),
233 line: Option<usize>,
234 bytes: Vec<u8>,
235 },
236 /// Failed to include a sub config file
237 IncludeError {
238 path: PathBuf,
239 io_error: std::io::Error,
240 },
241 /// Any IO error that isn't expected
242 #[from]
243 IO(std::io::Error),
244 }
234 }
245
235
246 fn make_regex(pattern: &'static str) -> Regex {
236 fn make_regex(pattern: &'static str) -> Regex {
General Comments 0
You need to be logged in to leave comments. Login now