changeset 46447:0cb1b02228a6

rust: use HgError in ConfigError Differential Revision: https://phab.mercurial-scm.org/D9938
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 01 Feb 2021 12:55:31 +0100
parents 1dcd9c9975ed
children 1a7d12c82057
files rust/hg-core/src/config/config.rs rust/hg-core/src/config/layer.rs
diffstat 2 files changed, 24 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/config/config.rs	Thu Jan 28 20:31:42 2021 +0100
+++ b/rust/hg-core/src/config/config.rs	Mon Feb 01 12:55:31 2021 +0100
@@ -8,7 +8,9 @@
 // GNU General Public License version 2 or any later version.
 
 use super::layer;
-use crate::config::layer::{ConfigError, ConfigLayer, ConfigValue};
+use crate::config::layer::{
+    ConfigError, ConfigLayer, ConfigParseError, ConfigValue,
+};
 use std::path::PathBuf;
 
 use crate::repo::Repo;
@@ -89,11 +91,11 @@
         &self,
         section: &[u8],
         item: &[u8],
-    ) -> Result<Option<bool>, ConfigError> {
+    ) -> Result<Option<bool>, ConfigParseError> {
         match self.get_inner(&section, &item) {
             Some((layer, v)) => match parse_bool(&v.bytes) {
                 Some(b) => Ok(Some(b)),
-                None => Err(ConfigError::Parse {
+                None => Err(ConfigParseError {
                     origin: layer.origin.to_owned(),
                     line: v.line,
                     bytes: v.bytes.to_owned(),
--- a/rust/hg-core/src/config/layer.rs	Thu Jan 28 20:31:42 2021 +0100
+++ b/rust/hg-core/src/config/layer.rs	Mon Feb 01 12:55:31 2021 +0100
@@ -7,6 +7,7 @@
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
+use crate::errors::{HgError, IoResultExt};
 use crate::utils::files::{
     get_bytes_from_path, get_path_from_bytes, read_whole_file,
 };
@@ -99,20 +100,12 @@
             if let Some(m) = INCLUDE_RE.captures(&bytes) {
                 let filename_bytes = &m[1];
                 let filename_to_include = get_path_from_bytes(&filename_bytes);
-                match read_include(&src, &filename_to_include) {
-                    (include_src, Ok(data)) => {
-                        layers.push(current_layer);
-                        layers.extend(Self::parse(&include_src, &data)?);
-                        current_layer =
-                            Self::new(ConfigOrigin::File(src.to_owned()));
-                    }
-                    (_, Err(e)) => {
-                        return Err(ConfigError::IncludeError {
-                            path: filename_to_include.to_owned(),
-                            io_error: e,
-                        })
-                    }
-                }
+                let (include_src, result) =
+                    read_include(&src, &filename_to_include);
+                let data = result.for_file(filename_to_include)?;
+                layers.push(current_layer);
+                layers.extend(Self::parse(&include_src, &data)?);
+                current_layer = Self::new(ConfigOrigin::File(src.to_owned()));
             } else if let Some(_) = EMPTY_RE.captures(&bytes) {
             } else if let Some(m) = SECTION_RE.captures(&bytes) {
                 section = m[1].to_vec();
@@ -145,11 +138,12 @@
                     map.remove(&m[1]);
                 }
             } else {
-                return Err(ConfigError::Parse {
+                return Err(ConfigParseError {
                     origin: ConfigOrigin::File(src.to_owned()),
                     line: Some(index + 1),
                     bytes: bytes.to_owned(),
-                });
+                }
+                .into());
             }
         }
         if !current_layer.is_empty() {
@@ -226,21 +220,17 @@
     }
 }
 
+#[derive(Debug)]
+pub struct ConfigParseError {
+    pub origin: ConfigOrigin,
+    pub line: Option<usize>,
+    pub bytes: Vec<u8>,
+}
+
 #[derive(Debug, derive_more::From)]
 pub enum ConfigError {
-    Parse {
-        origin: ConfigOrigin,
-        line: Option<usize>,
-        bytes: Vec<u8>,
-    },
-    /// Failed to include a sub config file
-    IncludeError {
-        path: PathBuf,
-        io_error: std::io::Error,
-    },
-    /// Any IO error that isn't expected
-    #[from]
-    IO(std::io::Error),
+    Parse(ConfigParseError),
+    Other(HgError),
 }
 
 fn make_regex(pattern: &'static str) -> Regex {