module_util/file/
format.rs

1use std::path::{Path, PathBuf};
2
3use module::Error;
4use serde::Deserialize;
5use serde::de::DeserializeOwned;
6
7use crate::evaluator::Imports;
8
9/// The top-level structure of a [`File`] module.
10///
11/// [`File`]: super::File
12#[derive(Debug, Default, Clone, Deserialize)]
13pub struct Module<T> {
14    /// Imports of the module.
15    ///
16    /// This field instructs [`File`] to additionally [`read()`] all modules
17    /// specified here.
18    ///
19    /// [`File`]: super::File
20    /// [`read()`]: super::File::read
21    #[serde(default)]
22    pub imports: Imports<PathBuf>,
23
24    /// Value of the module.
25    #[serde(flatten)]
26    pub value: T,
27}
28
29/// The format of a file.
30///
31/// The job of a [`Format`] is to read a file, parse it and convert it to a
32/// [`Module`] so it can be merged.
33///
34/// [`File`]: super::File
35pub trait Format {
36    /// Read the module at `path`.
37    ///
38    /// See [trait-level docs](Format) for more information.
39    fn read<T>(&mut self, path: &Path) -> Result<Module<T>, Error>
40    where
41        T: DeserializeOwned;
42}