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"
),
];
const BLOC: &[TemplateFile] = &[
file!("Cargo.toml", "bloc/Cargo.toml.tmpl"),
file!("build.rs", "bloc/build.rs"),
file!("clippy.toml", "bloc/clippy.toml"),
file!("rok-ui.toml", "bloc/rok-ui.toml"),
file!("src/main.rs", "bloc/src/main.rs"),
file!("src/lib.rs", "bloc/src/lib.rs"),
file!("src/app.rs", "bloc/src/app.rs"),
file!("src/data.rs", "bloc/src/data.rs"),
file!("src/features.rs", "bloc/src/features.rs"),
file!("src/shared.rs", "bloc/src/shared.rs"),
file!("src/shared/widgets.rs", "bloc/src/shared/widgets.rs"),
file!("src/routes/__root.rs", "bloc/src/routes/__root.rs"),
file!(
"src/routes/__not_found.rs",
"bloc/src/routes/__not_found.rs"
),
file!("src/routes/index.rs", "bloc/src/routes/index.rs"),
];
const BLOC_HTTP: &[TemplateFile] = &[
file!("Cargo.toml", "bloc-http/Cargo.toml.tmpl"),
file!("build.rs", "bloc/build.rs"),
file!("clippy.toml", "bloc/clippy.toml"),
file!("rok-ui.toml", "bloc/rok-ui.toml"),
file!("src/main.rs", "bloc-http/src/main.rs"),
file!("src/lib.rs", "bloc/src/lib.rs"),
file!("src/app.rs", "bloc-http/src/app.rs"),
file!("src/data.rs", "bloc/src/data.rs"),
file!("src/features.rs", "bloc/src/features.rs"),
file!("src/shared.rs", "bloc/src/shared.rs"),
file!("src/shared/widgets.rs", "bloc-http/src/shared/widgets.rs"),
file!("src/routes/__root.rs", "bloc-http/src/routes/__root.rs"),
file!(
"src/routes/__not_found.rs",
"bloc/src/routes/__not_found.rs"
),
file!("src/routes/index.rs", "bloc/src/routes/index.rs"),
];
const BLOC_WORKSPACE: &[TemplateFile] = &[
file!("Cargo.toml", "bloc-workspace/Cargo.toml.tmpl"),
file!("rok-ui.toml", "bloc-workspace/rok-ui.toml"),
file!(
"crates/{{name}}-data/Cargo.toml",
"bloc-workspace/data/Cargo.toml.tmpl"
),
file!("crates/{{name}}-data/src/lib.rs", "bloc/src/data.rs"),
file!(
"crates/{{name}}/Cargo.toml",
"bloc-workspace/app/Cargo.toml.tmpl"
),
file!("crates/{{name}}/build.rs", "bloc/build.rs"),
file!("crates/{{name}}/src/main.rs", "bloc/src/main.rs"),
file!("crates/{{name}}/src/lib.rs", "bloc-workspace/app/lib.rs"),
file!("crates/{{name}}/src/app.rs", "bloc/src/app.rs"),
file!("crates/{{name}}/src/features.rs", "bloc/src/features.rs"),
file!("crates/{{name}}/src/shared.rs", "bloc/src/shared.rs"),
file!(
"crates/{{name}}/src/shared/widgets.rs",
"bloc/src/shared/widgets.rs"
),
file!(
"crates/{{name}}/src/routes/__root.rs",
"bloc/src/routes/__root.rs"
),
file!(
"crates/{{name}}/src/routes/__not_found.rs",
"bloc/src/routes/__not_found.rs"
),
file!(
"crates/{{name}}/src/routes/index.rs",
"bloc/src/routes/index.rs"
),
];
const BLOC_HTTP_WORKSPACE: &[TemplateFile] = &[
file!("Cargo.toml", "bloc-workspace/Cargo.toml.tmpl"),
file!("rok-ui.toml", "bloc-workspace/rok-ui.toml"),
file!(
"crates/{{name}}-data/Cargo.toml",
"bloc-workspace/data/Cargo.toml.tmpl"
),
file!("crates/{{name}}-data/src/lib.rs", "bloc/src/data.rs"),
file!(
"crates/{{name}}/Cargo.toml",
"bloc-workspace/app/Cargo.toml.tmpl"
),
file!("crates/{{name}}/build.rs", "bloc/build.rs"),
file!("crates/{{name}}/src/main.rs", "bloc-http/src/main.rs"),
file!("crates/{{name}}/src/lib.rs", "bloc-workspace/app/lib.rs"),
file!("crates/{{name}}/src/app.rs", "bloc-http/src/app.rs"),
file!("crates/{{name}}/src/features.rs", "bloc/src/features.rs"),
file!("crates/{{name}}/src/shared.rs", "bloc/src/shared.rs"),
file!(
"crates/{{name}}/src/shared/widgets.rs",
"bloc-http/src/shared/widgets.rs"
),
file!(
"crates/{{name}}/src/routes/__root.rs",
"bloc-http/src/routes/__root.rs"
),
file!(
"crates/{{name}}/src/routes/__not_found.rs",
"bloc/src/routes/__not_found.rs"
),
file!(
"crates/{{name}}/src/routes/index.rs",
"bloc/src/routes/index.rs"
),
];
pub const BLOC_FEATURE: &str = include_str!("../fixtures/notes.json");
pub const BLOC_HTTP_FEATURES: &[&str] = &[
include_str!("../fixtures/notes_http.json"),
include_str!("../fixtures/auth.json"),
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Template {
Minimal,
Full,
Db,
Bloc,
BlocHttp,
BlocWorkspace,
BlocHttpWorkspace,
}
impl Template {
pub fn parse(name: &str) -> Option<Self> {
match name {
"minimal" => Some(Self::Minimal),
"full" => Some(Self::Full),
"db" => Some(Self::Db),
"bloc" => Some(Self::Bloc),
"bloc-http" => Some(Self::BlocHttp),
"bloc-workspace" => Some(Self::BlocWorkspace),
"bloc-http-workspace" => Some(Self::BlocHttpWorkspace),
_ => None,
}
}
pub fn features(self) -> &'static [&'static str] {
match self {
Self::Minimal => &[],
Self::Full => &["router", "query"],
Self::Db => &["router", "query", "db"],
Self::Bloc | Self::BlocWorkspace => &["router", "bloc", "form", "typography", "empty"],
Self::BlocHttp | Self::BlocHttpWorkspace => {
&["router", "bloc", "http", "form", "typography", "empty"]
}
}
}
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(),
Self::Bloc => BLOC.iter().collect(),
Self::BlocHttp => BLOC_HTTP.iter().collect(),
Self::BlocWorkspace => BLOC_WORKSPACE.iter().collect(),
Self::BlocHttpWorkspace => BLOC_HTTP_WORKSPACE.iter().collect(),
}
}
}