ReplyError

Derive Macro ReplyError 

Source
#[derive(ReplyError)]
{
    // Attributes available to this derive:
    #[zlink]
}
Expand description

Implements serde::{Serialize, Deserialize} for service error enums.

This macro automatically generates both Serialize and Deserialize implementations for error types that are used in Varlink service replies.

The macro works in both std and no_std environments and requires the “error” field to appear before “parameters” field in JSON for efficient parsing.

§Supported Enum Variants

The macro supports:

  • Unit variants: Variants without any data
  • Named field variants: Variants with named fields

Tuple variants are not supported.

§Attributes

  • interface - This mandatory attribute specifies the Varlink interface name (e.g., “org.varlink.service”)

§Example

use zlink::ReplyError;

#[derive(ReplyError)]
#[zlink(interface = "com.example.MyService")]
enum ServiceError {
    // Unit variant - no parameters
    NotFound,
    PermissionDenied,

    // Named field variant - multiple parameters
    InvalidInput {
        field: String,
        reason: String,
    },

    // Another variant with a single field
    Timeout {
        seconds: u32,
    },
}

// The macro generates:
// - `Serialize` impl that creates properly tagged enum format
// - `Deserialize` impl that handles the tagged enum format efficiently

§Serialization Format

The generated serialization uses a tagged enum format:

// Unit variant:
{"error": "NotFound"}
// or with empty parameters:
{"error": "NotFound", "parameters": null}

// Variant with fields:
{
  "error": "InvalidInput",
  "parameters": {
    "field": "username",
    "reason": "too short"
  }
}