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
//! Load trait.

use crate::err::Error;
use std::path::Path;

/// Types implementing this trait can be loaded from a file.
pub trait Load
where
    Self: std::marker::Sized,
{
    /// Report the opening of a fil (if it is a filepath) and load the data.
    /// # Errors
    /// if the target file can not be found,
    /// or the read string can not be serialised into an instance of the required type.
    #[inline]
    fn load(path: &Path) -> Result<Self, Error> {
        if path.is_file() {
            println!("Loading file: {}", path.display());
        }

        Self::load_data(path)
    }

    /// Deserialize the type from a given file.
    /// # Errors
    /// if the target file can not be found,
    /// or the read string can not be serialised into an instance of the required type.
    fn load_data(path: &Path) -> Result<Self, Error>;
}