libmake/generators/yaml.rs
1use crate::macro_generate_files;
2use crate::models::model_params::FileGenerationParams;
3use std::fs;
4use std::io;
5
6/// Generates files for a new Rust project based on a YAML file.
7///
8/// The YAML file must contain a single object with the following
9/// properties:
10///
11/// - `author` - the author of the project (optional).
12/// - `build` - the build command to be used for building the project (optional).
13/// - `categories` - the categories that the project belongs to (optional).
14/// - `description` - a short description of the project (optional).
15/// - `documentation` - the documentation URL of the project (optional).
16/// - `edition` - the edition of the project (optional).
17/// - `email` - the email address of the author (optional).
18/// - `homepage` - the homepage of the project (optional).
19/// - `keywords` - keywords that describe the project (optional).
20/// - `license` - the license under which the project is released (optional).
21/// - `name` - the name of the project (optional).
22/// - `output` - the output directory where the project files will be created (required).
23/// - `readme` - the name of the readme file (optional).
24/// - `repository` - the url of the project's repository (optional).
25/// - `rustversion` - the minimum Rust version required by the project (optional).
26/// - `version` - the initial version of the project (optional).
27/// - `website` - the website of the project (optional).
28///
29/// # Errors
30///
31/// This function will return an error in the following situations:
32///
33/// - If the specified YAML file cannot be found, read, or is not valid UTF-8.
34/// - If the YAML data cannot be deserialized into the `FileGenerationParams` struct.
35/// - If there is an error in generating files based on the parameters.
36///
37pub fn generate_from_yaml(path: &str) -> io::Result<()> {
38 let contents = fs::read_to_string(path)?;
39 let params: FileGenerationParams =
40 serde_yml::from_str(&contents)
41 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
42 macro_generate_files!(params.clone())
43 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
44 Ok(())
45}