1
0

feat: add losse StrRefStr and IconRefStr

This commit is contained in:
2026-04-30 10:01:22 +08:00
parent 94db3fdfd4
commit 2fff56b74d
2 changed files with 119 additions and 2 deletions

View File

@@ -20,6 +20,10 @@ pub enum Error {
BadRegOp(#[from] std::io::Error), BadRegOp(#[from] std::io::Error),
#[error("{0}")] #[error("{0}")]
UnexpectedBlankKey(#[from] regext::BlankPathError), UnexpectedBlankKey(#[from] regext::BlankPathError),
#[error("{0}")]
LoadIconRc(#[from] concept::LoadIconRcError),
#[error("{0}")]
LoadStrRc(#[from] concept::LoadStrRcError),
} }
// endregion // endregion
@@ -99,8 +103,8 @@ pub enum LosseProgId {
impl Display for LosseProgId { impl Display for LosseProgId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
LosseProgId::Plain(v) => write!(f, "{}", v), LosseProgId::Plain(v) => v.fmt(f),
LosseProgId::Strict(v) => write!(f, "{}", v), LosseProgId::Strict(v) => v.fmt(f),
} }
} }
} }
@@ -125,6 +129,115 @@ impl From<concept::ProgId> for LosseProgId {
// endregion // endregion
// region: Losse Icon Reference String
/// The enum representing a losse Icon Reference String.
///
/// In real usage, programmer can use Icon Reference String,
/// or a plain string pointing to a icon file as the icon setting value.
/// This enum is designed for handling this scenario.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LosseIconRefStr {
Plain(String),
Strict(concept::IconRefStr),
}
impl LosseIconRefStr {
pub fn extract(&self, kind: concept::IconSizeKind) -> Result<concept::IconRc, Error> {
let rc = match self {
LosseIconRefStr::Plain(v) => concept::IconRc::with_ico_file(v.as_str(), kind)?,
LosseIconRefStr::Strict(v) => concept::IconRc::new(v.get_path(), v.get_index(), kind)?,
};
Ok(rc)
}
}
impl Display for LosseIconRefStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LosseIconRefStr::Plain(v) => v.fmt(f),
LosseIconRefStr::Strict(v) => v.fmt(f),
}
}
}
impl From<&str> for LosseIconRefStr {
fn from(s: &str) -> Self {
// match it for standard IconRefStr first
if let Ok(v) = concept::IconRefStr::from_str(s) {
Self::Strict(v)
} else {
// fallback with other
Self::Plain(s.to_string())
}
}
}
impl From<concept::IconRefStr> for LosseIconRefStr {
fn from(value: concept::IconRefStr) -> Self {
Self::Strict(value)
}
}
// endregion
// region: Losse String Reference String
/// The enum representing a losse String Reference String.
///
/// In real usage, programmer can use String Reference String,
/// or a plain string as the string setting value.
/// This enum is designed for handling this scenario.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LosseStrRefStr {
Plain(String),
Strict(concept::StrRefStr),
}
impl LosseStrRefStr {
pub fn extract(&self) -> Result<String, Error> {
let rv = match self {
// For plain string, we just simply clone it.
LosseStrRefStr::Plain(v) => v.clone(),
// For string reference string, we try to resolve it.
LosseStrRefStr::Strict(v) => {
let rc = concept::StrRc::new(v.get_path(), v.get_index())?;
rc.into_string()
},
};
Ok(rv)
}
}
impl Display for LosseStrRefStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LosseStrRefStr::Plain(v) => v.fmt(f),
LosseStrRefStr::Strict(v) => v.fmt(f),
}
}
}
impl From<&str> for LosseStrRefStr {
fn from(s: &str) -> Self {
// match it for standard StrRefStr first
if let Ok(v) = concept::StrRefStr::from_str(s) {
Self::Strict(v)
} else {
// fallback with other
Self::Plain(s.to_string())
}
}
}
impl From<concept::StrRefStr> for LosseStrRefStr {
fn from(value: concept::StrRefStr) -> Self {
Self::Strict(value)
}
}
// endregion
// endregion // endregion
// region: Utilities // region: Utilities

View File

@@ -650,6 +650,10 @@ impl StrRc {
pub fn get_string(&self) -> &str { pub fn get_string(&self) -> &str {
&self.inner &self.inner
} }
pub fn into_string(self) -> String {
self.inner
}
} }
// endregion // endregion