Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Deserializing enum varians

[package]
name = "deserializing-enum-varians"
version = "0.1.0"
edition = "2024"

[dependencies]
serde = { version = "1.0.227", features = ["derive"] }
serde_json = "1.0.145"
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct RequestData {
    name: String,
    email: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct PollNotificationsData {
    poll_interval_ms: u32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum UserFacingApi {
    Request(RequestData),
    PollNotifications(PollNotificationsData),
}

fn main() {
    let request_api = get_request_api("a");
    println!("request: {request_api:?}");

    let notifications_api = get_request_api("b");
    println!("notifications: {notifications_api:?}");
    //notifications: PollNotifications(PollNotificationsData { poll_interval_ms: 1000 })
}
// basic example for deserializing with enum varians from Yoni Peleg

fn get_request_api(which: &str) -> UserFacingApi {
    if which == "a" {
        let request_json = r#"{"Request": {"name": "John", "email": "john.doe@example.com" }}"#;
        let request_api: UserFacingApi = serde_json::from_str(request_json).unwrap();
        request_api
    } else {
        let notification_json = r#"{"PollNotifications": {"poll_interval_ms": 1000 }}"#;
        let notifications_api: UserFacingApi = serde_json::from_str(notification_json).unwrap();
        notifications_api
    }
}

#[test]
fn it_works() {
    use UserFacingApi;

    let request_api = get_request_api("a");
    assert_eq!(
        request_api,
        UserFacingApi::Request(RequestData {
            name: String::from("John"),
            email: String::from("john.doe@example.com")
        })
    );
}