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
extern crate preftool; extern crate preftool_dirs; use preftool::*; use preftool_dirs::*; use std::collections::HashSet; use std::fs::File; use std::io::{Error, ErrorKind, Read, Result}; use std::marker::PhantomData; use std::path::{Path, PathBuf}; pub trait ConfigurationFileFormat: Clone + Sized { type Provider: ConfigurationProvider + 'static; fn get_config<R: Read>(reader: R) -> Result<Self::Provider>; fn default_suffixes() -> &'static [&'static str]; fn file<P: AsRef<Path>>(path: P) -> ConfigurationFileSource<Self> { ConfigurationFileSource::new(PathBuf::from(path.as_ref()), false) } fn files<P: AsRef<Path>, I: IntoIterator<Item = P> + Clone + 'static, S: SearchPaths + Clone>( suffixes: I, search_paths: S, app_name: String, ) -> ConfigurationFileSources<S, Self> { ConfigurationFileSources::new(suffixes, search_paths, app_name) } fn default_config_files<S: SearchPaths + Clone>( search_paths: S, app_name: String, ) -> ConfigurationFileSources<S, Self> { Self::files(Self::default_suffixes(), search_paths, app_name) } } #[derive(Clone)] pub struct ConfigurationFileSource<F: ConfigurationFileFormat> { format: PhantomData<*const F>, path: PathBuf, required: bool, } impl<F: ConfigurationFileFormat> ConfigurationFileSource<F> { fn new(path: PathBuf, required: bool) -> Self { Self { format: PhantomData, path, required, } } } impl<F> ConfigurationSource for ConfigurationFileSource<F> where F: ConfigurationFileFormat, { fn build<B: ConfigurationBuilder>(self, mut builder: B) -> Result<B> { if self.required && !self.path.is_file() { return Err(Error::new( ErrorKind::NotFound, format!("Required config file {:#?} not found.", &self.path), )); } if self.path.is_file() { let f = File::open(&self.path)?; builder = builder.push_provider(F::get_config(f)?); } Ok(builder) } } #[derive(Clone)] pub struct ConfigurationFileSources<S, F> where S: SearchPaths + Clone, F: ConfigurationFileFormat, { format: PhantomData<*const F>, search_paths: S, suffixes: Vec<PathBuf>, app_name: String, } impl<S, F> ConfigurationFileSources<S, F> where S: SearchPaths + Clone, F: ConfigurationFileFormat, { fn new<P: AsRef<Path>, I: IntoIterator<Item = P> + Clone + 'static>( suffixes: I, search_paths: S, app_name: String, ) -> Self { let suffixes = suffixes .into_iter() .map(|name| PathBuf::from(name.as_ref())) .collect(); Self { format: PhantomData, search_paths, suffixes, app_name, } } } impl<S, F> ConfigurationSource for ConfigurationFileSources<S, F> where S: SearchPaths + Clone + 'static, F: ConfigurationFileFormat + 'static, { fn build<B: ConfigurationBuilder>(self, mut builder: B) -> Result<B> { let search_paths = self.search_paths; let suffixes = self.suffixes; let app_name = self.app_name; for file in search_paths.files(move |p| { let mut files = HashSet::new(); if p.is_bin() || p.is_cwd() || p.is_user() { for suffix in suffixes.iter() { files.insert( p.path() .join(format!("{}rc{}", app_name, suffix.to_str().unwrap())), ); files.insert( p.path() .join(format!(".{}rc{}", app_name, suffix.to_str().unwrap())), ); } } if p.is_app() { for suffix in suffixes.iter() { files.insert(p.path().join(format!("config{}", suffix.to_str().unwrap()))); } } files.into_iter() }) { let f = File::open(file)?; builder = builder.push_provider(F::get_config(f)?); } Ok(builder) } }