Skip to content

Instantly share code, notes, and snippets.

@mrsoftware
Created August 22, 2025 10:02
Show Gist options
  • Save mrsoftware/b77a02569b8d1fa4f359c53f44364a57 to your computer and use it in GitHub Desktop.
Save mrsoftware/b77a02569b8d1fa4f359c53f44364a57 to your computer and use it in GitHub Desktop.
custom Serialize for custom rename snum
use serde::{Serialize, Serializer};
#[derive(Clone, Debug, Copy)]
pub enum ComparisonOperators {
EQ(i64),
NEQ(Provider),
}
#[derive(Serialize, Clone, Debug, Copy)]
pub enum Provider {
#[serde(rename = "discord")]
ProviderDiscord,
#[serde(rename = "google")]
ProviderGoogle,
}
impl Serialize for ComparisonOperators {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ComparisonOperators::EQ(value) => { serializer.serialize_str(&format!("eq_{}", value)) }
ComparisonOperators::NEQ(value) => {
let inner = serde_json::to_string(value).unwrap();
serializer.serialize_str(&format!("neq_{}", inner.trim_matches('"')))
}
}
}
}
fn main() {
println!("{}", serde_json::to_string(&ComparisonOperators::EQ(10)).unwrap());
println!("{}", serde_json::to_string(&ComparisonOperators::NEQ(Provider::ProviderDiscord)).unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment