Debian Codegraph @

src/uu/csplit/src/csplit_error.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use std::io;
7 use std::ops::Range;
8 use thiserror::Error;
9 use uucore::display::Quotable;
10 use uucore::error::{UError, strip_errno};
11 use uucore::translate;
12
13 /// Where inside a pattern operand the caret belongs, and what to write under
14 /// it.
15 ///
16 /// `csplit: '/a(b/': invalid pattern` says nothing about which part of the
17 /// pattern the parser choked on. The parser works that out anyway, so it is
18 /// kept here for [`crate::diagnostics`] rather than thrown away; it never
19 /// changes the one-line message.
20 #[derive(Debug)]
21 pub struct PatternProblem {
22 /// Byte range inside the operand to point at.
23 pub span: Range<usize>,
24 /// What the regex engine said, when the operand was a well-formed
25 /// `/REGEXP/` or `%REGEXP%` whose regex did not compile. Not localized:
26 /// it is quoted from the engine, which has no translations.
27 pub label: Option<String>,
28 }
29
30 /// Errors thrown by the csplit command
31 #[derive(Debug, Error)]
32 pub enum CsplitError {
33 #[error("{}", strip_errno(_0))]
34 IoError(#[from] io::Error),
35 #[error("{}", translate!("csplit-error-line-out-of-range", "pattern" => _0.quote()))]
36 LineOutOfRange(String),
37 #[error("{}", translate!("csplit-error-line-out-of-range-on-repetition", "pattern" => _0.quote(), "repetition" => _1))]
38 LineOutOfRangeOnRepetition(String, usize),
39 #[error("{}", translate!("csplit-error-match-not-found", "pattern" => _0.quote()))]
40 MatchNotFound(String),
41 #[error("{}", translate!("csplit-error-match-not-found-on-repetition", "pattern" => _0.quote(), "repetition" => _1))]
42 MatchNotFoundOnRepetition(String, usize),
43 #[error("{}", translate!("csplit-error-line-number-is-zero"))]
44 LineNumberIsZero,
45 #[error("{}", translate!("csplit-error-line-number-smaller-than-previous", "current" => _0, "previous" => _1))]
46 LineNumberSmallerThanPrevious(usize, usize),
47 #[error("{}", translate!("csplit-error-invalid-pattern", "pattern" => _0.quote()))]
48 InvalidPattern(String, Option<PatternProblem>),
49 #[error("{}", translate!("csplit-error-invalid-number", "number" => _0.quote()))]
50 InvalidNumber(String),
51 #[error("{}", translate!("csplit-error-suffix-format-incorrect"))]
52 SuffixFormatIncorrect,
53 #[error("{}", translate!("csplit-error-suffix-format-too-many-percents"))]
54 SuffixFormatTooManyPercents,
55 #[error("{}", translate!("csplit-error-not-regular-file", "file" => _0.quote()))]
56 NotRegularFile(String),
57 #[error("{}", _0)]
58 UError(Box<dyn UError>),
59 }
60
61 impl From<Box<dyn UError>> for CsplitError {
62 fn from(error: Box<dyn UError>) -> Self {
63 Self::UError(error)
64 }
65 }
66
67 impl UError for CsplitError {
68 fn code(&self) -> i32 {
69 match self {
70 Self::UError(e) => e.code(),
71 _ => 1,
72 }
73 }
74 }
75
76 #[cfg(test)]
77 mod tests {
78 #[cfg(unix)]
79 use super::CsplitError;
80
81 #[cfg(unix)]
82 #[test]
83 fn io_error_display_is_clean() {
84 // GNU does not print "IO error:" nor the raw "(os error N)" suffix.
85 let err = CsplitError::IoError(std::io::Error::from_raw_os_error(13));
86 let msg = err.to_string();
87 assert_eq!(msg, "Permission denied");
88 assert!(!msg.contains("IO error:"));
89 assert!(!msg.contains("os error"));
90 }
91 }