Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Loads and returns the data from the specified file.
- ///
- /// # Parameters:
- ///
- /// * `conf_dir`: Directory relative to the executable in which to search for configuration.
- /// * `file_name`: Name of the file to search for relative to the executable.
- /// * `format`: File format.
- /// * `additional_base_dirs`: Additional base directories to look into. Useful at development time
- /// when configuration is generated and placed in a separate output directory.
- pub fn load_in<T, P>(
- conf_dir: P,
- file_name: &str,
- format: Format,
- additional_base_dirs: Option<Vec<PathBuf>>,
- ) -> Result<T>
- where
- for<'de> T: Deserialize<'de>,
- P: AsRef<Path> + AsRef<ffi::OsStr>,
- {
- let file_path = find_in(conf_dir, file_name, additional_base_dirs)?;
- let file_reader = File::open(file_path)?;
- match format {
- Format::Ron => {
- let result = from_reader(file_reader);
- match result {
- Ok(data) => Ok(data),
- Err(ron_de_error) => Err(Error::from_kind(ErrorKind::RonDeserialization(
- ron_de_error,
- ))),
- }
- }
- }
- }
- // === Caller === //
- fn load_in_returns_error_when_file_does_not_exist() {
- // We don't setup_temp_file(..);
- let load_result = load_in::<Result<Data>, _>(
- exe_dir(),
- "test__load_config.ron",
- Format::Ron,
- Some(development_base_dirs!()),
- );
- if let &ErrorKind::Find(ref find_context) = load_result.unwrap_err().kind() {
- let mut base_dirs = vec![exe_dir()];
- base_dirs.append(&mut development_base_dirs!());
- let expected = FindContext {
- base_dirs,
- conf_dir: PathBuf::from(""),
- file_name: "test__find_config.ron".to_owned(),
- }; // kcov-ignore
- assert_eq!(&expected, find_context);
- } else {
- panic!("Expected `load` to return error"); // kcov-ignore
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment