Last active
November 7, 2025 13:08
-
-
Save lmmx/bbf90e3f43f664e233c22549b04174b1 to your computer and use it in GitHub Desktop.
Constant static str de/ser in facet-json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env rust-script | |
| //! ```cargo | |
| //! [dependencies] | |
| //! facet = "0.30" | |
| //! facet-json = "0.30" | |
| //! ``` | |
| use facet::Facet; | |
| use facet_json; | |
| /// Example of a struct with a constant "type" field. | |
| #[derive(Debug, Clone, Facet)] | |
| pub struct SymbolRule { | |
| /// Always "SYMBOL". | |
| #[facet(rename = "type")] | |
| pub r#type: &'static str, | |
| /// The name of the referenced symbol. | |
| pub name: String, | |
| } | |
| fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| // Deserialize SymbolRule | |
| let json = r#"{"type":"SYMBOL","name":"my_symbol"}"#; | |
| let deserialized: SymbolRule = facet_json::from_str(&json)?; | |
| println!("Deserialized struct: {:#?}", deserialized); | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment