rust/rhg/src/error.rs
changeset 49485 ffd4b1f1c9cb
parent 49174 3f86ee422095
child 49487 9f14126cfc4c
equal deleted inserted replaced
49484:85f5d11c77dd 49485:ffd4b1f1c9cb
     6 use hg::dirstate_tree::on_disk::DirstateV2ParseError;
     6 use hg::dirstate_tree::on_disk::DirstateV2ParseError;
     7 use hg::errors::HgError;
     7 use hg::errors::HgError;
     8 use hg::exit_codes;
     8 use hg::exit_codes;
     9 use hg::repo::RepoError;
     9 use hg::repo::RepoError;
    10 use hg::revlog::revlog::RevlogError;
    10 use hg::revlog::revlog::RevlogError;
       
    11 use hg::sparse::SparseConfigError;
    11 use hg::utils::files::get_bytes_from_path;
    12 use hg::utils::files::get_bytes_from_path;
    12 use hg::{DirstateError, DirstateMapError, StatusError};
    13 use hg::{DirstateError, DirstateMapError, StatusError};
    13 use std::convert::From;
    14 use std::convert::From;
    14 
    15 
    15 /// The kind of command error
    16 /// The kind of command error
    47             // TODO: bytes-based (instead of Unicode-based) formatting
    48             // TODO: bytes-based (instead of Unicode-based) formatting
    48             // of error messages to handle non-UTF-8 filenames etc:
    49             // of error messages to handle non-UTF-8 filenames etc:
    49             // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output
    50             // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output
    50             message: utf8_to_local(message.as_ref()).into(),
    51             message: utf8_to_local(message.as_ref()).into(),
    51             detailed_exit_code: detailed_exit_code,
    52             detailed_exit_code: detailed_exit_code,
       
    53         }
       
    54     }
       
    55 
       
    56     pub fn abort_with_exit_code_bytes(
       
    57         message: impl AsRef<[u8]>,
       
    58         detailed_exit_code: exit_codes::ExitCode,
       
    59     ) -> Self {
       
    60         // TODO: use this everywhere it makes sense instead of the string
       
    61         // version.
       
    62         CommandError::Abort {
       
    63             message: message.as_ref().into(),
       
    64             detailed_exit_code,
    52         }
    65         }
    53     }
    66     }
    54 
    67 
    55     pub fn unsupported(message: impl AsRef<str>) -> Self {
    68     pub fn unsupported(message: impl AsRef<str>) -> Self {
    56         CommandError::UnsupportedFeature {
    69         CommandError::UnsupportedFeature {
   210 impl From<DirstateV2ParseError> for CommandError {
   223 impl From<DirstateV2ParseError> for CommandError {
   211     fn from(error: DirstateV2ParseError) -> Self {
   224     fn from(error: DirstateV2ParseError) -> Self {
   212         HgError::from(error).into()
   225         HgError::from(error).into()
   213     }
   226     }
   214 }
   227 }
       
   228 
       
   229 impl From<SparseConfigError> for CommandError {
       
   230     fn from(e: SparseConfigError) -> Self {
       
   231         match e {
       
   232             SparseConfigError::IncludesAfterExcludes { context } => {
       
   233                 Self::abort_with_exit_code_bytes(
       
   234                     format_bytes!(
       
   235                         b"{} config cannot have includes after excludes",
       
   236                         context
       
   237                     ),
       
   238                     exit_codes::CONFIG_PARSE_ERROR_ABORT,
       
   239                 )
       
   240             }
       
   241             SparseConfigError::EntryOutsideSection { context, line } => {
       
   242                 Self::abort_with_exit_code_bytes(
       
   243                     format_bytes!(
       
   244                         b"{} config entry outside of section: {}",
       
   245                         context,
       
   246                         &line,
       
   247                     ),
       
   248                     exit_codes::CONFIG_PARSE_ERROR_ABORT,
       
   249                 )
       
   250             }
       
   251             SparseConfigError::HgError(e) => Self::from(e),
       
   252             SparseConfigError::PatternError(e) => {
       
   253                 Self::unsupported(format!("{}", e))
       
   254             }
       
   255         }
       
   256     }
       
   257 }