watchexec/lib/src/lib.rs

116 lines
4 KiB
Rust
Raw Normal View History

2021-08-16 11:49:12 +02:00
//! Watchexec: a library for utilities and programs which respond to events;
//! file changes, human interaction, and more.
2019-10-27 10:54:44 +01:00
//!
2021-08-19 10:31:29 +02:00
//! Also see the CLI tool: <https://watchexec.github.io/>
2019-10-27 11:29:58 +01:00
//!
2021-08-19 10:31:29 +02:00
//! This library is powered by [Tokio](https://tokio.rs), minimum version 1.10. This requirement may
//! change (upwards) in the future without breaking change.
2021-08-16 11:49:12 +02:00
//!
//! The main way to use this crate involves constructing a [`Watchexec`] around an
//! [`InitConfig`][config::InitConfig] and a [`RuntimeConfig`][config::RuntimeConfig], then running
//! it. [`Handler`][handler::Handler]s are used to hook into watchexec at various points. The
2021-08-22 14:31:39 +02:00
//! runtime config can be changed at any time with the [`Watchexec::reconfigure()`] method.
2021-08-19 10:31:29 +02:00
//!
2021-10-16 08:08:35 +02:00
//! It's recommended to use the [miette] erroring library in applications, but all errors implement
//! [`std::error::Error`] so your favourite error handling library can of course be used.
//!
2021-08-22 12:06:50 +02:00
//! ```no_run
2021-10-16 08:08:35 +02:00
//! use miette::{IntoDiagnostic, Result};
2021-08-22 12:06:50 +02:00
//! use watchexec::{
//! Watchexec,
//! action::{Action, Outcome},
//! config::{InitConfig, RuntimeConfig},
2021-08-22 12:06:50 +02:00
//! handler::{Handler as _, PrintDebug},
//! };
//!
//! #[tokio::main]
2021-10-16 08:08:35 +02:00
//! async fn main() -> Result<()> {
//! let mut init = InitConfig::default();
2021-08-22 12:06:50 +02:00
//! init.on_error(PrintDebug(std::io::stderr()));
//!
2021-08-22 12:06:50 +02:00
//! let mut runtime = RuntimeConfig::default();
2021-08-22 10:49:24 +02:00
//! runtime.pathset(["watchexec.conf"]);
//!
//! let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
//! conf.apply(&mut runtime);
//!
//! let we = Watchexec::new(init, runtime.clone())?;
//! let w = we.clone();
//!
2021-08-22 12:06:50 +02:00
//! let c = runtime.clone();
//! runtime.on_action(move |action: Action| {
//! let mut c = c.clone();
//! let w = w.clone();
//! async move {
//! for event in &action.events {
2021-10-16 08:08:35 +02:00
//! if event.paths().any(|(p, _)| p.ends_with("/watchexec.conf")) {
2021-08-22 12:06:50 +02:00
//! let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
//!
2021-08-22 12:06:50 +02:00
//! conf.apply(&mut c);
2021-08-22 14:31:39 +02:00
//! w.reconfigure(c.clone());
2021-08-22 12:06:50 +02:00
//! // tada! self-reconfiguring watchexec on config file change!
//!
//! break;
//! }
//! }
//!
//! action.outcome(Outcome::if_running(
//! Outcome::DoNothing,
//! Outcome::both(Outcome::Clear, Outcome::Start),
2021-08-22 12:06:50 +02:00
//! ));
//!
//! Ok(())
2021-10-16 08:08:35 +02:00
//! # as std::result::Result<_, MietteStub>
//! }
//! });
//!
2021-10-16 08:08:35 +02:00
//! we.main().await.into_diagnostic()?;
2021-08-22 12:06:50 +02:00
//! Ok(())
//! }
2021-08-22 12:06:50 +02:00
//! # struct YourConfigFormat;
//! # impl YourConfigFormat {
2021-10-16 08:08:35 +02:00
//! # async fn load_from_file(_: &str) -> std::result::Result<Self, MietteStub> { Ok(Self) }
2021-08-22 12:06:50 +02:00
//! # fn apply(&self, _: &mut RuntimeConfig) { }
//! # }
2021-10-16 08:08:35 +02:00
//! # use miette::Diagnostic;
//! # use thiserror::Error;
//! # #[derive(Debug, Error, Diagnostic)]
//! # #[error("stub")]
//! # struct MietteStub;
//! ```
//!
2021-08-19 10:31:29 +02:00
//! Alternatively, one can use the modules exposed by the crate and the external crates such as
//! [ClearScreen][clearscreen] and [Command Group][command_group] to build something more advanced,
//! at the cost of reimplementing the glue code. See the examples folder for some basic/demo tools
//! written with the individual modules.
2021-08-16 11:49:12 +02:00
//!
//! This crate does not itself use `unsafe`. However, it depends on a number of libraries which do,
//! most because they interact with the operating system.
2019-10-27 10:54:44 +01:00
2021-07-20 10:32:38 +02:00
#![doc(html_favicon_url = "https://watchexec.github.io/logo:watchexec.svg")]
#![doc(html_logo_url = "https://watchexec.github.io/logo:watchexec.svg")]
2021-10-16 06:13:32 +02:00
#![warn(clippy::unwrap_used, missing_docs)]
2021-08-16 11:49:12 +02:00
#![forbid(unsafe_code)]
2019-10-27 12:06:09 +01:00
2021-08-18 15:12:50 +02:00
// the toolkit to make your own
2021-08-20 18:43:55 +02:00
pub mod action;
2021-08-19 11:28:56 +02:00
pub mod command;
pub mod error;
2021-08-16 11:49:12 +02:00
pub mod event;
2021-09-13 09:51:07 +02:00
pub mod filter;
2021-08-16 15:15:17 +02:00
pub mod fs;
2021-10-09 07:45:32 +02:00
pub mod ignore_files;
2021-10-16 02:55:20 +02:00
pub mod paths;
2021-10-09 07:45:32 +02:00
pub mod project;
2021-08-17 11:41:13 +02:00
pub mod signal;
2021-08-16 17:09:22 +02:00
2021-08-18 15:12:50 +02:00
// the core experience
pub mod config;
pub mod handler;
2021-08-18 15:12:50 +02:00
mod watchexec;
#[doc(inline)]
pub use crate::watchexec::Watchexec;
2021-08-18 15:12:50 +02:00
2021-08-16 17:09:22 +02:00
// the *action* is debounced, not the events