feat(packui): M113 第一批 —— 内容库页的导入/导出区(界面 + DTO + i18n + 拖拽落点)
导入入口(拖拽 + 路径输入 + 目标实例)与导出(格式选择 + 预检)的**界面层**,
数据面镜像 M111/M112 的管线报告(presenter DTO),进度整块复用下载页的安装面板。
## 落了什么
- pages/content/packui*:入口 / 进度块 / 结果块 / 导出预检块(每文件 ≤400)
- 拖拽区(hover 变主色描边)+ 路径输入框 + 目标实例 chips
- 进度:一行「步骤 n/5 · 解包覆盖内容」+ **下载页那块安装面板**(不另画进度条)
- 结果:三态(Success/Partial/Failed)+ 缺文件逐条(Warn/Tertiary)+ 冲突逐条
(成因 + 本次处置 + 可展开三方 SHA-1)+ 致命块(Error/ErrorContainer)
- 导出:mrpack / CF 二选一 + 包名/版本/路径 + 预检「将内联 N 个文件 · 无法导出 M 项」
+ 逐条无法导出项 + 已知损失 + 确认写盘
- presenter/src/dto/pack{,/format,/import,/conflict,/export}.rs:镜像 core 报告的 DTO
(三态、五阶段、三分类、十一条原因,全部 from_slug 与 core 稳定码逐字对齐)
- i18n:81 条 Pack* 键(content_entries! 尾部追加;golden.rs 同序同文)
- app/update.rs:FileDropped / FileHovered / FilesHoveredLeft → 内容库页(本版唯一的选文件通道)
- M109 的四个文件各一处插入:mod 声明 + Pack(..) 变体 + pack 字段 + 正列插入点
## 边界(如实)
- 没有系统文件选择框:依赖树里无对话框依赖,加它要动根 Cargo.toml + 许可门禁 +
Linux 后端选型(影响单文件产物)。入口是拖拽 + 路径输入,原因写在界面上。
- 冲突没有逐文件决策:core 的 ConflictPolicy 是整次导入一个全局档,ConflictFile 是事后账。
本批给「逐文件看 + 整体翻档(以包为准重新导入)」,文案明写「两者都留(改名)暂不支持」。
默认一律 KeepUser——绝不默认覆盖用户改过的文件。
- 本批按钮半通:拖拽/路径/格式/实例/展开都真生效;「点导入真的开跑」「预检/确认写盘」
要等第二批的 AppCommand 与事件通道(M109 rebase 到新 main 后按新形状写一次)。
This commit is contained in:
1 parent
0367073d32
commit
fbeff94e55
31 files changed
+3646
-9
No files matched your search
@@ -0,0 +1,228 @@
|
||||
//! 导出:格式选择 + **预检**(不写盘)。
|
||||
//!
|
||||
//! ## 预检与写盘是两次调用,同一套判据
|
||||
//!
|
||||
//! [`Preview`] 只扫描分类、不写盘;确认之后才是真导出。任务书要的
|
||||
//! 「将内联 N 个文件 / 无法导出 M 项」正是 [`Preview::head`]。
|
||||
//!
|
||||
//! 三分类的口径(与 core 一致,**没有第四类**):
|
||||
//!
|
||||
//! | 类 | 含义 | 预检里怎么呈现 |
|
||||
//! |---|---|---|
|
||||
//! | `Remote` | 能证明可被远程替代 | 一行计数 |
|
||||
//! | `Inline` | 证明不了 ⇒ 原字节进 `overrides/` | 计数 + 字节 + 前几条样例 |
|
||||
//! | `Skipped` | 压根没进包 | **逐条列出**(包不完整,不许合并成一句「已导出」) |
|
||||
//!
|
||||
//! `Skipped` 非零时 [`Preview::complete`] 为假,界面上会明确写「有文件不会进包」——
|
||||
//! 这是 M112 那句「不认识的 mod 不能丢、不能编」的用户侧落点:丢了就要看见丢在哪。
|
||||
|
||||
use unlucky_i18n::{Catalog, Key};
|
||||
use unlucky_presenter::dto::{
|
||||
human_bytes, ExportClassView, ExportFileView, ExportPreviewView, ExportReasonView, ExportState,
|
||||
PackFormatView,
|
||||
};
|
||||
|
||||
use super::{PackPageState, PackTarget, Row};
|
||||
|
||||
/// 预检里「内联文件样例」最多列几条(其余用一句「还有 N 个」收口)。
|
||||
const INLINE_SAMPLE: usize = 6;
|
||||
|
||||
/// 预检块(**还没写盘**)。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Preview {
|
||||
/// 一句话主结论:`将内联 12 个文件 · 无法导出 0 项`
|
||||
pub head: String,
|
||||
/// 远程引用行
|
||||
pub remote_line: String,
|
||||
/// 内联字节行
|
||||
pub inline_line: String,
|
||||
/// 落盘路径行
|
||||
pub target_line: String,
|
||||
/// 包会完整吗(`false` = 有文件压根没进包)
|
||||
pub complete: bool,
|
||||
/// 完整性那句话
|
||||
pub complete_line: String,
|
||||
/// 无法导出的项(逐条)
|
||||
pub skipped: Vec<Row>,
|
||||
/// 内联文件样例
|
||||
pub inline_sample: Vec<String>,
|
||||
/// `还有 N 个文件…`
|
||||
pub more_line: Option<String>,
|
||||
/// core 的 `notes`(已知损失)
|
||||
pub notes: Vec<String>,
|
||||
}
|
||||
|
||||
/// 导出块。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Export {
|
||||
/// 面板开着吗
|
||||
pub open: bool,
|
||||
/// 当前格式
|
||||
pub format: PackFormatView,
|
||||
/// 格式人读名
|
||||
pub format_label: &'static str,
|
||||
/// 可选的格式(只有两个:mrpack / CF)
|
||||
pub formats: Vec<(PackFormatView, &'static str)>,
|
||||
/// 目标实例候选
|
||||
pub targets: Vec<PackTarget>,
|
||||
/// 选中的实例
|
||||
pub instance: Option<unlucky_presenter::dto::InstanceId>,
|
||||
/// 包名
|
||||
pub name: String,
|
||||
/// 版本
|
||||
pub version: String,
|
||||
/// 落盘路径草稿(空 = 缺省)
|
||||
pub output: String,
|
||||
/// MultiMC/Prism 只导入不导出(如实写)
|
||||
pub format_note: &'static str,
|
||||
/// 预检在飞
|
||||
pub pending: bool,
|
||||
/// 预检的进行中说明
|
||||
pub pending_line: &'static str,
|
||||
/// 预检结果
|
||||
pub preview: Option<Preview>,
|
||||
/// 写盘完成行
|
||||
pub done: Option<String>,
|
||||
/// 失败原因
|
||||
pub failed: Option<String>,
|
||||
}
|
||||
|
||||
/// 导出一块。
|
||||
pub(super) fn export_of(
|
||||
state: &ExportState,
|
||||
page: &PackPageState,
|
||||
targets: Vec<PackTarget>,
|
||||
catalog: Catalog,
|
||||
) -> Export {
|
||||
let instance = page
|
||||
.instance
|
||||
.clone()
|
||||
.or_else(|| targets.first().map(|target| target.id.clone()));
|
||||
let formats = PackFormatView::EXPORTABLE
|
||||
.iter()
|
||||
.map(|format| (*format, format_label(*format, catalog)))
|
||||
.collect();
|
||||
|
||||
Export {
|
||||
open: page.export_open,
|
||||
format: page.format,
|
||||
format_label: format_label(page.format, catalog),
|
||||
formats,
|
||||
targets,
|
||||
instance,
|
||||
name: page.export_name.clone(),
|
||||
version: page.export_version.clone(),
|
||||
output: page.output.clone(),
|
||||
format_note: catalog.text(Key::PackExportMultimcNote),
|
||||
pending: state.pending,
|
||||
pending_line: catalog.text(Key::PackExportPending),
|
||||
preview: state
|
||||
.preview
|
||||
.as_ref()
|
||||
.map(|preview| preview_of(preview, catalog)),
|
||||
done: state.done.as_ref().map(|done| {
|
||||
catalog.render(
|
||||
Key::PackExportDone,
|
||||
&[
|
||||
("path", &done.path.display().to_string()),
|
||||
("bytes", &human_bytes(done.bytes)),
|
||||
],
|
||||
)
|
||||
}),
|
||||
failed: state.failed.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn preview_of(preview: &ExportPreviewView, catalog: Catalog) -> Preview {
|
||||
let skipped: Vec<Row> = preview
|
||||
.files
|
||||
.iter()
|
||||
.filter(|file| file.class == ExportClassView::Skipped)
|
||||
.map(|file| skipped_row(file, catalog))
|
||||
.collect();
|
||||
let inline: Vec<&ExportFileView> = preview
|
||||
.files
|
||||
.iter()
|
||||
.filter(|file| file.class == ExportClassView::Inline)
|
||||
.collect();
|
||||
let inline_sample = inline
|
||||
.iter()
|
||||
.take(INLINE_SAMPLE)
|
||||
.map(|file| file.path.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let more_line = (inline.len() > inline_sample.len()).then(|| {
|
||||
catalog.render(
|
||||
Key::PackPreviewMore,
|
||||
&[("count", &(inline.len() - inline_sample.len()).to_string())],
|
||||
)
|
||||
});
|
||||
|
||||
Preview {
|
||||
head: catalog.render(
|
||||
Key::PackPreviewHead,
|
||||
&[
|
||||
("inline", &preview.inline_count().to_string()),
|
||||
("skipped", &preview.skipped_count().to_string()),
|
||||
],
|
||||
),
|
||||
remote_line: catalog.render(
|
||||
Key::PackPreviewRemote,
|
||||
&[("count", &preview.remote_count().to_string())],
|
||||
),
|
||||
inline_line: catalog.render(
|
||||
Key::PackPreviewInlineBytes,
|
||||
&[("bytes", &human_bytes(preview.inline_bytes()))],
|
||||
),
|
||||
target_line: catalog.render(
|
||||
Key::PackPreviewTarget,
|
||||
&[("path", &preview.target.display().to_string())],
|
||||
),
|
||||
complete: preview.is_complete(),
|
||||
complete_line: catalog
|
||||
.text(if preview.is_complete() {
|
||||
Key::PackPreviewComplete
|
||||
} else {
|
||||
Key::PackPreviewIncomplete
|
||||
})
|
||||
.to_owned(),
|
||||
skipped,
|
||||
inline_sample,
|
||||
more_line,
|
||||
notes: preview.notes.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn skipped_row(file: &ExportFileView, catalog: Catalog) -> Row {
|
||||
Row {
|
||||
path: file.path.clone(),
|
||||
note: reason_label(file.reason, catalog).to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 导出归类原因 → 人读(每条都写得能自己解释,不靠图标猜)。
|
||||
#[must_use]
|
||||
pub fn reason_label(reason: ExportReasonView, catalog: Catalog) -> &'static str {
|
||||
match reason {
|
||||
ExportReasonView::ModrinthSha1 => catalog.text(Key::PackReasonModrinthSha1),
|
||||
ExportReasonView::CurseForgeFingerprint => catalog.text(Key::PackReasonCurseFingerprint),
|
||||
ExportReasonView::NoRemoteMatch => catalog.text(Key::PackReasonNoRemoteMatch),
|
||||
ExportReasonView::CurseOnlyMatch => catalog.text(Key::PackReasonCurseOnly),
|
||||
ExportReasonView::ModrinthOnlyMatch => catalog.text(Key::PackReasonModrinthOnly),
|
||||
ExportReasonView::MissingSha512 => catalog.text(Key::PackReasonMissingSha512),
|
||||
ExportReasonView::MissingCurseIds => catalog.text(Key::PackReasonMissingCurseIds),
|
||||
ExportReasonView::DisabledNotExpressible => catalog.text(Key::PackReasonDisabled),
|
||||
ExportReasonView::SymbolicLink => catalog.text(Key::PackReasonSymlink),
|
||||
ExportReasonView::UnsafePath => catalog.text(Key::PackReasonUnsafePath),
|
||||
ExportReasonView::Unreadable => catalog.text(Key::PackReasonUnreadable),
|
||||
}
|
||||
}
|
||||
|
||||
/// 格式人读名。
|
||||
#[must_use]
|
||||
pub fn format_label(format: PackFormatView, catalog: Catalog) -> &'static str {
|
||||
match format {
|
||||
PackFormatView::Curseforge => catalog.text(Key::PackFormatCurseforge),
|
||||
PackFormatView::Modrinth => catalog.text(Key::PackFormatModrinth),
|
||||
PackFormatView::Multimc => catalog.text(Key::PackFormatMultimc),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
//! 阶段与进度:**把导入进度映射成下载页那块面板的入参**(不另画一套进度 UI)。
|
||||
//!
|
||||
//! ## 任务书第 5 条怎么落
|
||||
//!
|
||||
//! 「沿用既有安装面板组件与状态机,禁止再写第二套进度 UI」——
|
||||
//!
|
||||
//! 1. 导入的 `ImportStageView` / 计数 / 当前路径先映射成 [`InstallProgressView`](本文件
|
||||
//! [`panel_progress`]);
|
||||
//! 2. 画法交给 `pages::download::install_panel`(M109 已经打过薄包装,同一个函数、同一套几何
|
||||
//! 与文案);
|
||||
//! 3. 面板只认四个粗阶段(规划 / 下载 / 安装 / 登记),任务书要的「解包 / overrides / 补文件」
|
||||
//! 三档由**一行步骤说明**([`stage_line`])承载。
|
||||
//!
|
||||
//! 第 3 点不是第二套进度 UI:多出来的只是一句文字,进度条、计数行、取消按钮仍然只有那一份。
|
||||
//! 为什么不再造一条按 `ImportStageView` 五档划分的进度条:那正是「第二套」的定义,而且两块条
|
||||
//! 同时出现时用户不知道该信哪一条。
|
||||
|
||||
use unlucky_i18n::{Catalog, Key};
|
||||
use unlucky_presenter::dto::{
|
||||
ImportProgressView, ImportStageView, InstallProgressView, InstallStage,
|
||||
};
|
||||
|
||||
use crate::pages::download::Download;
|
||||
|
||||
/// 抛进面板的进度行。
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Running {
|
||||
/// 包名(面板标题那一行)
|
||||
pub pack: String,
|
||||
/// 细档位说明(`步骤 2/5 · 解包覆盖内容`)
|
||||
pub stage_line: String,
|
||||
/// 交给下载页那块面板的进度(**唯一**的进度形状来源)
|
||||
pub panel: Download,
|
||||
}
|
||||
|
||||
/// 细档位说明(`步骤 2/5 · 解包覆盖内容`)。
|
||||
#[must_use]
|
||||
pub fn stage_line(stage: ImportStageView, catalog: Catalog) -> String {
|
||||
let total = ImportStageView::ALL.len();
|
||||
let index = ImportStageView::ALL
|
||||
.iter()
|
||||
.position(|item| *item == stage)
|
||||
.map_or(1, |at| at + 1);
|
||||
catalog.render(
|
||||
Key::PackStageLine,
|
||||
&[
|
||||
("index", &index.to_string()),
|
||||
("total", &total.to_string()),
|
||||
("stage", catalog.text(stage_key(stage))),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
/// 阶段 → 文案 key。
|
||||
#[must_use]
|
||||
pub const fn stage_key(stage: ImportStageView) -> Key {
|
||||
match stage {
|
||||
ImportStageView::Probe => Key::PackStageProbe,
|
||||
ImportStageView::Extract => Key::PackStageExtract,
|
||||
ImportStageView::Fetch => Key::PackStageFetch,
|
||||
ImportStageView::Commit => Key::PackStageCommit,
|
||||
ImportStageView::Register => Key::PackStageRegister,
|
||||
}
|
||||
}
|
||||
|
||||
/// 导入阶段 → 安装面板的粗阶段(**唯一映射处**)。
|
||||
///
|
||||
/// `Extract` / `Commit` 都落到 `Installing`:对那块面板而言它们都是「在写盘」,区别由
|
||||
/// [`stage_line`] 说给用户。
|
||||
#[must_use]
|
||||
pub const fn panel_stage(stage: ImportStageView) -> InstallStage {
|
||||
match stage {
|
||||
ImportStageView::Probe => InstallStage::Planning,
|
||||
ImportStageView::Extract | ImportStageView::Commit => InstallStage::Installing,
|
||||
ImportStageView::Fetch => InstallStage::Downloading,
|
||||
ImportStageView::Register => InstallStage::Registering,
|
||||
}
|
||||
}
|
||||
|
||||
/// 导入进度 → 下载页面板的入参。
|
||||
///
|
||||
/// `version` / `instance` 都填包名:面板第一行显示的就是「这次在装什么」,
|
||||
/// 而导入的对象是包,不是某个版本 id。
|
||||
#[must_use]
|
||||
pub fn panel_progress(progress: &ImportProgressView) -> InstallProgressView {
|
||||
InstallProgressView {
|
||||
version: progress.pack.clone(),
|
||||
instance: progress.pack.clone(),
|
||||
loader: String::new(),
|
||||
stage: panel_stage(progress.stage),
|
||||
files_done: u64::from(progress.done),
|
||||
files_total: u64::from(progress.total),
|
||||
bytes_done: 0,
|
||||
bytes_total: 0,
|
||||
speed_bps: 0,
|
||||
eta_secs: 0,
|
||||
message: progress.current.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 进度事件 → 面板要的视图模型。
|
||||
pub(super) fn running_of(progress: &ImportProgressView, catalog: Catalog) -> Running {
|
||||
Running {
|
||||
pack: progress.pack.clone(),
|
||||
stage_line: stage_line(progress.stage, catalog),
|
||||
panel: Download {
|
||||
install: Some(crate::pages::download::install_view(
|
||||
&panel_progress(progress),
|
||||
catalog,
|
||||
)),
|
||||
..Download::loading()
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
//! 导入结果的视图模型:**三态 + 容忍域逐条 + 致命块**。
|
||||
//!
|
||||
//! ## 为什么容忍域与致命域连字段类型都不同
|
||||
//!
|
||||
//! 任务书:「失败原因逐条展示(能容忍的缺文件与致命错误必须视觉可区分)」。
|
||||
//! 只在颜色上区分不够——颜色一关掉(截图/高对比/色弱)就没了。所以:
|
||||
//!
|
||||
//! | 域 | 类型 | 画法 |
|
||||
//! |---|---|---|
|
||||
//! | 容忍(缺文件 / 被拒 component / 注记) | [`Row`](路径 + 原因) | 中性容器 + `Info` 图标 + 逐条列表 |
|
||||
//! | 致命(已回滚) | [`Fatal`](大类 + 原文 + 建议 + 稳定码) | `ErrorContainer` + `Error` 图标 + 标题换措辞 |
|
||||
//!
|
||||
//! 一个 `Option<Fatal>` 与一个 `Vec<Row>` 之间没有「误合并」的写法,这比任何样式约定都硬。
|
||||
|
||||
use unlucky_i18n::{Catalog, Key};
|
||||
use unlucky_presenter::dto::{
|
||||
ConflictChoiceView, ConflictFileView, ConflictReasonView, ImportErrorView, ImportReportView,
|
||||
ImportStatusView, MissingFileView, MissingReasonView,
|
||||
};
|
||||
|
||||
use super::PackPageState;
|
||||
|
||||
/// 报告里的一行(容忍域:缺文件 / 被拒 component)。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Row {
|
||||
/// 左列(路径 / 组件名)
|
||||
pub path: String,
|
||||
/// 右列(原因的人读文案)
|
||||
pub note: String,
|
||||
}
|
||||
|
||||
/// 一条冲突(三方哈希 + 本次处置)。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ConflictRow {
|
||||
/// 实例内路径
|
||||
pub path: String,
|
||||
/// 成因(`你改过` / `你删过` / `本来就在`)
|
||||
pub reason: String,
|
||||
/// 本次实际处置(`保留我的` / `用包里的`)
|
||||
pub choice: String,
|
||||
/// 包内 SHA-1(截短)
|
||||
pub pack_sha1: String,
|
||||
/// 基线 SHA-1(`None` = 没有基线)
|
||||
pub base_sha1: Option<String>,
|
||||
/// 磁盘 SHA-1(`None` = 文件不在)
|
||||
pub disk_sha1: Option<String>,
|
||||
/// 展开中(展开才画三方哈希那行)
|
||||
pub expanded: bool,
|
||||
}
|
||||
|
||||
/// 致命失败块(**已整包回滚**)。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Fatal {
|
||||
/// 「已回滚 —— 目标目录零残留」
|
||||
pub title: String,
|
||||
/// 大类名(`路径穿越` / `清单损坏` …)
|
||||
pub label: String,
|
||||
/// 具体是什么
|
||||
pub message: String,
|
||||
/// 稳定码
|
||||
pub code: String,
|
||||
/// 补救建议
|
||||
pub hint: Option<String>,
|
||||
}
|
||||
|
||||
/// 导入结果块。
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Report {
|
||||
/// 三态
|
||||
pub status: ImportStatusView,
|
||||
/// 标题(已导入 / 有缺口 / 已回滚)
|
||||
pub title: String,
|
||||
/// 副标题(`包名 · 格式 · 游戏版本 · 加载器`)
|
||||
pub subtitle: String,
|
||||
/// 写盘行(`12 个文件已写盘 · 3.4 MB`)
|
||||
pub written_line: String,
|
||||
/// 致命失败(有它就**没有**下面的缺口区块)
|
||||
pub fatal: Option<Fatal>,
|
||||
/// 缺文件标题
|
||||
pub missing_title: Option<String>,
|
||||
/// 缺文件逐条(容忍域)
|
||||
pub missing: Vec<Row>,
|
||||
/// 冲突标题
|
||||
pub conflicts_title: Option<String>,
|
||||
/// 冲突逐条
|
||||
pub conflicts: Vec<ConflictRow>,
|
||||
/// 被拒 component 逐条
|
||||
pub rejected: Vec<Row>,
|
||||
/// 注记逐条
|
||||
pub warnings: Vec<String>,
|
||||
/// 能不能给「以包为准重新导入」(有冲突、且实例可用时才有意义)
|
||||
pub can_overwrite: bool,
|
||||
/// 「两者都留改名」为何不支持(有冲突时如实写)
|
||||
pub overwrite_note: Option<String>,
|
||||
}
|
||||
|
||||
impl Report {
|
||||
/// 实例可用吗(`Success` / `Partial` 都是装上了)。
|
||||
pub const fn installed(&self) -> bool {
|
||||
self.status.installed()
|
||||
}
|
||||
}
|
||||
|
||||
/// 一次导入结果 → 视图模型。
|
||||
pub(super) fn report_of(
|
||||
report: &ImportReportView,
|
||||
page: &PackPageState,
|
||||
catalog: Catalog,
|
||||
) -> Report {
|
||||
let missing_title = (!report.missing.is_empty()).then(|| {
|
||||
catalog.render(
|
||||
Key::PackReportMissing,
|
||||
&[("count", &report.missing.len().to_string())],
|
||||
)
|
||||
});
|
||||
let conflicts_title = (!report.conflicts.is_empty()).then(|| {
|
||||
catalog.render(
|
||||
Key::PackReportConflicts,
|
||||
&[("count", &report.conflicts.len().to_string())],
|
||||
)
|
||||
});
|
||||
|
||||
Report {
|
||||
status: report.status,
|
||||
title: status_title(report.status, catalog).to_owned(),
|
||||
subtitle: subtitle(report, catalog),
|
||||
written_line: catalog.render(
|
||||
Key::PackReportWritten,
|
||||
&[
|
||||
("count", &report.written().to_string()),
|
||||
("bytes", &human(report)),
|
||||
],
|
||||
),
|
||||
fatal: report.error.as_ref().map(|error| fatal_of(error, catalog)),
|
||||
missing_title,
|
||||
missing: report
|
||||
.missing
|
||||
.iter()
|
||||
.map(|file| missing_row(file, catalog))
|
||||
.collect(),
|
||||
conflicts_title,
|
||||
conflicts: report
|
||||
.conflicts
|
||||
.iter()
|
||||
.map(|file| conflict_row(file, page.expanded.as_deref(), catalog))
|
||||
.collect(),
|
||||
rejected: report
|
||||
.rejected
|
||||
.iter()
|
||||
.map(|component| Row {
|
||||
path: component.clone(),
|
||||
note: catalog.text(Key::PackReportRejectedNote).to_owned(),
|
||||
})
|
||||
.collect(),
|
||||
warnings: report.warnings.clone(),
|
||||
can_overwrite: !report.conflicts.is_empty() && report.status == ImportStatusView::Partial,
|
||||
overwrite_note: (!report.conflicts.is_empty())
|
||||
.then(|| catalog.text(Key::PackOverwriteNote).to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
/// 字节数的人读串。
|
||||
fn human(report: &ImportReportView) -> String {
|
||||
unlucky_presenter::dto::human_bytes(report.bytes_written)
|
||||
}
|
||||
|
||||
fn status_title(status: ImportStatusView, catalog: Catalog) -> &'static str {
|
||||
match status {
|
||||
ImportStatusView::Success => catalog.text(Key::PackStatusSuccess),
|
||||
ImportStatusView::Partial => catalog.text(Key::PackStatusPartial),
|
||||
ImportStatusView::Failed => catalog.text(Key::PackStatusFailed),
|
||||
}
|
||||
}
|
||||
|
||||
/// 副标题:包名 · 格式 · 游戏版本 · 加载器(缺哪个就不写哪个,不拼「未知」)。
|
||||
fn subtitle(report: &ImportReportView, catalog: Catalog) -> String {
|
||||
let mut parts: Vec<String> = Vec::new();
|
||||
if let Some(format) = report.format {
|
||||
parts.push(super::format_label(format, catalog).to_owned());
|
||||
}
|
||||
if let Some(version) = &report.game_version {
|
||||
parts.push(version.clone());
|
||||
}
|
||||
if let Some(loader) = &report.loader {
|
||||
parts.push(loader.clone());
|
||||
}
|
||||
if let Some(name) = &report.pack_name {
|
||||
parts.insert(0, name.clone());
|
||||
}
|
||||
parts.join(" · ")
|
||||
}
|
||||
|
||||
fn fatal_of(error: &ImportErrorView, catalog: Catalog) -> Fatal {
|
||||
Fatal {
|
||||
title: catalog.text(Key::PackFatalTitle).to_owned(),
|
||||
label: error.label.clone(),
|
||||
message: error.message.clone(),
|
||||
code: error.code.clone(),
|
||||
hint: error.hint.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_row(file: &MissingFileView, catalog: Catalog) -> Row {
|
||||
Row {
|
||||
path: file.path.clone().unwrap_or_else(|| file.display.clone()),
|
||||
note: match file.reason {
|
||||
MissingReasonView::Offline => catalog.text(Key::PackMissingOffline).to_owned(),
|
||||
MissingReasonView::NoSource => catalog.text(Key::PackMissingNoSource).to_owned(),
|
||||
MissingReasonView::FetchFailed => catalog.render(
|
||||
Key::PackMissingFetchFailed,
|
||||
&[("detail", file.detail.as_str())],
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn conflict_row(file: &ConflictFileView, expanded: Option<&str>, catalog: Catalog) -> ConflictRow {
|
||||
ConflictRow {
|
||||
path: file.path.clone(),
|
||||
reason: match file.reason {
|
||||
ConflictReasonView::UserModified => catalog.text(Key::PackConflictUserModified),
|
||||
ConflictReasonView::UserDeleted => catalog.text(Key::PackConflictUserDeleted),
|
||||
ConflictReasonView::PreExisting => catalog.text(Key::PackConflictPreExisting),
|
||||
}
|
||||
.to_owned(),
|
||||
choice: match file.choice {
|
||||
ConflictChoiceView::KeepUser => catalog.text(Key::PackChoiceKeepUser),
|
||||
ConflictChoiceView::Overwrite => catalog.text(Key::PackChoiceOverwrite),
|
||||
}
|
||||
.to_owned(),
|
||||
pack_sha1: short_sha(&file.pack_sha1),
|
||||
base_sha1: file.base_sha1.as_deref().map(short_sha),
|
||||
disk_sha1: file.disk_sha1.as_deref().map(short_sha),
|
||||
expanded: expanded == Some(file.path.as_str()),
|
||||
}
|
||||
}
|
||||
|
||||
/// SHA-1 截短(前 8 位够人眼区分;全 40 位会把一行撑爆)。
|
||||
#[must_use]
|
||||
pub fn short_sha(sha: &str) -> String {
|
||||
sha.chars().take(8).collect()
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
//! 导入/导出区视图模型的守门(M113):阶段映射、预检计数、三方哈希、容忍/致命分野。
|
||||
//!
|
||||
//! 这里**不测画法**(画法靠出图),测的是「决策」:哪些数字进了哪一行、哪些字段被截短、
|
||||
//! 哪一态该出现哪一块。所有断言都能在没有图形环境的情况下跑。
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use unlucky_i18n::Lang;
|
||||
use unlucky_presenter::dto::{
|
||||
ConflictChoiceView, ConflictFileView, ConflictReasonView, ExportClassView, ExportFileView,
|
||||
ExportPreviewView, ImportErrorView, ImportProgressView, ImportReportView, ImportStageView,
|
||||
ImportStatusView, InstallStage, MissingFileView, MissingReasonView, PackFormatView,
|
||||
};
|
||||
|
||||
use super::progress::{panel_progress, panel_stage, stage_line};
|
||||
use super::report::report_of;
|
||||
use super::report::short_sha;
|
||||
use super::{Pack, PackPageState};
|
||||
|
||||
fn catalog() -> unlucky_i18n::Catalog {
|
||||
Lang::En.catalog()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stage_line_counts_the_five_steps_in_order() {
|
||||
let expected = [
|
||||
(ImportStageView::Probe, "1/5"),
|
||||
(ImportStageView::Extract, "2/5"),
|
||||
(ImportStageView::Fetch, "3/5"),
|
||||
(ImportStageView::Commit, "4/5"),
|
||||
(ImportStageView::Register, "5/5"),
|
||||
];
|
||||
for (stage, position) in expected {
|
||||
let line = stage_line(stage, catalog());
|
||||
assert!(line.contains(position), "{stage:?} 的步号不对:{line}");
|
||||
}
|
||||
// 阶段名要真的是「那一档」,不能五个阶段都显示同一句
|
||||
let lines: Vec<String> = ImportStageView::ALL
|
||||
.iter()
|
||||
.map(|stage| stage_line(*stage, catalog()))
|
||||
.collect();
|
||||
for (index, line) in lines.iter().enumerate() {
|
||||
for other in lines.iter().skip(index + 1) {
|
||||
assert_ne!(line, other, "两个阶段的说明撞了:{line}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_progress_stage_maps_to_a_terminal_install_stage() {
|
||||
// 进度面板只该画「在跑」的形态:映射到 Done/Failed/Cancelled 会让面板提前画成收尾/失败
|
||||
for stage in ImportStageView::ALL {
|
||||
let mapped = panel_stage(stage);
|
||||
assert!(
|
||||
!mapped.is_terminal(),
|
||||
"{stage:?} 落到了终态 {mapped:?}(面板会画成收尾)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panel_progress_carries_the_counts_and_the_pack_name() {
|
||||
let progress = ImportProgressView {
|
||||
stage: ImportStageView::Fetch,
|
||||
done: 41,
|
||||
total: 90,
|
||||
current: Some("mods/sodium.jar".to_string()),
|
||||
pack: "Better MC".to_string(),
|
||||
};
|
||||
let panel = panel_progress(&progress);
|
||||
assert_eq!(panel.files_done, 41);
|
||||
assert_eq!(panel.files_total, 90);
|
||||
assert_eq!(panel.stage, InstallStage::Downloading);
|
||||
// 面板第一行显示的就是包名(导入的对象是包,不是某个版本 id)
|
||||
assert_eq!(panel.version, "Better MC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_counts_keep_the_bar_unknown() {
|
||||
// total == 0 时不允许画确定态进度条(那是「静止的假进度」的来源)
|
||||
let progress = ImportProgressView {
|
||||
stage: ImportStageView::Probe,
|
||||
done: 0,
|
||||
total: 0,
|
||||
current: None,
|
||||
pack: "Pack".to_string(),
|
||||
};
|
||||
let panel = panel_progress(&progress);
|
||||
assert_eq!(panel.files_total, 0);
|
||||
assert_eq!(panel.speed_bps, 0, "导入没有字节速率,不许编");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sha_is_shortened_to_eight_chars() {
|
||||
assert_eq!(
|
||||
short_sha("9f2c41ab77de3350aa1b8c0d5e6f708192a3b4c5"),
|
||||
"9f2c41ab"
|
||||
);
|
||||
assert_eq!(short_sha("abc"), "abc");
|
||||
assert_eq!(short_sha(""), "");
|
||||
}
|
||||
|
||||
fn preview(files: Vec<ExportFileView>) -> ExportPreviewView {
|
||||
ExportPreviewView {
|
||||
format: PackFormatView::Modrinth,
|
||||
instance: "survival".to_string(),
|
||||
name: "Survival".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
target: PathBuf::from("C:/tmp/out.mrpack"),
|
||||
files,
|
||||
notes: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn export_file(
|
||||
path: &str,
|
||||
class: ExportClassView,
|
||||
reason: unlucky_presenter::dto::ExportReasonView,
|
||||
) -> ExportFileView {
|
||||
ExportFileView {
|
||||
path: path.to_string(),
|
||||
class,
|
||||
reason,
|
||||
bytes: 1000,
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preview_counts_match_the_three_classes() {
|
||||
use unlucky_presenter::dto::ExportReasonView;
|
||||
let view = preview(vec![
|
||||
export_file(
|
||||
"a.jar",
|
||||
ExportClassView::Remote,
|
||||
ExportReasonView::ModrinthSha1,
|
||||
),
|
||||
export_file(
|
||||
"b.jar",
|
||||
ExportClassView::Inline,
|
||||
ExportReasonView::NoRemoteMatch,
|
||||
),
|
||||
export_file(
|
||||
"c.jar",
|
||||
ExportClassView::Inline,
|
||||
ExportReasonView::MissingSha512,
|
||||
),
|
||||
export_file(
|
||||
"d.jar",
|
||||
ExportClassView::Skipped,
|
||||
ExportReasonView::SymbolicLink,
|
||||
),
|
||||
]);
|
||||
assert_eq!(view.remote_count(), 1);
|
||||
assert_eq!(view.inline_count(), 2);
|
||||
assert_eq!(view.skipped_count(), 1);
|
||||
assert_eq!(view.inline_bytes(), 2000);
|
||||
assert!(!view.is_complete(), "有跳过项 ⇒ 包不完整");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_head_line_carries_the_two_numbers_the_task_asks_for() {
|
||||
use unlucky_presenter::dto::ExportReasonView;
|
||||
let view = preview(vec![
|
||||
export_file(
|
||||
"b.jar",
|
||||
ExportClassView::Inline,
|
||||
ExportReasonView::NoRemoteMatch,
|
||||
),
|
||||
export_file(
|
||||
"c.jar",
|
||||
ExportClassView::Inline,
|
||||
ExportReasonView::MissingSha512,
|
||||
),
|
||||
export_file(
|
||||
"d.jar",
|
||||
ExportClassView::Skipped,
|
||||
ExportReasonView::UnsafePath,
|
||||
),
|
||||
]);
|
||||
let block = super::export::preview_of(&view, catalog());
|
||||
// 「将内联 N 个文件 · 无法导出 M 项」两个数字必须来自同一份 files,不许另算
|
||||
assert!(
|
||||
block.head.contains('2'),
|
||||
"内联数没进 headline:{}",
|
||||
block.head
|
||||
);
|
||||
assert!(
|
||||
block.head.contains('1'),
|
||||
"跳过数没进 headline:{}",
|
||||
block.head
|
||||
);
|
||||
assert_eq!(block.skipped.len(), 1);
|
||||
assert!(!block.complete);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_keeps_tolerable_and_fatal_apart() {
|
||||
let report = ImportReportView {
|
||||
status: ImportStatusView::Failed,
|
||||
format: Some(PackFormatView::Curseforge),
|
||||
pack_name: Some("Broken".to_string()),
|
||||
pack_version: None,
|
||||
game_version: None,
|
||||
loader: None,
|
||||
version_id: None,
|
||||
target: PathBuf::from("C:/tmp"),
|
||||
files: Vec::new(),
|
||||
conflicts: Vec::new(),
|
||||
missing: vec![MissingFileView {
|
||||
path: Some("mods/a.jar".to_string()),
|
||||
display: "mods/a.jar".to_string(),
|
||||
reason: MissingReasonView::Offline,
|
||||
detail: String::new(),
|
||||
}],
|
||||
rejected: Vec::new(),
|
||||
warnings: Vec::new(),
|
||||
error: Some(ImportErrorView {
|
||||
code: "pack_traversal".to_string(),
|
||||
label: "路径穿越".to_string(),
|
||||
message: "../etc/passwd".to_string(),
|
||||
hint: Some("换一个来源".to_string()),
|
||||
}),
|
||||
registered: None,
|
||||
bytes_written: 0,
|
||||
rolled_back: true,
|
||||
};
|
||||
let block = report_of(&report, &PackPageState::default(), catalog());
|
||||
assert!(block.fatal.is_some(), "致命块必须单独存在");
|
||||
assert_eq!(block.missing.len(), 1, "容忍域仍要逐条列出");
|
||||
assert!(!block.installed(), "Failed 不是「装上了」");
|
||||
assert!(!block.can_overwrite, "没有冲突时不给翻档动作");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflicts_default_to_keeping_the_user_copy() {
|
||||
let report = ImportReportView {
|
||||
status: ImportStatusView::Partial,
|
||||
format: Some(PackFormatView::Modrinth),
|
||||
pack_name: Some("Pack".to_string()),
|
||||
pack_version: None,
|
||||
game_version: None,
|
||||
loader: None,
|
||||
version_id: None,
|
||||
target: PathBuf::from("C:/tmp"),
|
||||
files: Vec::new(),
|
||||
conflicts: vec![ConflictFileView {
|
||||
path: "config/x.json".to_string(),
|
||||
reason: ConflictReasonView::UserModified,
|
||||
choice: ConflictChoiceView::KeepUser,
|
||||
pack_sha1: "fixture".to_string(),
|
||||
base_sha1: None,
|
||||
disk_sha1: Some("disk".to_string()),
|
||||
pack_size: 12,
|
||||
}],
|
||||
missing: Vec::new(),
|
||||
rejected: Vec::new(),
|
||||
warnings: Vec::new(),
|
||||
error: None,
|
||||
registered: None,
|
||||
bytes_written: 0,
|
||||
rolled_back: true,
|
||||
};
|
||||
let page = PackPageState::default();
|
||||
let block = report_of(&report, &page, catalog());
|
||||
assert_eq!(block.conflicts.len(), 1);
|
||||
assert!(!block.conflicts[0].expanded, "默认收起");
|
||||
assert_eq!(
|
||||
block.conflicts[0].choice,
|
||||
catalog().text(unlucky_i18n::Key::PackChoiceKeepUser),
|
||||
"默认档必须是「保留我的」"
|
||||
);
|
||||
assert!(block.can_overwrite, "有冲突要能整体翻档");
|
||||
assert!(block.overwrite_note.is_some(), "不支持「两者都留」要如实写");
|
||||
|
||||
// 展开的那一条才画三方哈希
|
||||
let expanded = PackPageState {
|
||||
expanded: Some("config/x.json".to_string()),
|
||||
..PackPageState::default()
|
||||
};
|
||||
let block = report_of(&report, &expanded, catalog());
|
||||
assert!(block.conflicts[0].expanded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_idle_pack_has_nothing_to_draw_but_the_entry() {
|
||||
let import = unlucky_presenter::dto::ImportState::default();
|
||||
let export = unlucky_presenter::dto::ExportState::default();
|
||||
let model = Pack::of(&import, &export, &PackPageState::default(), &[], catalog());
|
||||
assert!(model.is_idle());
|
||||
assert!(model.running.is_none());
|
||||
assert!(model.report.is_none());
|
||||
// 入口说明永远在(用户得知道可以拖什么、为什么没有「选择文件」按钮)
|
||||
assert!(!model.drop_hint.is_empty());
|
||||
assert!(!model.no_dialog_note.is_empty());
|
||||
}
|
||||
Reference in new issue
Block a user