1use serde::Serialize;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
10#[serde(rename_all = "lowercase")]
11pub enum Level {
12 Error,
14 Warn,
16}
17
18impl Level {
19 pub fn as_str(self) -> &'static str {
21 match self {
22 Level::Error => "error",
23 Level::Warn => "warn",
24 }
25 }
26}
27
28pub mod code {
30 pub const PARSE: &str = "E_PARSE";
33 pub const META_MISSING: &str = "E_META_MISSING";
35 pub const SPEC_UNSUPPORTED: &str = "E_SPEC_UNSUPPORTED";
37 pub const KIND_INVALID: &str = "E_KIND_INVALID";
39 pub const KIND_DEPTH: &str = "E_KIND_DEPTH";
41 pub const SCHEMA_FIELD: &str = "E_SCHEMA_FIELD";
43 pub const ID_MISMATCH: &str = "E_ID_MISMATCH";
45 pub const ID_NOT_UUID: &str = "E_ID_NOT_UUID";
47 pub const ID_VERSION: &str = "E_ID_VERSION";
49 pub const ID_DUP: &str = "E_ID_DUP";
51 pub const ENTRY_ROLE_DEPTH: &str = "E_ENTRY_ROLE_DEPTH";
53 pub const ENTRY_ID_MISMATCH: &str = "E_ENTRY_ID_MISMATCH";
55 pub const DEPTH_EXCEEDED: &str = "E_DEPTH_EXCEEDED";
57
58 pub const REF_NO_TARGET: &str = "E_REF_NO_TARGET";
61 pub const REF_SELF: &str = "E_REF_SELF";
63 pub const REF_CYCLE: &str = "E_REF_CYCLE";
65
66 pub const LINK_NO_TARGET: &str = "E_LINK_NO_TARGET";
69 pub const LINK_TARGET_INVALID: &str = "E_LINK_TARGET_INVALID";
71 pub const LINK_HAS_PAYLOAD: &str = "E_LINK_HAS_PAYLOAD";
73 pub const LINK_CYCLE: &str = "E_LINK_CYCLE";
75 pub const LINK_DUP: &str = "E_LINK_DUP";
77 pub const LINK_OWN_CONTENT: &str = "E_LINK_OWN_CONTENT";
79
80 pub const MANIFEST_MISSING: &str = "E_MANIFEST_MISSING";
83 pub const MANIFEST_GHOST: &str = "E_MANIFEST_GHOST";
85 pub const MANIFEST_HASH: &str = "E_MANIFEST_HASH";
87 pub const MANIFEST_DIGEST_MISSING: &str = "E_MANIFEST_DIGEST_MISSING";
89 pub const MANIFEST_DUP: &str = "E_MANIFEST_DUP";
91 pub const RESERVED_NAME: &str = "E_RESERVED_NAME";
93 pub const REVISION_STALE: &str = "E_REVISION_STALE";
95 pub const SCHEMA_FAIL: &str = "E_SCHEMA_FAIL";
97
98 pub const BUNDLE_SUFFIX: &str = "W_BUNDLE_SUFFIX";
101 pub const DOTFILE: &str = "W_DOTFILE";
103 pub const ROOT_STRAY: &str = "W_ROOT_STRAY";
105 pub const NO_SUMMARY: &str = "W_NO_SUMMARY";
107 pub const NO_TYPE: &str = "W_NO_TYPE";
109 pub const DEEP_TREE: &str = "W_DEEP_TREE";
111 pub const LARGE_ASSET: &str = "W_LARGE_ASSET";
113 pub const OPTIONAL_MISSING: &str = "W_OPTIONAL_MISSING";
115 pub const MANIFEST_MISSING_W: &str = "W_MANIFEST_MISSING";
117 pub const MANIFEST_GHOST_W: &str = "W_MANIFEST_GHOST";
119 pub const MANIFEST_HASH_W: &str = "W_MANIFEST_HASH";
121
122 pub const ALL: &[&str] = &[
124 PARSE,
125 META_MISSING,
126 SPEC_UNSUPPORTED,
127 KIND_INVALID,
128 KIND_DEPTH,
129 SCHEMA_FIELD,
130 ID_MISMATCH,
131 ID_NOT_UUID,
132 ID_VERSION,
133 ID_DUP,
134 ENTRY_ROLE_DEPTH,
135 ENTRY_ID_MISMATCH,
136 DEPTH_EXCEEDED,
137 REF_NO_TARGET,
138 REF_SELF,
139 REF_CYCLE,
140 MANIFEST_MISSING,
141 MANIFEST_GHOST,
142 MANIFEST_HASH,
143 MANIFEST_DIGEST_MISSING,
144 MANIFEST_DUP,
145 RESERVED_NAME,
146 REVISION_STALE,
147 SCHEMA_FAIL,
148 BUNDLE_SUFFIX,
149 DOTFILE,
150 ROOT_STRAY,
151 NO_SUMMARY,
152 NO_TYPE,
153 DEEP_TREE,
154 LARGE_ASSET,
155 OPTIONAL_MISSING,
156 MANIFEST_MISSING_W,
157 MANIFEST_GHOST_W,
158 MANIFEST_HASH_W,
159 ];
160}
161
162#[derive(Debug, Clone, Serialize)]
164pub struct Issue {
165 pub code: String,
167 pub level: Level,
169 pub path: String,
171 pub message: String,
173}
174
175impl Issue {
176 pub fn new(
178 code: &'static str,
179 level: Level,
180 path: impl Into<String>,
181 message: impl Into<String>,
182 ) -> Self {
183 Self {
184 code: code.to_string(),
185 level,
186 path: path.into(),
187 message: message.into(),
188 }
189 }
190
191 pub fn error(code: &'static str, path: impl Into<String>, message: impl Into<String>) -> Self {
193 Self::new(code, Level::Error, path, message)
194 }
195
196 pub fn warn(code: &'static str, path: impl Into<String>, message: impl Into<String>) -> Self {
198 Self::new(code, Level::Warn, path, message)
199 }
200}
201
202#[derive(Debug, Clone, Default, Serialize)]
204pub struct Stats {
205 pub nodes: usize,
207 pub branches: usize,
209 pub entries: usize,
211 pub depth: usize,
213}
214
215#[derive(Debug, Clone, Default)]
217pub struct Report {
218 pub bundle: String,
220 pub issues: Vec<Issue>,
222 pub stats: Stats,
224}
225
226impl Report {
227 pub fn push(&mut self, issue: Issue) {
229 self.issues.push(issue);
230 }
231
232 pub fn errors(&self) -> impl Iterator<Item = &Issue> {
234 self.issues.iter().filter(|i| i.level == Level::Error)
235 }
236
237 pub fn warnings(&self) -> impl Iterator<Item = &Issue> {
239 self.issues.iter().filter(|i| i.level == Level::Warn)
240 }
241
242 pub fn error_count(&self) -> usize {
244 self.errors().count()
245 }
246
247 pub fn warning_count(&self) -> usize {
249 self.warnings().count()
250 }
251
252 pub fn exit_code(&self) -> i32 {
254 if self.error_count() > 0 { 1 } else { 0 }
255 }
256
257 pub fn to_json(&self) -> serde_json::Value {
259 let conv = |it: &Issue| {
260 serde_json::json!({
261 "code": it.code,
262 "level": it.level.as_str(),
263 "path": it.path,
264 "message": it.message,
265 })
266 };
267 serde_json::json!({
268 "bundle": self.bundle,
269 "errors": self.errors().map(conv).collect::<Vec<_>>(),
270 "warnings": self.warnings().map(conv).collect::<Vec<_>>(),
271 "stats": {
272 "nodes": self.stats.nodes,
273 "branches": self.stats.branches,
274 "entries": self.stats.entries,
275 "depth": self.stats.depth,
276 },
277 })
278 }
279
280 pub fn to_text(&self) -> String {
282 let mut out = String::new();
283 out.push_str(&format!(
284 "{} {} nodes / {} branches / {} entries depth={}\n",
285 self.bundle, self.stats.nodes, self.stats.branches, self.stats.entries, self.stats.depth
286 ));
287 for it in &self.issues {
288 let mark = match it.level {
289 Level::Error => "✗",
290 Level::Warn => "⚠",
291 };
292 out.push_str(&format!(
293 " {} {:<28} {} {}\n",
294 mark, it.code, it.path, it.message
295 ));
296 }
297 out.push_str(&format!(
298 "{} errors, {} warnings exit={}",
299 self.error_count(),
300 self.warning_count(),
301 self.exit_code()
302 ));
303 out
304 }
305}
306
307#[derive(Debug, thiserror::Error)]
309pub enum Error {
310 #[error("路径不存在:{0}")]
312 NotFound(String),
313 #[error("参数错误:{0}")]
315 BadArg(String),
316 #[error("I/O 错误({path}):{source}")]
318 Io {
319 path: String,
321 source: std::io::Error,
323 },
324 #[error("校验未通过:{0} 项错误")]
326 Validation(usize),
327 #[error("{0}")]
329 Other(String),
330}
331
332impl Error {
333 pub fn io(path: impl AsRef<std::path::Path>, source: std::io::Error) -> Self {
335 Error::Io {
336 path: path.as_ref().display().to_string(),
337 source,
338 }
339 }
340}
341
342pub type Result<T> = std::result::Result<T, Error>;