tr181-derive

Crates.iotr181-derive
lib.rstr181-derive
version0.1.0
created_at2025-10-25 02:30:00.574235+00
updated_at2025-10-25 02:30:00.574235+00
descriptionProcedural macros that generate TR-181 data model access structs and helpers.
homepage
repositoryhttps://github.com/Righ-inc/innocontroller
max_upload_size
id1899575
size45,908
Xavier Qian (FancyQian)

documentation

README

tr181-derive

A Procedural Macro implementation for TR-181 data models access.

Overview

This is a Rust procedural macro that automatically generates data struct and access code for TR-181 data models.

Features

Supported Types (11 types)

  • Option<String> → TR181Value::String
  • Option<u8..u64> → TR181Value::UInt
  • Option<i32, i64> → TR181Value::Int
  • Option<bool> → TR181Value::Bool
  • Option<f32, f64> → TR181Value::Float
  • Option<Vec<u8>> → TR181Value::Bytes
  • Option<Enum> → TR181Value::String (read-only)

Supported Attributes (6 types)

#[tr181(path = "Device.WiFi")]         // Object path
#[tr181(param = "SSIDReference")]      // Custom parameter name
#[tr181(readonly)]                     // Read-only parameter
#[tr181(skip)]                         // Skip field
#[tr181(child)]                        // Child object
#[tr181(table)]                        // Table object (combined with child)

Usage Example

use tr181_derive::TR181Node;

#[derive(TR181Node)]
#[tr181(path = "Device.WiFi.AccessPoint.{i}")]
pub struct AccessPoint {
    pub enable: Option<bool>,
    
    #[tr181(param = "SSIDReference")]
    pub ssid_ref: Option<String>,
    
    #[tr181(readonly)]
    pub status: Option<String>,
    
    #[tr181(child)]
    pub security: Security,
    
    #[tr181(child, table)]
    pub devices: Vec<Device>,
    
    #[tr181(skip)]
    pub internal: String,
}

Generated Code

The macro automatically implements the TR181Node trait, including:

  • get_value() - Get parameter value (with automatic child object navigation)
  • set_value() - Set parameter value (with automatic child object navigation)

Internal Modules

tr181-derive/src/
├── lib.rs         # Main macro implementation
├── attrs.rs       # Attribute parsing
└── type_utils.rs  # Type detection utilities

Testing

Run tests:

cargo test -p tr181-derive

Implementation Details

Name Conversion

Field names (snake_case) are automatically converted to parameter names (PascalCase):

enable         → Enable
ssid_ref       → SsidRef
channel_number → ChannelNumber

Override with param attribute:

#[tr181(param = "SSIDReference")]
pub ssid_ref: Option<String>,  // → "SSIDReference" instead of "SsidRef"

Child Object Navigation

Generated code recursively handles child objects:

// For Device.Security.KeyPassphrase
// 1. Check if Security child object exists
// 2. Recursively call security.get_value("KeyPassphrase")

Table Object Indexing

Table objects support indexed access:

// Device.Devices.{i}.Name
// 1. Parse index {i}
// 2. Access devices[i]
// 3. Get name parameter

Dependencies

[dependencies]
syn = "2.0"           # Rust syntax parsing
quote = "1.0"         # Rust code generation
proc-macro2 = "1.0"   # Proc macro infrastructure
darling = "0.20"      # Attribute parsing
async-trait = "0.1"   # Async trait support

License

BSD-3-Clause

Commit count: 0

cargo fmt