pub struct File<T, F> { /* private fields */ }
Expand description
An evaluator for files.
This evaluator reads modules from files of a specific format. It uses
Module
as the top-level format of the module and serde
to parse the
contents of the file.
-
File
is capable of detecting import-cycles between modules. -
Import paths are resolved relative to the path of the importer module.
§Example
use module::Merge;
use serde::Deserialize;
#[derive(Deserialize, Merge)]
struct Config {
key: String,
items: Vec<i32>,
}
let mut file = File::json();
// `config.json`:
// --------------
// {
// "key": "424242",
// "items": [1]
// }
assert!(file.read("config.json").is_ok());
// `config-extra.json`:
// --------------------
// {
// "items": [3, 6, 0]
// }
assert!(file.read("config-extra.json").is_ok());
let config: Config = file.finish().unwrap();
assert_eq!(config.key, "424242");
assert_eq!(config.items, &[1, 3, 6, 0]);
Implementations§
Source§impl<T, F> File<T, F>
impl<T, F> File<T, F>
Sourcepub fn format_mut(&mut self) -> &mut F
pub fn format_mut(&mut self) -> &mut F
Get a mutable reference to the Format
used.
Sourcepub fn finish(self) -> Option<T>
pub fn finish(self) -> Option<T>
Finish the evaluation and return the final value.
Returns None
if no file has been read()
successfully. Otherwise,
it returns Some(value)
.
§Example
let mut file = File::json();
assert_eq!(file.finish(), None);
let mut file = File::json();
assert!(file.read("non_existent.json").is_err());
assert_eq!(file.finish(), None);
let mut file = File::json();
assert!(file.read("exists.json").is_ok());
assert!(matches!(file.finish(), Some(_)));
Trait Implementations§
Auto Trait Implementations§
impl<T, F> Freeze for File<T, F>
impl<T, F> RefUnwindSafe for File<T, F>where
F: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, F> Send for File<T, F>
impl<T, F> Sync for File<T, F>
impl<T, F> Unpin for File<T, F>
impl<T, F> UnwindSafe for File<T, F>where
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more