optify/builder/
watcher_builder.rs

1use std::path::{Path, PathBuf};
2
3use crate::provider::{OptionsWatcher, WatcherOptions};
4
5use super::OptionsRegistryBuilder;
6
7/// A builder to use for local development to create an `OptionsWatcher` which changes the underlying `OptionsProvider` when files are changed.
8///
9/// This builder is kept separate from the `OptionsProviderBuilder` in order to keep `OptionsProviderBuilder` and `OptionsProvider` as simple and efficient as possible for production use.
10///
11/// ⚠️ Development in progress ⚠️\
12/// Not truly considered public yet and mainly available to support bindings for other languages.
13#[derive(Clone)]
14pub struct OptionsWatcherBuilder {
15    schema_path: Option<PathBuf>,
16    watched_directories: Vec<PathBuf>,
17    watcher_options: WatcherOptions,
18}
19
20impl Default for OptionsWatcherBuilder {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl OptionsWatcherBuilder {
27    pub fn new() -> Self {
28        OptionsWatcherBuilder {
29            schema_path: None,
30            watched_directories: Vec::new(),
31            watcher_options: WatcherOptions::default(),
32        }
33    }
34
35    pub fn with_watcher_options(&mut self, watcher_options: WatcherOptions) -> &mut Self {
36        self.watcher_options = watcher_options;
37        self
38    }
39}
40
41impl OptionsRegistryBuilder<OptionsWatcher> for OptionsWatcherBuilder {
42    /// Add a directory to watch for changes.
43    fn add_directory(&mut self, directory: impl AsRef<Path>) -> Result<&Self, String> {
44        self.watched_directories
45            .push(directory.as_ref().to_path_buf());
46        Ok(self)
47    }
48
49    fn with_schema(&mut self, schema_path: impl AsRef<Path>) -> Result<&Self, String> {
50        self.schema_path = Some(schema_path.as_ref().to_path_buf());
51        Ok(self)
52    }
53
54    fn build(&mut self) -> Result<OptionsWatcher, String> {
55        OptionsWatcher::new(
56            &self.watched_directories,
57            self.schema_path.as_ref(),
58            self.watcher_options.clone(),
59        )
60    }
61}