Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 26, 2023
1 parent d39bff3 commit 7d6e3eb
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,14 @@ pub fn write_project_file(
router_user_file.write_all(router_user_rendered.as_bytes())?;

//src/router/static_routers.rs
let router_static_routers_template = include_str!("../template/src/routers/static_routers.hbs");
render_and_write_to_file(&handlebars, router_static_routers_template, &data, router_path.join("static_routers.rs"))?;
let router_static_routers_template =
include_str!("../template/src/routers/static_routers.hbs");
render_and_write_to_file(
&handlebars,
router_static_routers_template,
&data,
router_path.join("static_routers.rs"),
)?;
//src/services
let services_path = src_path.join("services");
std::fs::create_dir_all(&services_path)?;
Expand Down Expand Up @@ -592,13 +598,28 @@ fn create_basic_file(
std::fs::create_dir_all(&src_path)?;

let templates = [
("/src/main.rs", include_str!("../template/src/main_template.hbs")),
("/src/Cargo.toml", include_str!("../template/src/cargo_template.hbs")),
("/src/config.rs", include_str!("../template/src/config_template.hbs")),
("/src/app_error.rs", include_str!("../template/src/app_error.hbs")),
("/src/app_response.rs", include_str!("../template/src/app_response.hbs")),
(
"/src/main.rs",
include_str!("../template/src/main_template.hbs"),
),
(
"/src/Cargo.toml",
include_str!("../template/src/cargo_template.hbs"),
),
(
"/src/config.rs",
include_str!("../template/src/config_template.hbs"),
),
(
"/src/app_error.rs",
include_str!("../template/src/app_error.hbs"),
),
(
"/src/app_response.rs",
include_str!("../template/src/app_response.hbs"),
),
];

for (file_name, template) in &templates {
render_and_write_to_file(&handlebars, template, &data, project_path.join(file_name))?;
}
Expand Down Expand Up @@ -796,28 +817,27 @@ fn handle_dependencies(
}

fn render_and_write_to_file<T: AsRef<Path>>(
handlebars: &Handlebars,
template: &str,
data: &impl serde::Serialize,
file_path: T
handlebars: &Handlebars,
template: &str,
data: &impl serde::Serialize,
file_path: T,
) -> Result<()> {
// Render the template
let rendered = handlebars.render_template(template, data)?;

// Get the parent directory of the file
if let Some(parent) = file_path.as_ref().parent() {
// Create the parent directory if it doesn't exist
fs::create_dir_all(parent)?;
}

// Create the file and write the rendered template to it
let mut file = fs::File::create(file_path)?;
file.write_all(rendered.as_bytes())?;

Ok(())
}


fn check_name(name: &str) -> Result<()> {
restricted_names::validate_package_name(name, "package name")?;

Expand Down

0 comments on commit 7d6e3eb

Please sign in to comment.