Skip to main content

Module singleton_map

Module singleton_map 

Expand description

Serialize enums as single-entry maps.

This module provides serialize and deserialize functions for use with serde’s #[serde(with = "...")] attribute to serialize enum variants as single-entry maps where the key is the variant name.

§Examples

use noyalib::with::singleton_map;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
enum Status {
    Active,
    Pending { reason: String },
    Error { code: i32, message: String },
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Task {
    name: String,
    #[serde(with = "singleton_map")]
    status: Status,
}

let task = Task {
    name: "example".to_string(),
    status: Status::Pending {
        reason: "waiting".to_string(),
    },
};

let yaml = noyalib::to_string(&task).unwrap();
// Output:
// name: example
// status:
//   Pending:
//     reason: waiting

Functions§

deserialize
Deserialize a value from a singleton map.
serialize
Serialize a value as a singleton map.