mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-15 00:47:01 +05:30
feat(config): add config validation
This commit is contained in:
parent
a8b4b579a7
commit
0e4fe250d7
2 changed files with 55 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
use tracing::trace;
|
||||
|
@ -66,6 +67,36 @@ pub trait Act {
|
|||
fn apply_action(&self, input: &str) -> String;
|
||||
}
|
||||
|
||||
impl Replacements<'_> {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
for subst in self.substitutors.iter() {
|
||||
match &subst.matcher {
|
||||
MatcherType::Single(matcher) => match matcher {
|
||||
Matcher::Regex { pattern } => {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
MatcherType::Multiple(matchers) => {
|
||||
for matcher in matchers.iter() {
|
||||
match matcher {
|
||||
Matcher::Regex { pattern } => {
|
||||
if let Err(e) = Regex::from_str(pattern) {
|
||||
bail!(e);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Match for Matcher<'_> {
|
||||
fn check_match(&self, string: &str) -> bool {
|
||||
trace!(?self, ?string, "Checking for match");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue