1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
use std::path::{Path, PathBuf};
use std::time::Duration;
/// Represents a file configuration source.
#[derive(Clone)]
pub struct FileSource {
/// Gets or sets the source file path.
pub path: PathBuf,
/// Gets or sets a value indicating whether the file is optional.
/// The default value is false.
pub optional: bool,
/// Gets or sets a value indicating whether the file will be loaded
/// if the underlying file changes. The default value is false.
pub reload_on_change: bool,
/// Get or sets the amount of time to wait after a change before reloading.
/// The default value is 250ms.
///
/// # Remarks
///
/// This helps avoid triggering reload before a file is completely written.
pub reload_delay: Duration,
}
impl FileSource {
/// Initializes a new file configuration source.
///
/// # Arguments
///
/// * `path` - The source file path
/// * `optional` - Indicates whether the source file must exist
/// * `reload_on_change` - Indicates if a reload should occur if the source file changes
/// * `reload_delay` - The amount of delay before reload after the source file changes
pub fn new(
path: PathBuf,
optional: bool,
reload_on_change: bool,
reload_delay: Option<Duration>,
) -> Self {
Self {
path,
optional,
reload_on_change,
reload_delay: reload_delay.unwrap_or(Duration::from_millis(250)),
}
}
/// Initializes a new, optional file configuration source.
///
/// # Arguments
///
/// * `path` - The source file path
pub fn optional<P: AsRef<Path>>(path: P) -> Self {
Self::new(path.as_ref().to_path_buf(), true, false, None)
}
}
impl From<PathBuf> for FileSource {
fn from(value: PathBuf) -> Self {
Self::new(value, false, false, None)
}
}
impl From<&PathBuf> for FileSource {
fn from(value: &PathBuf) -> Self {
Self::from(value.clone())
}
}
impl From<&Path> for FileSource {
fn from(value: &Path) -> Self {
Self::from(value.to_path_buf())
}
}
impl From<&str> for FileSource {
fn from(value: &str) -> Self {
Self::from(PathBuf::from(value))
}
}
impl From<String> for FileSource {
fn from(value: String) -> Self {
Self::from(PathBuf::from(value))
}
}
impl From<&String> for FileSource {
fn from(value: &String) -> Self {
Self::from(PathBuf::from(value))
}
}
/// Represents a builder for a file source.
pub struct FileSourceBuilder {
path: PathBuf,
optional: bool,
reload_on_change: bool,
reload_delay: Option<Duration>,
}
impl FileSourceBuilder {
/// Initializes a new file source builder.
///
/// # Arguments
///
/// * `path` - The path to build a file source for
pub fn new(path: PathBuf) -> Self {
Self {
path,
optional: false,
reload_on_change: false,
reload_delay: None,
}
}
/// Indicates the file source is optional.
pub fn optional(mut self) -> Self {
self.optional = true;
self
}
/// Indicates the file source can be reloaded.
pub fn reloadable(mut self) -> Self {
self.reload_on_change = true;
self
}
/// Sets the delay to wait before reloading when a file source changes.
pub fn reload_delay(mut self, delay: Duration) -> Self {
self.reload_delay = Some(delay);
self
}
/// Creates and returns a new [`FileSource`].
pub fn build(&self) -> FileSource {
FileSource::new(
self.path.clone(),
self.optional,
self.reload_on_change,
self.reload_delay,
)
}
}
impl From<FileSourceBuilder> for FileSource {
fn from(value: FileSourceBuilder) -> Self {
value.build()
}
}
impl From<&FileSourceBuilder> for FileSource {
fn from(value: &FileSourceBuilder) -> Self {
value.build()
}
}
pub mod ext {
use super::*;
/// Provides extension methods to create a [`FileSourceBuilder`].
pub trait FileSourceBuilderExtensions {
fn is(&self) -> FileSourceBuilder;
}
impl<T: AsRef<Path>> FileSourceBuilderExtensions for T {
fn is(&self) -> FileSourceBuilder {
FileSourceBuilder::new(self.as_ref().to_path_buf())
}
}
}