use ggen_core::utils::error::Result;
use std::fs;
use std::path::Path;
use super::ConventionPreset;
pub struct ClapNounVerbPreset;
impl ConventionPreset for ClapNounVerbPreset {
fn name(&self) -> &str {
"clap-noun-verb"
}
fn create_structure(&self, root: &Path) -> Result<()> {
let dirs = [
".ggen/rdf",
".ggen/templates/clap-noun-verb",
"src/cmds",
"src/domain",
];
for dir in &dirs {
fs::create_dir_all(root.join(dir))?;
}
fs::write(root.join(".ggen/convention.toml"), self.config_content())?;
for (path, content) in self.rdf_files() {
fs::write(root.join(".ggen/rdf").join(path), content)?;
}
for (path, content) in self.templates() {
fs::write(root.join(".ggen/templates").join(path), content)?;
}
Ok(())
}
fn rdf_files(&self) -> Vec<(&str, &str)> {
vec![(
"example_command.rdf",
include_str!("../../templates/rdf/example_command.rdf"),
)]
}
fn templates(&self) -> Vec<(&str, &str)> {
vec![
(
"clap-noun-verb/command.rs.tera",
include_str!("../../templates/clap-noun-verb/command.rs.tera"),
),
(
"clap-noun-verb/domain.rs.tera",
include_str!("../../templates/clap-noun-verb/domain.rs.tera"),
),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_create_structure() {
let temp = TempDir::new().unwrap();
let root = temp.path();
let preset = ClapNounVerbPreset;
preset.create_structure(root).unwrap();
assert!(root.join(".ggen/rdf").exists());
assert!(root.join(".ggen/templates/clap-noun-verb").exists());
assert!(root.join(".ggen/convention.toml").exists());
assert!(root.join("src/cmds").exists());
assert!(root.join("src/domain").exists());
}
}