Created
August 22, 2025 10:02
-
-
Save mrsoftware/b77a02569b8d1fa4f359c53f44364a57 to your computer and use it in GitHub Desktop.
custom Serialize for custom rename snum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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