rok-ui-cli 0.9.2

cargo rok-ui: create rok-ui apps, generate BLoC features, vendor components and write route trees.
//! Project templates for `cargo rok-ui new`.

/// A file of a template: its path in the new project (which may hold `{{name}}`) and its
/// text, with `{{name}}`, `{{crate_name}}`, `{{rok_ui}}`, `{{rok_ui_bloc}}`,
/// `{{rok_ui_http}}` and `{{rok_ui_build}}` placeholders.
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"
    ),
];

/// The feature the `bloc` template generates into a new app.
pub const BLOC_FEATURE: &str = include_str!("../fixtures/notes.json");

/// The features the `bloc-http` template generates: notes over an API, and signing in.
pub const BLOC_HTTP_FEATURES: &[&str] = &[
    include_str!("../fixtures/notes_http.json"),
    include_str!("../fixtures/auth.json"),
];

/// A project template.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Template {
    /// One window with a counter.
    Minimal,
    /// File-based routes, features, queries and a test.
    Full,
    /// `Full` with `PostgreSQL` through rok-db.
    Db,
    /// The BLoC architecture: data, features (bloc + view) and thin routes, with a generated
    /// `notes` feature.
    Bloc,
    /// `Bloc` over an HTTP API: a `Session` and an `HttpClient` in `main`, a session guard,
    /// and generated `notes` and `auth` features.
    BlocHttp,
    /// `Bloc` split into crates: data, one per feature's business logic (no GPUI), and the
    /// app.
    BlocWorkspace,
    /// `BlocHttp` split into crates, like `BlocWorkspace`; the data crate uses rok-ui-http.
    BlocHttpWorkspace,
}

impl Template {
    /// Parse a template name.
    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,
        }
    }

    /// The rok-ui features the template needs.
    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"]
            }
        }
    }

    /// Whether the template generates routes in `build.rs`.
    pub fn uses_build_script(self) -> bool {
        self != Self::Minimal
    }

    /// The template's files.
    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(),
        }
    }
}