Yuki Sireneva 477254f27c silence another warning
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
       --> src/linux/entry.rs:223:10
        |
    223 | #[derive(Deserialize_enum)]
        |          ^^^^^^^^^^^^^^^^
        |          |
        |          `__Command_Bind_Struct` is not local
        |          `Deserialize` is not local
        |          move the `impl` block outside of this constant `_` and up 2 bodies
        |
        = note: the derive macro `Deserialize_enum` defines the non-local `impl`, and may need to be changed
        = note: the derive macro `Deserialize_enum` may come from an old version of the `miniserde_enum` crate, try updating your dependency with `cargo update -p miniserde_enum`
        = note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
        = note: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration for the purpose of this lint
        = note: `#[warn(non_local_definitions)]` on by default
        = note: this warning originates in the derive macro `Deserialize_enum` (in Nightly builds, run with -Z macro-backtrace for more info)
2024-10-22 11:13:31 +03:00
2019-11-18 16:41:23 +01:00
2024-10-22 11:13:31 +03:00
2019-11-19 11:08:34 +01:00
2019-11-18 11:41:49 +01:00
2019-11-18 11:41:49 +01:00
2019-11-18 16:41:23 +01:00

miniserde-enum

Build

This crate exposes derive macros for miniserde's Serialize and Deserialize traits on enums.

The goal of this crate is to provide enum support like that of Serde for miniserde (see Serde's list of enum representations).

Examples

Deserializing an externally tagged enum

use miniserde::{Deserialize, json};
use miniserde_enum::Deserialize_enum;

#[derive(Deserialize_enum, Debug, PartialEq)]
enum External {
    A(i32),
    #[serde(rename = "renamedB")]
    B(i32, String),
    C {
        x: i32,
    },
    D,
}
use External::*;

let example = r#"[{"A":21},{"renamedB":[42,"everything"]},{"C":{"x":2}},"D"]"#;
let actual: Vec<External> = json::from_str(example).unwrap();
let expected = [A(21), B(42, "everything".to_string()), C { x: 2 }, D];
assert_eq!(actual, expected);

Serializing an internally tagged enum

use miniserde::{json, Serialize};
use miniserde_enum::Serialize_enum;

#[serde(tag = "type")]
#[derive(Serialize_enum)]
enum Internal {
    A,
    #[serde(rename = "renamedB")]
    B,
    C {
        x: i32,
    },
}
use Internal::*;

let example = [A, B, C { x: 2 }];
let actual = json::to_string(&example[..]);
let expected = r#"[{"type":"A"},{"type":"renamedB"},{"type":"C","x":2}]"#;
assert_eq!(actual, expected);

More examples can be found in the tests directory.

Limitations

Deserializing internally tagged enums and adjacently tagged enums requires the tag to be the first key in the object, otherwise from_str will return an error.

Additionally, not every enum representation is currently supported (see TODO).

TODO

  • Serialization:
    • Externally tagged enums
    • Internally tagged enums
    • Untagged enums
    • Adjacently tagged enums
  • Deserialization
    • Externally tagged enums
    • Internally tagged enums
    • Untagged enums
    • Adjacently tagged enums

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Description
Added support for optional enum field fork from https://github.com/yuki0iq/miniserde-enum
Readme 74 KiB
Languages
Rust 100%