refactor: 添加数据集元数据加载和保存功能,重构相关逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::config::{AppConfig, AppConfigState, ProxyKind};
|
||||
use crate::dataset_meta::{load_dataset_meta, save_dataset_meta, DatasetMeta};
|
||||
use tauri::State;
|
||||
use tauri::{AppHandle, WebviewWindow};
|
||||
|
||||
@@ -55,6 +56,16 @@ pub fn list_proxy_kind_options() -> Vec<SelectOption> {
|
||||
]
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn load_dataset_meta_by_dir(dataset_dir: String) -> Result<DatasetMeta, String> {
|
||||
load_dataset_meta(&dataset_dir).map_err(|e| format!("读取数据集元数据失败: {e}"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_dataset_meta_by_dir(dataset_dir: String, meta: DatasetMeta) -> Result<(), String> {
|
||||
save_dataset_meta(&dataset_dir, &meta).map_err(|e| format!("保存数据集元数据失败: {e}"))
|
||||
}
|
||||
|
||||
fn proxy_kind_value(kind: &ProxyKind) -> &'static str {
|
||||
match kind {
|
||||
ProxyKind::Http => "Http",
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
const DATASET_META_FILE: &str = "meta.toml";
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DatasetMeta {
|
||||
pub name: String,
|
||||
pub target_model: String,
|
||||
pub lora_type: String,
|
||||
pub unified_image_size: bool,
|
||||
pub unified_image_ratio: bool,
|
||||
pub image_size: [u32; 2],
|
||||
pub trigger_words: Vec<String>,
|
||||
}
|
||||
|
||||
impl DatasetMeta {
|
||||
pub fn validate(&self) -> anyhow::Result<()> {
|
||||
if self.name.trim().is_empty() {
|
||||
anyhow::bail!("数据集名称不能为空")
|
||||
}
|
||||
|
||||
if self.target_model.trim().is_empty() {
|
||||
anyhow::bail!("目标模型不能为空")
|
||||
}
|
||||
|
||||
if self.image_size[0] == 0 || self.image_size[1] == 0 {
|
||||
anyhow::bail!("imageSize 必须为正整数")
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn dataset_meta_path(dataset_dir: &str) -> PathBuf {
|
||||
PathBuf::from(dataset_dir).join(DATASET_META_FILE)
|
||||
}
|
||||
|
||||
pub fn save_dataset_meta(dataset_dir: &str, meta: &DatasetMeta) -> anyhow::Result<()> {
|
||||
meta.validate()?;
|
||||
|
||||
let meta_path = dataset_meta_path(dataset_dir);
|
||||
if let Some(parent_dir) = meta_path.parent() {
|
||||
fs::create_dir_all(parent_dir)?;
|
||||
}
|
||||
|
||||
let content = toml::to_string_pretty(meta)?;
|
||||
fs::write(meta_path, content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_dataset_meta(dataset_dir: &str) -> anyhow::Result<DatasetMeta> {
|
||||
let meta_path = dataset_meta_path(dataset_dir);
|
||||
if !meta_path.exists() {
|
||||
anyhow::bail!("缺少 meta.toml")
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(meta_path)?;
|
||||
let meta: DatasetMeta = toml::from_str(&content)?;
|
||||
meta.validate()?;
|
||||
Ok(meta)
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod commands;
|
||||
mod config;
|
||||
mod dataset_meta;
|
||||
|
||||
use tauri::Manager;
|
||||
|
||||
@@ -19,7 +20,9 @@ pub fn run() {
|
||||
commands::set_window_title,
|
||||
commands::load_app_config,
|
||||
commands::save_app_config,
|
||||
commands::list_proxy_kind_options
|
||||
commands::list_proxy_kind_options,
|
||||
commands::load_dataset_meta_by_dir,
|
||||
commands::save_dataset_meta_by_dir
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user