rust/hg-core/src/config/layer.rs
changeset 46744 3d692e724d06
parent 46740 28a54c128e82
child 46746 1bac7764ceef
equal deleted inserted replaced
46743:dfd35823635b 46744:3d692e724d06
     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::errors::{HgError, IoResultExt};
    11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes};
    11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes};
    12 use format_bytes::{write_bytes, DisplayBytes};
    12 use format_bytes::{format_bytes, write_bytes, DisplayBytes};
    13 use lazy_static::lazy_static;
    13 use lazy_static::lazy_static;
    14 use regex::bytes::Regex;
    14 use regex::bytes::Regex;
    15 use std::collections::HashMap;
    15 use std::collections::HashMap;
    16 use std::path::{Path, PathBuf};
    16 use std::path::{Path, PathBuf};
    17 
    17 
   185             } else if let Some(m) = UNSET_RE.captures(&bytes) {
   185             } else if let Some(m) = UNSET_RE.captures(&bytes) {
   186                 if let Some(map) = current_layer.sections.get_mut(&section) {
   186                 if let Some(map) = current_layer.sections.get_mut(&section) {
   187                     map.remove(&m[1]);
   187                     map.remove(&m[1]);
   188                 }
   188                 }
   189             } else {
   189             } else {
       
   190                 let message = if bytes.starts_with(b" ") {
       
   191                     format_bytes!(b"unexpected leading whitespace: {}", bytes)
       
   192                 } else {
       
   193                     bytes.to_owned()
       
   194                 };
   190                 return Err(ConfigParseError {
   195                 return Err(ConfigParseError {
   191                     origin: ConfigOrigin::File(src.to_owned()),
   196                     origin: ConfigOrigin::File(src.to_owned()),
   192                     line: Some(index + 1),
   197                     line: Some(index + 1),
   193                     bytes: bytes.to_owned(),
   198                     message,
   194                 }
   199                 }
   195                 .into());
   200                 .into());
   196             }
   201             }
   197         }
   202         }
   198         if !current_layer.is_empty() {
   203         if !current_layer.is_empty() {
   276 
   281 
   277 #[derive(Debug)]
   282 #[derive(Debug)]
   278 pub struct ConfigParseError {
   283 pub struct ConfigParseError {
   279     pub origin: ConfigOrigin,
   284     pub origin: ConfigOrigin,
   280     pub line: Option<usize>,
   285     pub line: Option<usize>,
   281     pub bytes: Vec<u8>,
   286     pub message: Vec<u8>,
   282 }
   287 }
   283 
   288 
   284 #[derive(Debug, derive_more::From)]
   289 #[derive(Debug, derive_more::From)]
   285 pub enum ConfigError {
   290 pub enum ConfigError {
   286     Parse(ConfigParseError),
   291     Parse(ConfigParseError),