Skip to main content

watch_path/
watcher.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4use thiserror::Error;
5
6#[derive(Debug, Clone)]
7pub struct WatchEvent {
8    pub path: String,
9    pub kind: WatchEventKind,
10}
11
12#[derive(Debug, Clone)]
13pub enum WatchEventKind {
14    Modified,
15    Created,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum ConnectionState {
20    Connected,
21    Degraded,
22    Lost,
23}
24
25#[derive(Debug, Error)]
26pub enum WatchError {
27    #[error("io error: {0}")]
28    Io(#[from] std::io::Error),
29
30    #[error("connection failed: {0}")]
31    Connection(String),
32
33    #[error("unsupported protocol: {0}")]
34    UnsupportedProtocol(String),
35
36    #[error("invalid url: {0}")]
37    InvalidUrl(String),
38
39    #[error("notify error: {0}")]
40    Notify(String),
41
42    #[error("ssh error: {0}")]
43    Ssh(String),
44
45    #[error("ftp error: {0}")]
46    Ftp(String),
47
48    #[error("http error: {0}")]
49    Http(String),
50}
51
52#[derive(Debug, Clone)]
53pub struct WatchOptions {
54    pub debounce: Duration,
55    pub poll_interval: Duration,
56    pub loss_timeout: Duration,
57    pub password: Option<String>,
58    pub key_path: Option<PathBuf>,
59}
60
61impl Default for WatchOptions {
62    fn default() -> Self {
63        Self {
64            debounce: Duration::from_secs(2),
65            poll_interval: Duration::from_secs(5),
66            loss_timeout: Duration::from_secs(30),
67            password: None,
68            key_path: None,
69        }
70    }
71}
72
73pub trait PathWatcher {
74    fn poll(&mut self) -> Result<Vec<WatchEvent>, WatchError>;
75    fn read(&mut self, path: &str) -> Result<Vec<u8>, WatchError>;
76    fn has_pending(&self) -> bool;
77    fn connection_state(&self) -> ConnectionState;
78}