Function run

Source
pub fn run<P, Q>(
    input: P,
    output: Q,
    options: WatchOptions,
    config_adapter: &impl ConfigPort,
    persist_adapter: &impl PersistPort,
) -> Result<()>
where P: AsRef<Path>, Q: AsRef<Path>,
Expand description

Runs the configuration watch loop.

This function blocks, continuously watching the input file and converting it to JSON when changes are detected. It only returns on unrecoverable errors or when interrupted.

§Arguments

  • input — Path to the TOML configuration file to watch
  • output — Path where JSON output will be written
  • options — Watch configuration options
  • config_adapter — Adapter for parsing TOML
  • persist_adapter — Adapter for writing JSON

§Returns

  • Ok(()) — Watch loop exited cleanly (interrupted)
  • Err(FernError) — Unrecoverable error (e.g., watch system failure)

§Errors

Returns an error if:

  • The input file does not exist at startup
  • The file watcher cannot be initialized (e.g., too many open files)
  • The parent directory of the output file doesn’t exist

Configuration errors during the watch loop do not cause this function to return an error — they are reported via notification and logged.

§Example

ⓘ
use fernctl::commands::watch::{run, WatchOptions};
use fernctl::adapters::{TomlConfigAdapter, FileSystemAdapter};
use std::path::Path;

let config_adapter = TomlConfigAdapter::new();
let persist_adapter = FileSystemAdapter::new();
let options = WatchOptions::default();

// This blocks until Ctrl+C
run(
    Path::new("~/.config/fern/config.toml"),
    Path::new("~/.config/fern/config.json"),
    options,
    &config_adapter,
    &persist_adapter,
)?;

§Graceful Shutdown

The watch loop responds to SIGINT (Ctrl+C) by exiting cleanly. No special cleanup is required — in-progress writes are completed atomically before the function returns.

§Thread Safety

This function is not async and blocks the calling thread. For async usage, wrap it in a blocking task:

ⓘ
tokio::task::spawn_blocking(|| {
    watch::run(&input, &output, options, &config, &persist)
}).await?;