use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProfileSection {
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub banner_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tagline: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub categories: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub languages: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timezone: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub accent_color: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serde_round_trip() {
let profile = ProfileSection {
avatar_url: Some("https://example.com/avatar.png".to_string()),
banner_url: None,
tagline: Some("Test agent".to_string()),
categories: vec!["test".to_string()],
languages: vec!["en".to_string()],
timezone: Some("UTC".to_string()),
accent_color: None,
};
let json = serde_json::to_string(&profile).unwrap_or_default();
let parsed: ProfileSection =
serde_json::from_str(&json).unwrap_or_else(|_| profile.clone());
assert_eq!(profile, parsed);
}
#[test]
fn minimal_profile_deserializes() {
let json = r#"{"tagline":"Hello"}"#;
let profile: Result<ProfileSection, _> = serde_json::from_str(json);
assert!(profile.is_ok());
let profile = profile.unwrap_or_else(|_| ProfileSection {
avatar_url: None,
banner_url: None,
tagline: None,
categories: vec![],
languages: vec![],
timezone: None,
accent_color: None,
});
assert_eq!(profile.tagline.as_deref(), Some("Hello"));
assert!(profile.categories.is_empty());
}
#[test]
fn empty_vecs_omitted_in_serialization() {
let profile = ProfileSection {
avatar_url: None,
banner_url: None,
tagline: Some("Minimal".to_string()),
categories: vec![],
languages: vec![],
timezone: None,
accent_color: None,
};
let json = serde_json::to_string(&profile).unwrap_or_default();
assert!(!json.contains("categories"));
assert!(!json.contains("languages"));
}
}