comparison rust/hg-core/src/lib.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 79e8118cd846
children 22d24f6d6411
comparison
equal deleted inserted replaced
52305:79e8118cd846 52306:f33b87b46135
40 pub mod transaction; 40 pub mod transaction;
41 pub mod update; 41 pub mod update;
42 pub mod utils; 42 pub mod utils;
43 pub mod vfs; 43 pub mod vfs;
44 44
45 use crate::utils::hg_path::HgPathError;
46 pub use filepatterns::{ 45 pub use filepatterns::{
47 parse_pattern_syntax_kind, read_pattern_file, IgnorePattern, 46 parse_pattern_syntax_kind, read_pattern_file, IgnorePattern,
48 PatternFileWarning, PatternSyntax, 47 PatternFileWarning, PatternSyntax,
49 }; 48 };
50 use std::fmt;
51 use std::{collections::HashMap, sync::atomic::AtomicBool}; 49 use std::{collections::HashMap, sync::atomic::AtomicBool};
52 use twox_hash::RandomXxHashBuilder64; 50 use twox_hash::RandomXxHashBuilder64;
53 51
54 /// Used to communicate with threads spawned from code within this crate that 52 /// Used to communicate with threads spawned from code within this crate that
55 /// they should stop their work (SIGINT was received). 53 /// they should stop their work (SIGINT was received).
64 62
65 // TODO: should this be the default `FastHashMap` for all of hg-core, not just 63 // TODO: should this be the default `FastHashMap` for all of hg-core, not just
66 // dirstate? How does XxHash compare with AHash, hashbrown’s default? 64 // dirstate? How does XxHash compare with AHash, hashbrown’s default?
67 pub type FastHashbrownMap<K, V> = 65 pub type FastHashbrownMap<K, V> =
68 hashbrown::HashMap<K, V, RandomXxHashBuilder64>; 66 hashbrown::HashMap<K, V, RandomXxHashBuilder64>;
69
70 #[derive(Debug, derive_more::From)]
71 pub enum PatternError {
72 #[from]
73 Path(HgPathError),
74 UnsupportedSyntax(String),
75 UnsupportedSyntaxInFile(String, String, usize),
76 TooLong(usize),
77 #[from]
78 IO(std::io::Error),
79 /// Needed a pattern that can be turned into a regex but got one that
80 /// can't. This should only happen through programmer error.
81 NonRegexPattern(IgnorePattern),
82 }
83
84 impl fmt::Display for PatternError {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 match self {
87 PatternError::UnsupportedSyntax(syntax) => {
88 write!(f, "Unsupported syntax {}", syntax)
89 }
90 PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => {
91 write!(
92 f,
93 "{}:{}: unsupported syntax {}",
94 file_path, line, syntax
95 )
96 }
97 PatternError::TooLong(size) => {
98 write!(f, "matcher pattern is too long ({} bytes)", size)
99 }
100 PatternError::IO(error) => error.fmt(f),
101 PatternError::Path(error) => error.fmt(f),
102 PatternError::NonRegexPattern(pattern) => {
103 write!(f, "'{:?}' cannot be turned into a regex", pattern)
104 }
105 }
106 }
107 }