add docs for Config::load()
All checks were successful
CI/CD Pipeline / test (push) Successful in 1m57s

This commit is contained in:
philipp 2024-02-27 08:30:42 +01:00
parent f2e9a36e64
commit 497bc87160

View File

@ -55,6 +55,43 @@ pub struct Config {
} }
impl Config { impl Config {
/// Loads a configuration from a specified path and constructs a `LawBuilder` and `Parser` based on it.
///
/// This function reads a configuration file from the given path, expecting it to be in TOML format. It then
/// parses the configuration to set up a `LawBuilder` with specified classifiers and a `Parser` with specific
/// string manipulation rules (like removing or replacing strings). Additionally, it processes parser settings
/// for moving paragraph headers into content if specified.
///
/// # Parameters
///
/// - `path`: A path to the configuration file. This can be any type that implements the `AsRef<Path>` trait,
/// allowing for flexible path specifications (e.g., `&str`, `String`, `Path`, or `PathBuf`).
///
/// # Returns
///
/// Returns a `Result` containing a tuple of the law ID (`usize`), the constructed `LawBuilder`, and the `Parser`
/// upon successful operation. If any error occurs during the process (e.g., file reading, TOML parsing, classifier
/// creation), it returns an `Error`.
///
/// # Errors
///
/// This function can return an `Error` in several cases:
///
/// - If the specified path does not exist or cannot be read.
/// - If the configuration file content is not valid TOML or does not conform to the expected structure.
/// - If there's an issue creating any of the classifiers specified in the configuration (e.g., if the `match_function`
/// for a classifier fails).
///
/// # Examples
///
/// ```
/// use risp::config::Config;
/// use std::path::Path;
///
/// let (law_id, builder, parser) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
///
/// assert_eq!(law_id, 10001622);
/// ```
pub fn load<P: AsRef<Path>>(path: P) -> Result<(usize, LawBuilder, Parser), Error> { pub fn load<P: AsRef<Path>>(path: P) -> Result<(usize, LawBuilder, Parser), Error> {
let config_str = fs::read_to_string(path)?; let config_str = fs::read_to_string(path)?;
let config: Config = toml::from_str(&config_str)?; let config: Config = toml::from_str(&config_str)?;