use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
include!(concat!(env!("OUT_DIR"), "/examples_data.rs"));
#[derive(Debug, Serialize, Deserialize)]
pub struct ExampleInfo {
pub name: String,
pub description: String,
pub template: String,
pub files: Vec<String>, }
#[derive(Debug, Serialize, Deserialize)]
struct ExamplesYaml {
examples: HashMap<String, ExampleInfo>,
}
pub fn get_available_examples() -> anyhow::Result<HashMap<String, ExampleInfo>> {
let yaml: ExamplesYaml = serde_yaml::from_str(EXAMPLES_YAML)?;
Ok(yaml.examples)
}
pub fn write_example_to_directory(example_name: &str, target_dir: &Path) -> anyhow::Result<()> {
let files = EXAMPLE_FILES
.get(example_name)
.ok_or_else(|| anyhow::anyhow!("Example '{}' not found", example_name))?;
for (dest_path, content) in files.iter() {
let dest_full_path = target_dir.join(dest_path);
if let Some(parent) = dest_full_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&dest_full_path, content)?;
}
Ok(())
}
pub fn list_examples() -> Vec<String> {
EXAMPLE_FILES.keys().map(|k| k.to_string()).collect()
}