mirror of
https://github.com/watchexec/watchexec.git
synced 2024-11-13 07:41:11 +01:00
Implement basic check_glob()
This commit is contained in:
parent
f58e97a62f
commit
e06f615531
2 changed files with 49 additions and 8 deletions
|
@ -1,5 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ignore::gitignore::GitignoreBuilder;
|
||||
use miette::Diagnostic;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{error::RuntimeError, event::Event};
|
||||
|
||||
pub mod globset;
|
||||
|
@ -24,6 +28,43 @@ impl<T: Filterer> Filterer for Arc<T> {
|
|||
/// Convenience function to check a glob pattern from a string.
|
||||
///
|
||||
/// This parses the glob and wraps any error with nice [miette] diagnostics.
|
||||
pub fn check_glob(s: &str) -> Result<&str, ()> {
|
||||
todo!()
|
||||
pub fn check_glob(glob: &str) -> Result<(), GlobParseError> {
|
||||
let mut builder = GitignoreBuilder::new("/");
|
||||
if let Err(err) = builder.add_line(None, glob) {
|
||||
if let ignore::Error::Glob { err, .. } = err {
|
||||
// TODO: use globset and return a nicer error
|
||||
Err(GlobParseError::new(glob, &err))
|
||||
} else {
|
||||
Err(GlobParseError::new(glob, "unknown error"))
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error when parsing a glob pattern from string.
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
#[error("invalid glob `{src}`: {err}")]
|
||||
#[diagnostic(code(watchexec::filter::glob_parse), url(docsrs))]
|
||||
pub struct GlobParseError {
|
||||
// The string that was parsed.
|
||||
#[source_code]
|
||||
src: String,
|
||||
|
||||
// The error that occurred.
|
||||
err: String,
|
||||
|
||||
// The span of the source which is in error.
|
||||
#[label = "invalid"]
|
||||
span: (usize, usize),
|
||||
}
|
||||
|
||||
impl GlobParseError {
|
||||
fn new(src: &str, err: &str) -> Self {
|
||||
Self {
|
||||
src: src.to_owned(),
|
||||
err: err.to_owned(),
|
||||
span: (0, src.len()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ impl From<MainSignal> for SubSignal {
|
|||
}
|
||||
|
||||
impl FromStr for SubSignal {
|
||||
type Err = ParseSignalError;
|
||||
type Err = SignalParseError;
|
||||
|
||||
#[cfg(unix)]
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
|
@ -157,7 +157,7 @@ impl FromStr for SubSignal {
|
|||
return Ok(Self::from_nix(sig));
|
||||
}
|
||||
|
||||
Err(ParseSignalError::new(s, "unsupported signal"))
|
||||
Err(SignalParseError::new(s, "unsupported signal"))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
@ -167,13 +167,13 @@ impl FromStr for SubSignal {
|
|||
"CTRL-BREAK" | "CTRL+BREAK" | "BREAK" => Ok(Self::Terminate),
|
||||
"CTRL-C" | "CTRL+C" | "C" => Ok(Self::Interrupt),
|
||||
"KILL" | "SIGKILL" | "FORCE-STOP" | "STOP" => Ok(Self::ForceStop),
|
||||
_ => Err(ParseSignalError::new(s, "unknown control name")),
|
||||
_ => Err(SignalParseError::new(s, "unknown control name")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Err(ParseSignalError::new(s, "no signals supported"))
|
||||
Err(SignalParseError::new(s, "no signals supported"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,7 +181,7 @@ impl FromStr for SubSignal {
|
|||
#[derive(Debug, Diagnostic, Error)]
|
||||
#[error("invalid signal `{src}`: {err}")]
|
||||
#[diagnostic(code(watchexec::signal::process::parse), url(docsrs))]
|
||||
pub struct ParseSignalError {
|
||||
pub struct SignalParseError {
|
||||
// The string that was parsed.
|
||||
#[source_code]
|
||||
src: String,
|
||||
|
@ -194,7 +194,7 @@ pub struct ParseSignalError {
|
|||
span: (usize, usize),
|
||||
}
|
||||
|
||||
impl ParseSignalError {
|
||||
impl SignalParseError {
|
||||
fn new(src: &str, err: &str) -> Self {
|
||||
Self {
|
||||
src: src.to_owned(),
|
||||
|
|
Loading…
Reference in a new issue