diff rust/hg-core/src/filepatterns.rs @ 52306:f33b87b46135

rust-lib: move `PatternError` to the `filepatterns` module This is where it belongs
author Raphaël Gomès <rgomes@octobus.net>
date Mon, 04 Nov 2024 11:21:43 +0100
parents e4b9f8a74d5f
children
line wrap: on
line diff
--- a/rust/hg-core/src/filepatterns.rs	Mon Nov 04 11:18:36 2024 +0100
+++ b/rust/hg-core/src/filepatterns.rs	Mon Nov 04 11:21:43 2024 +0100
@@ -13,13 +13,52 @@
         hg_path::{path_to_hg_path_buf, HgPathBuf, HgPathError},
         SliceExt,
     },
-    FastHashMap, PatternError,
+    FastHashMap,
 };
 use lazy_static::lazy_static;
 use regex::bytes::{NoExpand, Regex};
-use std::ops::Deref;
 use std::path::{Path, PathBuf};
 use std::vec::Vec;
+use std::{fmt, ops::Deref};
+
+#[derive(Debug, derive_more::From)]
+pub enum PatternError {
+    #[from]
+    Path(HgPathError),
+    UnsupportedSyntax(String),
+    UnsupportedSyntaxInFile(String, String, usize),
+    TooLong(usize),
+    #[from]
+    IO(std::io::Error),
+    /// Needed a pattern that can be turned into a regex but got one that
+    /// can't. This should only happen through programmer error.
+    NonRegexPattern(IgnorePattern),
+}
+
+impl fmt::Display for PatternError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            PatternError::UnsupportedSyntax(syntax) => {
+                write!(f, "Unsupported syntax {}", syntax)
+            }
+            PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => {
+                write!(
+                    f,
+                    "{}:{}: unsupported syntax {}",
+                    file_path, line, syntax
+                )
+            }
+            PatternError::TooLong(size) => {
+                write!(f, "matcher pattern is too long ({} bytes)", size)
+            }
+            PatternError::IO(error) => error.fmt(f),
+            PatternError::Path(error) => error.fmt(f),
+            PatternError::NonRegexPattern(pattern) => {
+                write!(f, "'{:?}' cannot be turned into a regex", pattern)
+            }
+        }
+    }
+}
 
 lazy_static! {
     static ref RE_ESCAPE: Vec<Vec<u8>> = {