{"id":16893136,"url":"https://github.com/dtolnay/serde-untagged","last_synced_at":"2025-04-13T00:46:05.168Z","repository":{"id":191065224,"uuid":"683248712","full_name":"dtolnay/serde-untagged","owner":"dtolnay","description":"Serde Visitor for deserializing untagged enums","archived":false,"fork":false,"pushed_at":"2025-03-03T07:51:32.000Z","size":154,"stargazers_count":62,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T00:45:33.079Z","etag":null,"topics":["serde"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtolnay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"dtolnay"}},"created_at":"2023-08-26T01:18:54.000Z","updated_at":"2025-03-24T06:15:35.000Z","dependencies_parsed_at":"2023-12-21T21:29:09.710Z","dependency_job_id":"8f90c5ce-8c55-4ac1-9a7b-d41cdea1ac3a","html_url":"https://github.com/dtolnay/serde-untagged","commit_stats":null,"previous_names":["dtolnay/serde-untagged"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fserde-untagged","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fserde-untagged/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fserde-untagged/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fserde-untagged/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtolnay","download_url":"https://codeload.github.com/dtolnay/serde-untagged/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650420,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["serde"],"created_at":"2024-10-13T17:13:47.138Z","updated_at":"2025-04-13T00:46:05.141Z","avatar_url":"https://github.com/dtolnay.png","language":"Rust","funding_links":["https://github.com/sponsors/dtolnay"],"categories":[],"sub_categories":[],"readme":"serde-untagged\n==============\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-dtolnay/serde--untagged-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\" height=\"20\"\u003e](https://github.com/dtolnay/serde-untagged)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/serde-untagged.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"20\"\u003e](https://crates.io/crates/serde-untagged)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-serde--untagged-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\" height=\"20\"\u003e](https://docs.rs/serde-untagged)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/dtolnay/serde-untagged/ci.yml?branch=master\u0026style=for-the-badge\" height=\"20\"\u003e](https://github.com/dtolnay/serde-untagged/actions?query=branch%3Amaster)\n\nThis crate provides a Serde `Visitor` implementation that is useful for\ndeserializing untagged enums.\n\n```toml\n[dependencies]\nserde-untagged = \"0.1\"\n```\n\n\u003cbr\u003e\n\nUntagged enum `Deserialize` impls look like this:\n\n```rust\nuse serde::de::{Deserialize, Deserializer};\nuse serde_untagged::UntaggedEnumVisitor;\n\nimpl\u003c'de\u003e Deserialize\u003c'de\u003e for $MyType {\n    fn deserialize\u003cD\u003e(deserializer: D) -\u003e Result\u003cSelf, D::Error\u003e\n    where\n        D: Deserializer\u003c'de\u003e,\n    {\n        UntaggedEnumVisitor::new()\n            /*\n             *\n             */\n            .deserialize(deserializer)\n    }\n}\n```\n\nInside the `/* ... */`, we list each type that the untagged enum needs to\nsupport deserializing from, giving a closure that turns the input into $MyType.\nThe following types are supported:\n\n- bool\n- i8, i16, i32, i64, i128, u8, u16, u32, u64, u128\n- f32\n- f64\n- char\n- string\n- borrowed\\_str\n- bytes\n- borrowed\\_bytes\n- byte\\_buf\n- unit\n- seq\n- map\n\n### Example: string or struct\n\nCargo's `http.ssl-version` configuration supports deserialization from the\nfollowing two representations:\n\n```toml\n[http]\nssl-version = \"tlsv1.3\"\n```\n\n```toml\n[http]\nssl-version.min = \"tlsv1.2\"\nssl-version.max = \"tlsv1.3\"\n```\n\n```rust\nuse serde::de::{Deserialize, Deserializer};\nuse serde_derive::Deserialize;\nuse serde_untagged::UntaggedEnumVisitor;\n\npub enum SslVersionConfig {\n    Single(String),\n    Range(SslVersionConfigRange),\n}\n\nimpl\u003c'de\u003e Deserialize\u003c'de\u003e for SslVersionConfig {\n    fn deserialize\u003cD\u003e(deserializer: D) -\u003e Result\u003cSelf, D::Error\u003e\n    where\n        D: Deserializer\u003c'de\u003e,\n    {\n        UntaggedEnumVisitor::new()\n            .string(|single| Ok(SslVersionConfig::Single(single.to_owned())))\n            .map(|map| map.deserialize().map(SslVersionConfig::Range))\n            .deserialize(deserializer)\n    }\n}\n\n#[derive(Deserialize)]\npub struct SslVersionConfigRange {\n    pub min: Option\u003cString\u003e,\n    pub max: Option\u003cString\u003e,\n}\n```\n\n### Example: unit variant or bool\n\nCargo's LTO setting in profiles supports the 5 values `false`, `true`, `\"fat\"`,\n`\"thin\"`, and `\"off\"`.\n\n```toml\n[profile.release]\nlto = \"thin\"\n```\n\n```rust\nuse serde::de::{Deserialize, Deserializer, IntoDeserializer};\nuse serde_derive::Deserialize;\nuse serde_untagged::UntaggedEnumVisitor;\n\npub enum LinkTimeOptimization {\n    Enabled(bool),\n    Enum(LinkTimeOptimizationString),\n}\n\nimpl\u003c'de\u003e Deserialize\u003c'de\u003e for LinkTimeOptimization {\n    fn deserialize\u003cD\u003e(deserializer: D) -\u003e Result\u003cSelf, D::Error\u003e\n    where\n        D: Deserializer\u003c'de\u003e,\n    {\n        UntaggedEnumVisitor::new()\n            .bool(|b| Ok(LinkTimeOptimization::Enabled(b)))\n            .string(|string| {\n                let de = string.into_deserializer();\n                LinkTimeOptimizationString::deserialize(de).map(LinkTimeOptimization::Enum)\n            })\n            .deserialize(deserializer)\n    }\n}\n\n#[derive(Deserialize)]\n#[serde(rename = \"lowercase\")]\npub enum LinkTimeOptimizationString {\n    Fat,\n    Thin,\n    Off,\n}\n```\n\nSince `lto = true` means the same thing as `lto = \"fat\"` to Cargo, there are\nreally only 4 distinct options. This type could be implemented alternatively as:\n\n```rust\nuse serde::de::{Deserialize, Deserializer, Unexpected};\nuse serde_untagged::UntaggedEnumVisitor;\n\npub enum LinkTimeOptimization {\n    ThinLocal,  // false\n    Fat,        // true or \"fat\"\n    Thin,       // \"thin\"\n    Off,        // \"off\"\n}\n\nimpl\u003c'de\u003e Deserialize\u003c'de\u003e for LinkTimeOptimization {\n    fn deserialize\u003cD\u003e(deserializer: D) -\u003e Result\u003cSelf, D::Error\u003e\n    where\n        D: Deserializer\u003c'de\u003e,\n    {\n        UntaggedEnumVisitor::new()\n            .bool(|b| match b {\n                false =\u003e Ok(LinkTimeOptimization::ThinLocal),\n                true =\u003e Ok(LinkTimeOptimization::Fat),\n            })\n            .string(|string| match string {\n                \"fat\" =\u003e Ok(LinkTimeOptimization::Fat),\n                \"thin\" =\u003e Ok(LinkTimeOptimization::Thin),\n                \"off\" =\u003e Ok(LinkTimeOptimization::Off),\n                _ =\u003e Err(serde::de::Error::invalid_value(\n                    Unexpected::Str(string),\n                    \u0026r#\"\"fat\" or \"thin\" or \"off\"\"#,\n                )),\n            })\n            .deserialize(deserializer)\n    }\n}\n```\n\n\u003cbr\u003e\n\n#### License\n\n\u003csup\u003e\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\u003c/sup\u003e\n\n\u003cbr\u003e\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n\u003c/sub\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fserde-untagged","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtolnay%2Fserde-untagged","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fserde-untagged/lists"}