Skip to content

Commit

Permalink
Rename Regex -> PreventSyncPattern step 3: rename `Regex{Error|Re…
Browse files Browse the repository at this point in the history
…sult}` -> `PreventSyncPattern{Error|Result}`
  • Loading branch information
touilleMan committed Dec 2, 2024
1 parent ef20e90 commit dade3f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions libparsec/crates/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use libparsec_crypto::CryptoError;
use crate::{DateTime, DeviceFileType, DeviceID, HumanHandle, UserID, VlobID};

#[derive(Error, Debug)]
pub enum RegexError {
pub enum PreventSyncPatternError {
#[error("Regex parsing err: {err}")]
ParseError { err: regex::Error },
#[error("Failed to convert glob pattern into regex: {err}")]
Expand All @@ -21,7 +21,7 @@ pub enum RegexError {
},
}

pub type RegexResult<T> = Result<T, RegexError>;
pub type PreventSyncPatternResult<T> = Result<T, PreventSyncPatternError>;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum DataError {
Expand Down
32 changes: 19 additions & 13 deletions libparsec/crates/types/src/prevent_sync_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
path::Path,
};

use crate::{RegexError, RegexResult};
use crate::{PreventSyncPatternError, PreventSyncPatternResult};

#[derive(Clone, Debug)]
pub struct PreventSyncPattern(pub Vec<regex::Regex>);
Expand All @@ -34,9 +34,9 @@ fn escape_globing_pattern(string: &str) -> String {

impl PreventSyncPattern {
/// Returns a regex which is built from a file that contains shell like patterns
pub fn from_file(file_path: &Path) -> RegexResult<Self> {
pub fn from_file(file_path: &Path) -> PreventSyncPatternResult<Self> {
let reader = fs::File::open(file_path).map(BufReader::new).map_err(|e| {
RegexError::PatternFileIOError {
PreventSyncPatternError::PatternFileIOError {
file_path: file_path.to_path_buf(),
err: e,
}
Expand All @@ -45,7 +45,10 @@ impl PreventSyncPattern {
Self::from_glob_reader(file_path, reader)
}

pub fn from_glob_reader<P: AsRef<Path>, R: BufRead>(path: P, reader: R) -> RegexResult<Self> {
pub fn from_glob_reader<P: AsRef<Path>, R: BufRead>(
path: P,
reader: R,
) -> PreventSyncPatternResult<Self> {
reader
.lines()
.filter_map(|line| match line {
Expand All @@ -58,31 +61,33 @@ impl PreventSyncPattern {
None
}
}
Err(e) => Some(Err(RegexError::PatternFileIOError {
Err(e) => Some(Err(PreventSyncPatternError::PatternFileIOError {
file_path: path.as_ref().to_path_buf(),
err: e,
})),
})
.collect::<RegexResult<Vec<regex::Regex>>>()
.collect::<PreventSyncPatternResult<Vec<regex::Regex>>>()
.map(Self)
}

/// Returns a regex which is an union of all regexes from `raw_regexes` slice parameter
pub fn from_raw_regexes(raw_regexes: &[&str]) -> RegexResult<Self> {
pub fn from_raw_regexes(raw_regexes: &[&str]) -> PreventSyncPatternResult<Self> {
Ok(Self(
raw_regexes
.iter()
.map(|l| regex::Regex::new(l).map_err(|err| RegexError::ParseError { err }))
.collect::<Result<Vec<regex::Regex>, RegexError>>()?,
.map(|l| {
regex::Regex::new(l).map_err(|err| PreventSyncPatternError::ParseError { err })
})
.collect::<Result<Vec<regex::Regex>, PreventSyncPatternError>>()?,
))
}

/// Returns a regex from a glob pattern
pub fn from_glob_pattern(pattern: &str) -> RegexResult<Self> {
pub fn from_glob_pattern(pattern: &str) -> PreventSyncPatternResult<Self> {
from_glob_pattern(pattern).map(|re| Self(vec![re]))
}

pub fn from_regex_str(regex_str: &str) -> RegexResult<Self> {
pub fn from_regex_str(regex_str: &str) -> PreventSyncPatternResult<Self> {
Self::from_raw_regexes(&[regex_str])
}

Expand All @@ -97,9 +102,10 @@ impl PreventSyncPattern {
}

/// Parse a glob pattern like `*.rs` and convert it to an regex.
fn from_glob_pattern(pattern: &str) -> RegexResult<regex::Regex> {
fn from_glob_pattern(pattern: &str) -> PreventSyncPatternResult<regex::Regex> {
let escaped_str = escape_globing_pattern(pattern);
fnmatch_regex::glob_to_regex(&escaped_str).map_err(|err| RegexError::GlobPatternError { err })
fnmatch_regex::glob_to_regex(&escaped_str)
.map_err(|err| PreventSyncPatternError::GlobPatternError { err })
}

impl PartialEq for PreventSyncPattern {
Expand Down

0 comments on commit dade3f3

Please sign in to comment.