create_rspc_app/
database.rs

1use std::{fs::create_dir_all, io, path::Path, str::FromStr};
2
3use include_dir::{include_dir, Dir};
4use strum::EnumIter;
5
6use crate::{framework::Framework, utils::replace_in_file};
7
8static BASE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/base");
9static PCR_BASE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/pcr_base");
10static AXUM_BASE_TEMPLATE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/axum_pcr_base");
11static TAURI_BASE_TEMPLATE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/tauri_pcr_base");
12
13#[derive(Debug, Clone, EnumIter, PartialEq, Eq)]
14pub enum Database {
15    PrismaClientRust,
16    None,
17}
18
19impl ToString for Database {
20    fn to_string(&self) -> String {
21        match self {
22            Self::PrismaClientRust => "Prisma Client Rust",
23            Self::None => "None",
24        }
25        .to_string()
26    }
27}
28
29impl FromStr for Database {
30    type Err = String;
31
32    fn from_str(s: &str) -> Result<Self, Self::Err> {
33        match s {
34            "Prisma Client Rust" => Ok(Self::PrismaClientRust),
35            "None" => Ok(Self::None),
36            _ => Err(format!("{} is not a valid database", s)),
37        }
38    }
39}
40
41impl Database {
42    pub fn render(&self, path: &Path, project_name: &str, framework: Framework) -> io::Result<()> {
43        match framework {
44            Framework::Tauri => {
45                create_dir_all(path).unwrap();
46                BASE.extract(path)?;
47                TAURI_BASE_TEMPLATE.extract(path)?;
48                PCR_BASE.extract(path)?;
49
50                replace_in_file(
51                    path.join("src-tauri").join("Cargo__toml").as_path(),
52                    "__name__",
53                    project_name,
54                )?;
55            }
56            Framework::Axum => {
57                create_dir_all(path).unwrap();
58                BASE.extract(path)?;
59                AXUM_BASE_TEMPLATE.extract(path)?;
60                PCR_BASE.extract(path)?;
61
62                replace_in_file(
63                    path.join("api").join("Cargo__toml").as_path(),
64                    "__name__",
65                    project_name,
66                )?;
67            }
68        }
69
70        Ok(())
71    }
72}