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

§Enum-level attributes

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

§Field-level attributes

  • rename = "..." - Specifies a custom name for the field in the JSON representation
  • borrow - Enables zero-copy deserialization for types like Cow<'_, str>

§Example

use std::borrow::Cow;
use zlink::ReplyError;

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

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

    // Variant with zero-copy deserialization using borrow
    CustomError {
        #[zlink(borrow)]
        message: Cow<'a, str>,
    },

    // Variant with renamed field
    Timeout {
        #[zlink(rename = "timeoutSeconds")]
        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"
  }
}