#[derive(ToWXF)]
{
// Attributes available to this derive:
#[wolfram]
}
Expand description
Derive ToWXF for a struct or enum.
§Structs
Named-field structs encode as a WL Association (<|…|>). Field names are
converted to camelCase by default:
use wolfram_serialize::{ToWXF, to_wxf};
#[derive(ToWXF)]
struct Point {
x: f64, // → "x" key
y: f64, // → "y" key
}
// Encodes as <|"x" -> 1.0, "y" -> 2.0|>
let bytes = to_wxf(&Point { x: 1.0, y: 2.0 }, None).unwrap();Tuple structs encode as a WL List:
use wolfram_serialize::{ToWXF, to_wxf};
#[derive(ToWXF)]
struct Pair(i64, i64);
// Encodes as {1, 2}
let bytes = to_wxf(&Pair(1, 2), None).unwrap();§Enums
Enum variants encode as <|"Enum" -> "VariantName", "Data" -> {fields…}|>.
Unit variants omit the "Data" key:
use wolfram_serialize::{ToWXF, to_wxf};
#[derive(ToWXF)]
enum Color {
Red,
Rgb(u8, u8, u8),
}
// Red → <|"Enum" -> "Red"|>
// Rgb → <|"Enum" -> "Rgb", "Data" -> {255, 0, 0}|>
let _ = to_wxf(&Color::Red, None).unwrap();
let _ = to_wxf(&Color::Rgb(255, 0, 0), None).unwrap();§Special field types
| Rust field type | WL wire encoding |
|---|---|
Vec<u8> | ByteArray[…] |
Vec<f64> / Vec<i64> / … | NumericArray[…, "Real64"] / "Integer64" / … |
Vec<T: WxfStruct> | {…} (List of Associations) |
Option<T> | <|"Enum" -> "Some"/"None", "Data" -> {v}|> |
§Field attributes
use wolfram_serialize::ToWXF;
#[derive(ToWXF)]
struct Config {
#[wolfram(rename = "MaxCount")]
max_count: i64,
}
// Encodes the field as "MaxCount" instead of "maxCount"