pub struct TemplateFile {
pub path: &'static str,
pub text: &'static str,
}
macro_rules! file {
($path:literal, $source:literal) => {
TemplateFile {
path: $path,
text: include_str!(concat!("../templates/", $source)),
}
};
}
const MINIMAL: &[TemplateFile] = &[
file!("Cargo.toml", "minimal/Cargo.toml.tmpl"),
file!("src/main.rs", "minimal/src/main.rs"),
];
const FULL_SHARED: &[TemplateFile] = &[
file!("Cargo.toml", "full/Cargo.toml.tmpl"),
file!("build.rs", "full/build.rs"),
file!("rok-ui.toml", "full/rok-ui.toml"),
file!("src/lib.rs", "full/src/lib.rs"),
file!("src/components.rs", "full/src/components.rs"),
file!("src/components/ui.rs", "full/src/components/ui.rs"),
file!("src/features.rs", "full/src/features.rs"),
file!("src/routes/__root.rs", "full/src/routes/__root.rs"),
file!(
"src/routes/__not_found.rs",
"full/src/routes/__not_found.rs"
),
file!("src/routes/index.rs", "full/src/routes/index.rs"),
file!("src/routes/notes.rs", "full/src/routes/notes.rs"),
file!(
"src/routes/notes/index.rs",
"full/src/routes/notes/index.rs"
),
file!("src/routes/notes/$id.rs", "full/src/routes/notes/$id.rs"),
file!("tests/routes.rs", "full/tests/routes.rs"),
];
const FULL_ONLY: &[TemplateFile] = &[
file!("src/main.rs", "full/src/main.rs"),
file!("src/features/notes.rs", "full/src/features/notes.rs"),
];
const DB_ONLY: &[TemplateFile] = &[
file!("src/main.rs", "db/main.rs"),
file!("src/features/notes.rs", "db/notes.rs"),
file!(
"migrations/0001_create_notes.sql",
"db/migrations/0001_create_notes.sql"
),
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Template {
Minimal,
Full,
Db,
}
impl Template {
pub fn parse(name: &str) -> Option<Self> {
match name {
"minimal" => Some(Self::Minimal),
"full" => Some(Self::Full),
"db" => Some(Self::Db),
_ => None,
}
}
pub fn features(self) -> &'static [&'static str] {
match self {
Self::Minimal => &[],
Self::Full => &["router", "query"],
Self::Db => &["router", "query", "db"],
}
}
pub fn uses_build_script(self) -> bool {
self != Self::Minimal
}
pub fn files(self) -> Vec<&'static TemplateFile> {
match self {
Self::Minimal => MINIMAL.iter().collect(),
Self::Full => FULL_SHARED.iter().chain(FULL_ONLY).collect(),
Self::Db => FULL_SHARED.iter().chain(DB_ONLY).collect(),
}
}
}