use alloy_primitives::{Address, TxHash, U256};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AvatarType {
#[serde(rename = "CrcV2_RegisterHuman")]
CrcV2RegisterHuman,
#[serde(rename = "CrcV2_RegisterGroup")]
CrcV2RegisterGroup,
#[serde(rename = "CrcV2_RegisterOrganization")]
CrcV2RegisterOrganization,
#[serde(rename = "CrcV1_Signup")]
CrcV1Signup,
#[serde(rename = "CrcV1_OrganizationSignup")]
CrcV1OrganizationSignup,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GeoLocation {
pub lat: f64,
pub lng: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AvatarInfo {
#[serde(default, rename = "blockNumber")]
pub block_number: u64,
#[serde(default)]
pub timestamp: Option<u64>,
#[serde(default, rename = "transactionIndex")]
pub transaction_index: u32,
#[serde(default, rename = "logIndex")]
pub log_index: u32,
#[serde(default, rename = "transactionHash")]
pub transaction_hash: TxHash,
pub version: u32,
#[serde(rename = "type")]
pub avatar_type: AvatarType,
pub avatar: Address,
#[serde(rename = "tokenId")]
pub token_id: Option<U256>,
#[serde(rename = "hasV1")]
pub has_v1: bool,
#[serde(rename = "v1Token")]
pub v1_token: Option<Address>,
#[serde(rename = "cidV0Digest")]
pub cid_v0_digest: Option<String>,
#[serde(rename = "cidV0")]
pub cid_v0: Option<String>,
#[serde(rename = "v1Stopped")]
pub v1_stopped: Option<bool>,
#[serde(rename = "isHuman")]
pub is_human: bool,
pub name: Option<String>,
pub symbol: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Profile {
pub name: String,
pub description: Option<String>,
pub preview_image_url: Option<String>,
pub image_url: Option<String>,
pub location: Option<String>,
pub geo_location: Option<GeoLocation>,
pub extensions: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupProfile {
#[serde(flatten)]
pub profile: Profile,
pub symbol: String,
}
#[cfg(test)]
mod tests {
use super::Profile;
#[test]
fn profile_deserializes_camel_case() {
let json = r#"{
"name": "franco",
"previewImageUrl": "data:image/jpeg;base64,abc",
"imageUrl": "https://example.com/full.jpg",
"location": "Berlin"
}"#;
let profile: Profile = serde_json::from_str(json).expect("should deserialize");
assert_eq!(profile.name, "franco");
assert_eq!(
profile.preview_image_url.as_deref(),
Some("data:image/jpeg;base64,abc")
);
assert_eq!(
profile.image_url.as_deref(),
Some("https://example.com/full.jpg")
);
assert_eq!(profile.location.as_deref(), Some("Berlin"));
}
}