style: 应用 rustfmt 格式化

整理 components、commands、entity 和 utils 中的 import 顺序、尾逗号、换行与文件末尾换行。

这保持代码风格与 rustfmt 输出一致,减少后续功能提交里的格式噪音。
This commit is contained in:
2026-05-09 22:02:15 +08:00
parent 405cdaab81
commit 02e72fc9d8
4 changed files with 47 additions and 50 deletions

View File

@@ -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<String>, texture: &Option<String>) -> 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(())
}

View File

@@ -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<String>,
/// The item's identifier
#[arg(short, long)]
pub identifier: Option<String>
pub identifier: Option<String>,
}

View File

@@ -12,4 +12,4 @@ pub struct ReleaseInfo {
pub resource_version: Vec<u32>,
pub behavior_identifier: String,
pub resource_identifier: String,
}
}

View File

@@ -7,9 +7,8 @@ use std::{
pub fn read_file_to_json(path: &PathBuf) -> Result<Value> {
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<()> {