Struct File

Source
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>

Source

pub fn new(format: F) -> Self

Create a new File that reads files according to format.

Source

pub fn format(&self) -> &F

Get a reference to the Format used.

Source

pub fn format_mut(&mut self) -> &mut F

Get a mutable reference to the Format used.

Source

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(_)));
Source§

impl<T, F> File<T, F>
where T: Merge + DeserializeOwned, F: Format,

Source

pub fn read<P>(&mut self, path: P) -> Result<(), Error>
where P: AsRef<Path>,

Read the module at path.

See the type-level docs for more information

Source§

impl<T> File<T, Json>

Source

pub fn json() -> Self

Available on crate feature json only.

Create a new File that reads Json files.

See: Json.

Equivalent to: File::new(Json::default())

Source§

impl<T> File<T, Toml>

Source

pub fn toml() -> Self

Available on crate feature toml only.

Create a new File that reads Toml files.

See: Toml.

Equivalent to: File::new(Toml::default())

Source§

impl<T> File<T, Yaml>

Source

pub fn yaml() -> Self

Available on crate feature yaml only.

Create a new File that reads Yaml files.

See: Yaml.

Equivalent to: File::new(Yaml::default())

Trait Implementations§

Source§

impl<T: Debug, F: Debug> Debug for File<T, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, F> Default for File<T, F>
where F: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for File<T, F>
where F: Freeze, T: Freeze,

§

impl<T, F> RefUnwindSafe for File<T, F>

§

impl<T, F> Send for File<T, F>
where F: Send, T: Send,

§

impl<T, F> Sync for File<T, F>
where F: Sync, T: Sync,

§

impl<T, F> Unpin for File<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for File<T, F>
where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.