Skip to main content

FileWatcher

Struct FileWatcher 

Source
pub struct FileWatcher { /* private fields */ }
Expand description

A polling-based file watcher that tracks modification times.

Call FileWatcher::check_for_changes to perform a single non-blocking scan, or watch_blocking to enter a poll-sleep loop with a rebuild callback.

Implementations§

Source§

impl FileWatcher

Source

pub fn new(config: WatchConfig) -> Result<Self>

Creates a new FileWatcher and takes an initial snapshot of the watched directory.

Returns an error if the directory does not exist or is unreadable.

§Examples
use ssg::watch::{FileWatcher, WatchConfig};
use std::time::Duration;
use tempfile::tempdir;

let dir = tempdir().unwrap();
let cfg = WatchConfig::new(dir.path().to_path_buf(), Duration::from_secs(1));
let watcher = FileWatcher::new(cfg).unwrap();
assert_eq!(watcher.tracked_file_count(), 0);
Source

pub const fn config(&self) -> &WatchConfig

Returns a reference to the watcher’s configuration.

§Examples
use ssg::watch::{FileWatcher, WatchConfig};
use std::time::Duration;
use tempfile::tempdir;

let dir = tempdir().unwrap();
let cfg = WatchConfig::new(dir.path().to_path_buf(), Duration::from_secs(1));
let watcher = FileWatcher::new(cfg).unwrap();
assert_eq!(watcher.config().poll_interval(), Duration::from_secs(1));
Source

pub fn check_for_changes(&mut self) -> Result<Vec<PathBuf>>

Performs a single, non-blocking check for file changes.

Scans the watched directory, compares modification times against the internal snapshot, and returns the list of paths that have been added, modified, or removed since the last check.

The internal snapshot is updated to reflect the current state of the filesystem after each call.

§Examples
use ssg::watch::{FileWatcher, WatchConfig};
use std::time::Duration;
use tempfile::tempdir;
use std::fs;

let dir = tempdir().unwrap();
let cfg = WatchConfig::new(dir.path().to_path_buf(), Duration::from_secs(1));
let mut watcher = FileWatcher::new(cfg).unwrap();
fs::write(dir.path().join("a.md"), "hi").unwrap();
let changes = watcher.check_for_changes().unwrap();
assert_eq!(changes.len(), 1);
Source

pub fn tracked_file_count(&self) -> usize

Returns the number of files currently tracked in the snapshot.

§Examples
use ssg::watch::{FileWatcher, WatchConfig};
use std::time::Duration;
use tempfile::tempdir;

let dir = tempdir().unwrap();
let cfg = WatchConfig::new(dir.path().to_path_buf(), Duration::from_secs(1));
let watcher = FileWatcher::new(cfg).unwrap();
assert_eq!(watcher.tracked_file_count(), 0);

Trait Implementations§

Source§

impl Debug for FileWatcher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more