Skip to content

Instantly share code, notes, and snippets.

@lmmx
Last active November 7, 2025 13:08
Show Gist options
  • Select an option

  • Save lmmx/bbf90e3f43f664e233c22549b04174b1 to your computer and use it in GitHub Desktop.

Select an option

Save lmmx/bbf90e3f43f664e233c22549b04174b1 to your computer and use it in GitHub Desktop.
Constant static str de/ser in facet-json
#!/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