#监视 #文件 #基本

fwatch

一个简单的文件监视器库

6 个版本

0.1.5 2019年11月2日
0.1.4 2019年11月2日
0.1.3 2019年7月24日

#4 in #监视

每月 42 次下载
2 crates 中使用

MIT/Apache

13KB
126 行代码

fwatch

fwatch 是一个用 Rust 编写的文件监视库。


安装

将以下内容添加到您的 Cargo.toml

[dependencies]
fwatch = "0.1"

基本用法

以下示例设置了一个 Watcher 来监视对 foo.txtbar.txt 的任何更改(在 fwatch 中称为 Transition)。目前可用的转换有 CreatedModifiedDeletedNone

以下可以使用实现 Watchable 特质的任何结构体,而不仅仅是 BasicTarget

use fwatch::{BasicTarget, Watcher, Transition};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();

    // Add a couple of files to watch
    watcher.add_target(BasicTarget::new("foo.txt"));
    watcher.add_target(BasicTarget::new("bar.txt"));
    
    // Calling watcher.watch() returns a vector of Transitions, 
    // teling us if any of the watched files have undergone a
    // transition since the previous call to watcher.watch()
    for (index, transition) in watcher.watch().into_iter().enumerate() {
        // Get the path and state of the current target
        let path = watcher.get_path(index).unwrap();
        let state = watcher.get_state(index).uwnrap();

        // Do something based on the observed transition.
        match transition {
            Transition::Created => { 
            /* The watched file has been created */ 
            },
            Transition::Modified => { 
            /* The watched file has been modified */ 
            },
            Transition::Deleted => { 
            /* The watched file has been deleted */ 
            },
            Transition::None => { 
            /* None of the above transitions were observed */ 
            },
        }
    }
}

许可证

许可如下:

无运行时依赖