diff options
author | Radosław Kot <rdkt13@gmail.com> | 2021-10-23 17:16:52 +0200 |
---|---|---|
committer | Radosław Kot <rdkt13@gmail.com> | 2021-10-23 17:16:52 +0200 |
commit | b09d57a1a649a13fdea247d0016e043d4eeff285 (patch) | |
tree | 696fc33eac5b235bf8404a6874dcc435035b39fc /src | |
parent | e0df1523a4569742ac10636a8cb975d11b453d54 (diff) |
Change FileExtensions (rename, make subtrait)
Diffstat (limited to 'src')
-rw-r--r-- | src/file/extension.rs | 14 | ||||
-rw-r--r-- | src/file/format/mod.rs | 6 | ||||
-rw-r--r-- | src/file/mod.rs | 24 | ||||
-rw-r--r-- | src/file/source/file.rs | 8 | ||||
-rw-r--r-- | src/file/source/mod.rs | 4 | ||||
-rw-r--r-- | src/file/source/string.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 11 |
7 files changed, 28 insertions, 43 deletions
diff --git a/src/file/extension.rs b/src/file/extension.rs deleted file mode 100644 index ff67492..0000000 --- a/src/file/extension.rs +++ /dev/null @@ -1,14 +0,0 @@ -/// Describes an entity that can return file extensions. -/// -/// Meant primarily for [`Format`](crate::Format) trait. -/// Since [`Format`](crate::Format) only describes certain internal encoding, for instance JSON or Yaml -/// it is not necessarily bound to file extension name. -/// -/// In networking context JSONs are used without file extensions. -/// One can also imagine some encodings that do not necessarily have file extension associated, for instance -/// MessagePack or bincode. -/// Hence the decision to have extensions separated from [`Format`](crate::Format). -pub trait FileExtensions { - /// Returns a vector of file extensions, for instance `[yml, yaml]`. - fn extensions(&self) -> &'static [&'static str]; -} diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index d5414f5..d8817f2 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::error::Error; use crate::map::Map; -use crate::{file::extension::FileExtensions, value::Value, Format}; +use crate::{file::FileStoredFormat, value::Value, Format}; #[cfg(feature = "toml")] mod toml; @@ -129,8 +129,8 @@ impl Format for FileFormat { } } -impl FileExtensions for FileFormat { - fn extensions(&self) -> &'static [&'static str] { +impl FileStoredFormat for FileFormat { + fn file_extensions(&self) -> &'static [&'static str] { self.extensions() } } diff --git a/src/file/mod.rs b/src/file/mod.rs index fa6d383..629a866 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -1,4 +1,3 @@ -pub mod extension; mod format; pub mod source; @@ -14,7 +13,6 @@ use crate::Format; pub use self::format::FileFormat; use self::source::FileSource; -pub use self::extension::FileExtensions; pub use self::source::file::FileSourceFile; pub use self::source::string::FileSourceString; @@ -22,11 +20,7 @@ pub use self::source::string::FileSourceString; /// /// It supports optional automatic file format discovery. #[derive(Clone, Debug)] -pub struct File<T, F> -where - F: Format + FileExtensions, - T: FileSource<F>, -{ +pub struct File<T, F> { source: T, /// Format of file (which dictates what driver to use). @@ -36,9 +30,17 @@ where required: bool, } +/// An extension of [`Format`](crate::Format) trait. +/// +/// Associates format with file extensions, therefore linking storage-agnostic notion of format to a file system. +pub trait FileStoredFormat: Format { + /// Returns a vector of file extensions, for instance `[yml, yaml]`. + fn file_extensions(&self) -> &'static [&'static str]; +} + impl<F> File<source::string::FileSourceString, F> where - F: Format + FileExtensions + 'static, + F: FileStoredFormat + 'static, { pub fn from_str(s: &str, format: F) -> Self { File { @@ -51,7 +53,7 @@ where impl<F> File<source::file::FileSourceFile, F> where - F: Format + FileExtensions + 'static, + F: FileStoredFormat + 'static, { pub fn new(name: &str, format: F) -> Self { File { @@ -96,7 +98,7 @@ impl From<PathBuf> for File<source::file::FileSourceFile, FileFormat> { impl<T, F> File<T, F> where - F: Format + FileExtensions + 'static, + F: FileStoredFormat + 'static, T: FileSource<F>, { pub fn format(mut self, format: F) -> Self { @@ -112,7 +114,7 @@ where impl<T, F> Source for File<T, F> where - F: Format + FileExtensions + Debug + Clone + Send + Sync + 'static, + F: FileStoredFormat + Debug + Clone + Send + Sync + 'static, T: Sync + Send + FileSource<F> + 'static, { fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> { diff --git a/src/file/source/file.rs b/src/file/source/file.rs index e50b200..64126d0 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -6,7 +6,7 @@ use std::iter::Iterator; use std::path::{Path, PathBuf}; use crate::file::{ - format::ALL_EXTENSIONS, source::FileSourceResult, FileExtensions, FileSource, Format, + format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format, }; /// Describes a file sourced from a file @@ -26,7 +26,7 @@ impl FileSourceFile { format_hint: Option<F>, ) -> Result<(PathBuf, Box<dyn Format>), Box<dyn Error + Send + Sync>> where - F: FileExtensions + Format + 'static, + F: FileStoredFormat + Format + 'static, { // First check for an _exact_ match let mut filename = env::current_dir()?.as_path().join(self.name.clone()); @@ -59,7 +59,7 @@ impl FileSourceFile { match format_hint { Some(format) => { - for ext in format.extensions() { + for ext in format.file_extensions() { filename.set_extension(ext); if filename.is_file() { @@ -93,7 +93,7 @@ impl FileSourceFile { impl<F> FileSource<F> for FileSourceFile where - F: Format + FileExtensions + 'static, + F: Format + FileStoredFormat + 'static, { fn resolve( &self, diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index cd37317..3c3d10c 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -4,12 +4,12 @@ pub mod string; use std::error::Error; use std::fmt::Debug; -use crate::{file::FileExtensions, Format}; +use crate::{file::FileStoredFormat, Format}; /// Describes where the file is sourced pub trait FileSource<T>: Debug + Clone where - T: Format + FileExtensions, + T: Format + FileStoredFormat, { fn resolve( &self, diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 700d574..577c1e7 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -2,7 +2,7 @@ use std::error::Error; use crate::{ file::source::FileSourceResult, - file::{FileExtensions, FileSource}, + file::{FileSource, FileStoredFormat}, Format, }; @@ -18,7 +18,7 @@ impl<'a> From<&'a str> for FileSourceString { impl<F> FileSource<F> for FileSourceString where - F: Format + FileExtensions + 'static, + F: Format + FileStoredFormat + 'static, { fn resolve( &self, @@ -63,15 +63,12 @@ mod ser; mod source; mod value; -pub use crate::builder::AsyncConfigBuilder; -pub use crate::builder::ConfigBuilder; +pub use crate::builder::{AsyncConfigBuilder, ConfigBuilder}; pub use crate::config::Config; pub use crate::env::Environment; pub use crate::error::ConfigError; -pub use crate::file::{File, FileExtensions, FileFormat, FileSourceFile, FileSourceString}; +pub use crate::file::{File, FileFormat, FileSourceFile, FileSourceString, FileStoredFormat}; pub use crate::format::Format; pub use crate::map::Map; -pub use crate::source::AsyncSource; -pub use crate::source::Source; -pub use crate::value::Value; -pub use crate::value::ValueKind; +pub use crate::source::{AsyncSource, Source}; +pub use crate::value::{Value, ValueKind}; |