async_inotify/lib.rs
1/// Async Linux inotify wrapper for Rust.
2///
3/// # About
4///
5/// This is a wrapper for Linux inotify API, based `inotify-rs` to provide an async func to watch
6/// path(es) changes.
7///
8/// # Example
9///
10/// ```
11/// use std::ffi::OsString;
12/// use async_inotify::{
13/// Anotify,
14/// Event,
15/// WatchMask,
16/// }
17///
18/// #[tokio::main]
19/// async fn main() {
20/// let anotify = Anotify {
21/// mask: WatchMask::CREATE,
22/// regex: None,
23/// recursive: true,
24/// targets: vec![OsString::from("/tmp/cc")],
25/// };
26///
27/// let (tx, mut rx) = tokio::sync::broadcast::channel::<Event>(128);
28/// tokio::spawn(async move {
29/// loop {
30/// if let Ok(event) = rx.recv().await {
31/// println!("{:?}: {:?}", event.mask(), event.path());
32/// }
33/// }
34/// });
35///
36/// match async_inotify::handler::run(anotify, Some(tx), tokio::signal::ctrl_c()).await {
37/// // press ctrl_c
38/// Ok(()) => {},
39/// // catch error
40/// Err(e) => panic!("{}", e),
41/// };
42/// }
43/// ```
44///
45/// Or operate Watcher as you like.
46///
47/// ```rust
48/// use async_inotify::{WatchMask, Watcher};
49///
50/// #[tokio::main]
51/// async fn main() {
52/// let mut watcher = Watcher::init();
53/// let mask = WatchMask::CREATE;
54///
55/// let wd = watcher.add("/tmp/cc", &mask).unwrap();
56///
57/// // watch once
58/// if let Some(event) = watcher.next().await {
59/// println!("{:?}: {:?}", event.mask(), event.path());
60/// }
61///
62/// watcher.remove(wd).unwrap();
63/// }
64/// ```
65pub mod app;
66
67pub mod handler;
68mod watcher;
69
70pub use app::Anotify;
71pub use inotify::WatchMask;
72pub use watcher::Event;
73pub use watcher::Watcher;
74
75/// 定义 crate::Error
76/// 大部分函数返回的错误
77pub type Error = Box<dyn std::error::Error + Send + Sync>;
78
79/// 定义 crate::Result
80pub type Result<T> = std::result::Result<T, Error>;