core/types: add custom de/serialization for DbOrUser
Build and test / check (push) Successful in 1m46s
Build and test / check-license (push) Successful in 2m5s
Build and test / test (push) Failing after 2m54s
Build and test / build (push) Successful in 3m0s
Build and test / docs (push) Successful in 5m20s

This commit is contained in:
2026-04-28 07:45:46 +09:00
parent 5444ab46ca
commit 7a9b233611
+32 -1
View File
@@ -105,12 +105,43 @@ impl From<MySQLDatabase> for OsString {
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DbOrUser {
Database(MySQLDatabase),
User(MySQLUser),
}
impl Serialize for DbOrUser {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
DbOrUser::Database(db) => ("d:".to_string() + &db.to_string()).serialize(serializer),
DbOrUser::User(user) => ("u:".to_string() + &user.to_string()).serialize(serializer),
}
}
}
impl<'de> Deserialize<'de> for DbOrUser {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if let Some(rest) = s.strip_prefix("d:") {
Ok(DbOrUser::Database(MySQLDatabase(rest.to_string())))
} else if let Some(rest) = s.strip_prefix("u:") {
Ok(DbOrUser::User(MySQLUser(rest.to_string())))
} else {
Err(serde::de::Error::custom(format!(
"Invalid DbOrUser format: {}",
s
)))
}
}
}
impl DbOrUser {
#[must_use]
pub fn lowercased_noun(&self) -> &'static str {