From 880f90211b50a5fceb1d9274224ebdc1ee30f577 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 25 Jun 2026 13:42:23 +0800 Subject: [PATCH] feat: update error notes for wfassoc --- wfassoc-cdylib/src/lib.rs | 8 +++--- wfassoc-exec/src/manifest.rs | 2 +- wfassoc-exec/src/runner.rs | 19 +++++++------- wfassoc/src/highlevel/program.rs | 44 +++++++++++++++++--------------- wfassoc/src/highlevel/schema.rs | 2 +- wfassoc/src/lowlevel.rs | 20 +++++++-------- wfassoc/src/utilities.rs | 2 +- wfassoc/src/win32/concept.rs | 8 ++++++ 8 files changed, 58 insertions(+), 47 deletions(-) diff --git a/wfassoc-cdylib/src/lib.rs b/wfassoc-cdylib/src/lib.rs index 9baf416..39cf370 100644 --- a/wfassoc-cdylib/src/lib.rs +++ b/wfassoc-cdylib/src/lib.rs @@ -14,17 +14,17 @@ use wfassoc::highlevel::{Program, Schema}; #[derive(Debug, TeError)] enum Error { /// Error when operating Schema. - #[error("{0}")] + #[error("wfassoc error: {0}")] Schema(#[from] wfassoc::highlevel::SchemaError), /// Error when parsing Schema into Program. - #[error("{0}")] + #[error("wfassoc error: {0}")] ParseProgram(#[from] wfassoc::highlevel::ParseProgramError), /// Error when operating Program. - #[error("{0}")] + #[error("wfassoc error: {0}")] Program(#[from] wfassoc::highlevel::ProgramError), /// Error when manipulating with C-style string. - #[error("C-Style string FFI error:{0}")] + #[error("C-Style string FFI error: {0}")] CStrFfi(#[from] cstr_ffi::Error), /// Error when manipulating with object pool. #[error("object pool error: {0}")] diff --git a/wfassoc-exec/src/manifest.rs b/wfassoc-exec/src/manifest.rs index 476c3aa..9d349d0 100644 --- a/wfassoc-exec/src/manifest.rs +++ b/wfassoc-exec/src/manifest.rs @@ -63,7 +63,7 @@ impl Manifest { #[derive(Debug, TeError)] pub enum ParseSchemaError { /// Error when operating with schema. - #[error("{0}")] + #[error("failed on parsing manifest file to Schema: {0}")] Schema(#[from] wfassoc::highlevel::SchemaError) } diff --git a/wfassoc-exec/src/runner.rs b/wfassoc-exec/src/runner.rs index dc57b0c..fb89a97 100644 --- a/wfassoc-exec/src/runner.rs +++ b/wfassoc-exec/src/runner.rs @@ -12,20 +12,21 @@ use toml; #[derive(Debug, TeError)] pub enum Error { /// Error when parsing Manifest TOML file. - #[error("{0}")] + #[error("initialization error: {0}")] ParseManifest(#[from] manifest::ParseManifestError), /// Error when parsing Manifest into Schema. - #[error("{0}")] + #[error("initialization error: {0}")] ParseSchema(#[from] manifest::ParseSchemaError), + /// Error when serializing TOML + #[error("initialization error: {0}")] + SerializeToml(#[from] toml::ser::Error), + /// Error when parsing Schema into Program. - #[error("{0}")] + #[error("wfassoc error: {0}")] ParseProgram(#[from] wfassoc::highlevel::ParseProgramError), /// Error when operating Program. - #[error("{0}")] + #[error("wfassoc error: {0}")] Program(#[from] wfassoc::highlevel::ProgramError), - /// Error when serializing TOML - #[error("{0}")] - SerializeToml(#[from] toml::ser::Error), /// Find duplicated name when converting extension name to index #[error("given extension name {0} has been specified more than one time")] @@ -35,7 +36,7 @@ pub enum Error { BadExtName(String), /// Find star (*) extension name with other extension names when converting extension name to index #[error("wildcard extension name \"*\" is not allowed to be used with other extension names")] - ExclusiveStarExtName(String), + ExclusiveStarExtName, } /// Result type used in this module. @@ -60,7 +61,7 @@ fn stringified_exts_to_indices( // Check for star (*) with other extensions let has_star = exts.iter().any(|ext| ext == "*"); if has_star && exts.len() > 1 { - return Err(Error::ExclusiveStarExtName("*".to_string())); + return Err(Error::ExclusiveStarExtName); } // If star is present alone, return fixed list from zero to the maximum ext index. diff --git a/wfassoc/src/highlevel/program.rs b/wfassoc/src/highlevel/program.rs index b6160a6..293844f 100644 --- a/wfassoc/src/highlevel/program.rs +++ b/wfassoc/src/highlevel/program.rs @@ -17,37 +17,36 @@ use thiserror::Error as TeError; /// Error occurs when trying converting [Schema] into [Program]. #[derive(Debug, TeError)] pub enum ParseProgramError { - #[error("{0}")] + #[error("parsing into Program error: {0}")] BadExtBody(#[from] concept::BadExtBodyError), - #[error("{0}")] + #[error("parsing into Program error: {0}")] BadProgIdPart(#[from] concept::BadProgIdPartError), - #[error("{0}")] + #[error("parsing into Program error: {0}")] BadFileName(#[from] concept::BadFileNameError), - #[error("{0}")] + #[error("parsing into Program error: {0}")] ParseCmdLine(#[from] concept::ParseCmdLineError), - #[error("{0}")] + #[error("parsing into Program error: {0}")] CastOsStr(#[from] utilities::CastOsStrError), - #[error("given path doesn't has legal file name part")] + + #[error("given path doesn't has legal file name part when parsing into Program")] NoFileNamePart, - #[error("given path doesn't has legal directory part")] + #[error("given path doesn't has legal directory part when parsing into Program")] NoDirNamePart, - #[error("given identifier is not presented in dict")] - NoSuchIdentifier, - #[error("extension name should not be empty")] - EmptyExtension, - #[error("given program identifier is not allowed")] + #[error("identifier {0} is unexpected not presented in HashMap when parsing into Program")] + NoSuchIdentifier(String), + #[error("bad program identifier found when parsing into Program")] BadIdentifier, } /// Error occurs when operating with [Program]. #[derive(Debug, TeError)] pub enum ProgramError { - #[error("{0}")] + #[error("Program operation error: {0}")] Lowlevel(#[from] lowlevel::Error), - #[error("{0}")] + #[error("Program operation error: {0}")] LoadIconRc(#[from] concept::LoadIconRcError), - #[error("given index is invalid")] - BadIndex, + #[error("given file extension index {0} is invalid when visiting Program")] + BadIndex(usize), } // endregion @@ -129,7 +128,7 @@ impl Program { .get(*index) .expect("unexpected invalid index") .clone()), - None => Err(ParseProgramError::NoSuchIdentifier), + None => Err(ParseProgramError::NoSuchIdentifier(key.to_string())), } } @@ -322,7 +321,10 @@ impl Program { pub fn resolve_ext(&self, index: usize) -> Result { // Fetch data - let progid_ext_key = self.ext_keys.get(index).ok_or(ProgramError::BadIndex)?; + let progid_ext_key = self + .ext_keys + .get(index) + .ok_or(ProgramError::BadIndex(index))?; // Try resolving name with string resource first, // and fallback to ProgId verbatim. @@ -507,7 +509,7 @@ impl Program { ext_key.ensure(scope)?; ext_key.set_default(scope, Some(progid_key.inner()))?; } - None => return Err(ProgramError::BadIndex), + None => return Err(ProgramError::BadIndex(index)), }; // Everything is okey. @@ -529,7 +531,7 @@ impl Program { ext_key.ensure(scope)?; ext_key.set_default(scope, None)?; } - None => return Err(ProgramError::BadIndex), + None => return Err(ProgramError::BadIndex(index)), } // Everything is okey. @@ -597,7 +599,7 @@ impl Program { // Okey, return it. Ok(Some(ProgramExtStatus::new(name, icon))) } - None => Err(ProgramError::BadIndex), + None => Err(ProgramError::BadIndex(index)), } } } diff --git a/wfassoc/src/highlevel/schema.rs b/wfassoc/src/highlevel/schema.rs index dcced4d..f389ddb 100644 --- a/wfassoc/src/highlevel/schema.rs +++ b/wfassoc/src/highlevel/schema.rs @@ -7,7 +7,7 @@ use super::{Program, ParseProgramError}; /// Error occurs when operating with [Schema]. #[derive(Debug, TeError)] pub enum SchemaError { - #[error("duplicate key: {0}")] + #[error("duplicate key in Schema operation: {0}")] DuplicateKey(String), } diff --git a/wfassoc/src/lowlevel.rs b/wfassoc/src/lowlevel.rs index 41d6728..02db357 100644 --- a/wfassoc/src/lowlevel.rs +++ b/wfassoc/src/lowlevel.rs @@ -10,27 +10,27 @@ use winreg::enums::{KEY_READ, KEY_WRITE}; /// Error occurs in this module. #[derive(Debug, TeError)] pub enum Error { - #[error("can not perform this operation because lack essential privilege")] + #[error("some lowlevel operations require escalating privilege")] NoPrivilege, - #[error("given registry key is inexistant")] + #[error("given lowlevel registry key is not presented")] InexistantKey, - #[error("registry operation error: {0}")] + #[error("lowlevel registry operation error: {0}")] BadRegOp(#[from] std::io::Error), - #[error("{0}")] + #[error("unexpected blank registry key in lowlevel registry operation")] UnexpectedBlankKey(#[from] regext::BlankPathError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] ExpandEnvVar(#[from] concept::ExpandEnvVarError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] LoadIconRc(#[from] concept::LoadIconRcError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] LoadStrRc(#[from] concept::LoadStrRcError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] ParseVerb(#[from] concept::ParseVerbError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] ParseCmdLine(#[from] concept::ParseCmdLineError), - #[error("{0}")] + #[error("lowlevel operation error: {0}")] ParseExt(#[from] concept::ParseExtError), } diff --git a/wfassoc/src/utilities.rs b/wfassoc/src/utilities.rs index ba45d51..356021c 100644 --- a/wfassoc/src/utilities.rs +++ b/wfassoc/src/utilities.rs @@ -9,7 +9,7 @@ use thiserror::Error as TeError; /// The error occurs when casting `OsStr` into `str`. #[derive(Debug, TeError)] -#[error("fail to cast OS string into string")] +#[error("fail to cast OS string into Rust string")] pub struct CastOsStrError {} impl CastOsStrError { diff --git a/wfassoc/src/win32/concept.rs b/wfassoc/src/win32/concept.rs index 64e960f..4897b1b 100644 --- a/wfassoc/src/win32/concept.rs +++ b/wfassoc/src/win32/concept.rs @@ -42,6 +42,8 @@ impl Ext { /// Create an new file extension. /// /// `body` is the body of file extension (excluding dot, such as `jpg`). + /// Empty body is not allowed. + /// /// If you want to create this struct with ordinary extension string like `.jpg`, /// please use `Ext::from_str()` or `parse::()` instead. pub fn new(body: &str) -> Result { @@ -56,11 +58,17 @@ impl Ext { } /// Get the body part of file extension (excluding dot) + /// + /// This struct promise that there is no possibility that return value is blank + /// (i.e. the body of file extension is empty). pub fn inner(&self) -> &str { &self.body } /// Get the body part of file extension (with leading dot) + /// + /// This struct promise that there is no possibility that return value is dot only + /// (i.e. the body of file extension is empty). pub fn dotted_inner(&self) -> String { // Reuse Display trait result self.to_string()