From 02e72fc9d8896950908dcb2445a05d57aa704b68 Mon Sep 17 00:00:00 2001 From: Blank038 Date: Sat, 9 May 2026 22:02:15 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E5=BA=94=E7=94=A8=20rustfmt=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 整理 components、commands、entity 和 utils 中的 import 顺序、尾逗号、换行与文件末尾换行。 这保持代码风格与 rustfmt 输出一致,减少后续功能提交里的格式噪音。 --- src/commands/components.rs | 86 +++++++++++++++++++------------------- src/commands/mod.rs | 4 +- src/entity/project.rs | 2 +- src/utils/file.rs | 5 +-- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/commands/components.rs b/src/commands/components.rs index 514cf47..b13104c 100644 --- a/src/commands/components.rs +++ b/src/commands/components.rs @@ -1,7 +1,7 @@ use crate::commands::ComponentsArgs; use crate::entity; -use crate::utils::file; use crate::error::Result; +use crate::utils::file; use serde_json::{json, to_string_pretty}; use std::fs; use std::path::PathBuf; @@ -18,40 +18,43 @@ pub fn execute(args: &ComponentsArgs) { fn run_components(args: &ComponentsArgs) -> Result<()> { let project_path = file::find_project_dir(&args.path)?; - + validate_input_files(&args.geo, &args.texture)?; - + let identifier = args.identifier.as_deref().unwrap_or("unknown"); - + match args.component.as_str() { COMPONENT_3D_ITEM => create_3dmodel( args.geo.as_deref().unwrap_or("./model.geo.json"), args.texture.as_deref().unwrap_or("./texture.png"), identifier, - &project_path + &project_path, ), - _ => Err(crate::error::CliError::NotFound( - format!("组件 '{}' 不存在", args.component) - )), + _ => Err(crate::error::CliError::NotFound(format!( + "组件 '{}' 不存在", + args.component + ))), } } fn validate_input_files(geo: &Option, texture: &Option) -> Result<()> { let geo_path = geo.as_deref().unwrap_or("./model.geo.json"); let texture_path = texture.as_deref().unwrap_or("./texture.png"); - + if !PathBuf::from(geo_path).exists() { - return Err(crate::error::CliError::NotFound( - format!("几何文件 {} 不存在", geo_path) - )); + return Err(crate::error::CliError::NotFound(format!( + "几何文件 {} 不存在", + geo_path + ))); } - + if !PathBuf::from(texture_path).exists() { - return Err(crate::error::CliError::NotFound( - format!("材质文件 {} 不存在", texture_path) - )); + return Err(crate::error::CliError::NotFound(format!( + "材质文件 {} 不存在", + texture_path + ))); } - + Ok(()) } @@ -62,7 +65,7 @@ fn create_3dmodel( project_path: &PathBuf, ) -> Result<()> { let project_info = entity::get_current_release_info(&project_path)?; - + let beh_path = project_path.join(format!( "behavior_pack_{}", project_info.behavior_identifier @@ -71,32 +74,32 @@ fn create_3dmodel( "resource_pack_{}", project_info.resource_identifier )); - + create_item_files(&beh_path, &res_path, identifier)?; copy_assets(&res_path, geo, texture, identifier)?; create_attachable_file(&res_path, identifier)?; - + Ok(()) } fn create_item_files(beh_path: &PathBuf, res_path: &PathBuf, identifier: &str) -> Result<()> { let behavior_item = create_behavior_item_json(identifier); let resource_item = create_resource_item_json(identifier); - + let f_identifier = identifier.replace(":", "_"); - + let items_beh_dir = beh_path.join("netease_items_beh"); let items_res_dir = res_path.join("netease_items_res"); - + fs::create_dir_all(&items_beh_dir)?; fs::create_dir_all(&items_res_dir)?; - + let beh_item_path = items_beh_dir.join(format!("{}.json", f_identifier)); let res_item_path = items_res_dir.join(format!("{}.json", f_identifier)); - + fs::write(&beh_item_path, to_string_pretty(&behavior_item)?)?; fs::write(&res_item_path, to_string_pretty(&resource_item)?)?; - + Ok(()) } @@ -137,27 +140,22 @@ fn create_resource_item_json(identifier: &str) -> serde_json::Value { }) } -fn copy_assets( - res_path: &PathBuf, - geo: &str, - texture: &str, - identifier: &str, -) -> Result<()> { +fn copy_assets(res_path: &PathBuf, geo: &str, texture: &str, identifier: &str) -> Result<()> { let f_identifier = identifier.replace(":", "_"); - + copy_texture(res_path, texture, &f_identifier)?; copy_geometry(res_path, geo, identifier, &f_identifier)?; - + Ok(()) } fn copy_texture(res_path: &PathBuf, texture: &str, f_identifier: &str) -> Result<()> { let texture_dir = res_path.join("textures/models"); fs::create_dir_all(&texture_dir)?; - + let target_texture = texture_dir.join(format!("{}.png", f_identifier)); fs::copy(texture, target_texture)?; - + Ok(()) } @@ -169,26 +167,26 @@ fn copy_geometry( ) -> Result<()> { let geo_dir = res_path.join("models/entity"); fs::create_dir_all(&geo_dir)?; - + let mut geo_value = file::read_file_to_json(&PathBuf::from(geo))?; - + let geo_name = format!("geometry.{}", identifier.replace(":", ".")); geo_value["format_version"] = json!("1.12.0"); geo_value["minecraft:geometry"][0]["description"]["identifier"] = json!(geo_name); - + let target_geo = geo_dir.join(format!("{}.geo.json", f_identifier)); file::write_json_to_file(&target_geo, &geo_value)?; - + Ok(()) } fn create_attachable_file(res_path: &PathBuf, identifier: &str) -> Result<()> { let attachable_dir = res_path.join("attachables"); fs::create_dir_all(&attachable_dir)?; - + let f_identifier = identifier.replace(":", "_"); let geo_name = identifier.replace(":", "."); - + let attachable = json!({ "format_version": "1.10.0", "minecraft:attachable": { @@ -214,9 +212,9 @@ fn create_attachable_file(res_path: &PathBuf, identifier: &str) -> Result<()> { } } }); - + let target_file = attachable_dir.join(format!("{}.json", &f_identifier)); fs::write(target_file, to_string_pretty(&attachable)?)?; - + Ok(()) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 30c2f8c..0f7258d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,4 @@ -use clap::{arg, Args, Parser, Subcommand}; +use clap::{Args, Parser, Subcommand, arg}; pub mod components; pub mod create; @@ -68,5 +68,5 @@ pub struct ComponentsArgs { pub texture: Option, /// The item's identifier #[arg(short, long)] - pub identifier: Option + pub identifier: Option, } diff --git a/src/entity/project.rs b/src/entity/project.rs index cb1ea59..350e7b1 100644 --- a/src/entity/project.rs +++ b/src/entity/project.rs @@ -12,4 +12,4 @@ pub struct ReleaseInfo { pub resource_version: Vec, pub behavior_identifier: String, pub resource_identifier: String, -} \ No newline at end of file +} diff --git a/src/utils/file.rs b/src/utils/file.rs index 71519b6..07be841 100644 --- a/src/utils/file.rs +++ b/src/utils/file.rs @@ -7,9 +7,8 @@ use std::{ pub fn read_file_to_json(path: &PathBuf) -> Result { let content = fs::read_to_string(path).map_err(|e| io_error("读取文件", path, e))?; - serde_json::from_str(&content).map_err(|e| { - CliError::InvalidData(format!("解析 JSON '{}' 失败: {}", path.display(), e)) - }) + serde_json::from_str(&content) + .map_err(|e| CliError::InvalidData(format!("解析 JSON '{}' 失败: {}", path.display(), e))) } pub fn write_json_to_file(path: &PathBuf, value: &Value) -> Result<()> {