parse_event

Function parse_event 

Source
pub fn parse_event(message: &ControlMessage) -> Result<ParsedEvent, Error>
Expand description

Parses an event from a control message.

Extracts the event type and content from a 650-status message and returns the appropriate ParsedEvent variant.

§Arguments

  • message - The control message to parse (must have status code 650)

§Returns

A ParsedEvent variant corresponding to the event type. Unknown event types are returned as ParsedEvent::Unknown.

§Errors

Returns Error::Protocol if:

  • The message is empty
  • The status code is not 650
  • The event content is empty
  • The event-specific parsing fails

§Example

use stem_rs::response::{ControlMessage, events::parse_event};
use stem_rs::events::ParsedEvent;

// Parse a circuit event
let msg = ControlMessage::from_str("650 CIRC 1 BUILT", None, true).unwrap();
let event = parse_event(&msg).unwrap();

match event {
    ParsedEvent::Circuit(circ) => {
        println!("Circuit {} status changed", circ.id.0);
    }
    _ => {}
}

// Unknown events are still parsed
let msg = ControlMessage::from_str("650 FUTURE_EVENT data", None, true).unwrap();
let event = parse_event(&msg).unwrap();

match event {
    ParsedEvent::Unknown { event_type, content } => {
        println!("Unknown event: {} - {}", event_type, content);
    }
    _ => {}
}