ControlMessage

Struct ControlMessage 

Source
pub struct ControlMessage {
    pub status_code: u16,
    pub lines: Vec<String>,
}
Expand description

A parsed control protocol response message.

ControlMessage represents a complete response from Tor’s control interface, containing the status code and all response lines. It provides methods to check success/failure and access the response content.

§Protocol Format

Control protocol responses consist of:

  • A 3-digit status code (similar to HTTP)
  • One or more content lines

Status code ranges:

  • 2xx: Success (command accepted)
  • 4xx: Temporary failure (try again later)
  • 5xx: Permanent failure (command rejected)
  • 6xx: Asynchronous event notification

§Example

use stem_rs::ControlSocket;

let mut socket = ControlSocket::connect_port("127.0.0.1:9051".parse()?).await?;

socket.send("GETINFO version").await?;
let msg = socket.recv().await?;

// Check if successful
if msg.is_ok() {
    // Get first line content
    println!("Version: {}", msg.content());
} else {
    // Handle error
    println!("Error {}: {}", msg.status_code, msg.content());
}

Fields§

§status_code: u16

The 3-digit status code from the response.

Common status codes:

  • 250: Command successful
  • 251: Resource exhausted
  • 451: Resource temporarily unavailable
  • 500: Syntax error
  • 510: Unrecognized command
  • 515: Authentication required/failed
  • 550: Unspecified error
  • 650: Asynchronous event
§lines: Vec<String>

The content lines from the response.

For single-line responses, this contains one element. For multi-line responses, each line is a separate element. For data responses, the data block content is included.

Implementations§

Source§

impl ControlMessage

Source

pub fn is_ok(&self) -> bool

Checks if the response indicates success.

Returns true if the status code is in the 2xx range (200-299), indicating the command was accepted and executed successfully.

§Returns

true if status_code is between 200 and 299 inclusive.

§Example
use stem_rs::socket::ControlMessage;

let success = ControlMessage {
    status_code: 250,
    lines: vec!["OK".to_string()],
};
assert!(success.is_ok());

let error = ControlMessage {
    status_code: 515,
    lines: vec!["Authentication failed".to_string()],
};
assert!(!error.is_ok());
Source

pub fn content(&self) -> &str

Returns the first line of response content.

For most responses, this is the primary content. For multi-line responses, use all_content to get all lines.

§Returns

The first line of content, or an empty string if there are no lines.

§Example
use stem_rs::socket::ControlMessage;

let msg = ControlMessage {
    status_code: 250,
    lines: vec!["version=0.4.7.1".to_string()],
};
assert_eq!(msg.content(), "version=0.4.7.1");

let empty = ControlMessage {
    status_code: 250,
    lines: vec![],
};
assert_eq!(empty.content(), "");
Source

pub fn all_content(&self) -> String

Returns all response lines joined with newlines.

Combines all content lines into a single string, separated by newline characters (\n). Useful for multi-line responses.

§Returns

All lines joined with \n separators.

§Example
use stem_rs::socket::ControlMessage;

let msg = ControlMessage {
    status_code: 250,
    lines: vec![
        "version=0.4.7.1".to_string(),
        "config-file=/etc/tor/torrc".to_string(),
        "OK".to_string(),
    ],
};
assert_eq!(
    msg.all_content(),
    "version=0.4.7.1\nconfig-file=/etc/tor/torrc\nOK"
);
Source

pub fn raw_content(&self) -> String

Returns the raw protocol representation of the response.

Reconstructs the response in control protocol format with status codes and CRLF line endings. Useful for debugging or logging.

§Returns

The response formatted as it would appear on the wire.

§Example
use stem_rs::socket::ControlMessage;

let msg = ControlMessage {
    status_code: 250,
    lines: vec![
        "version=0.4.7.1".to_string(),
        "OK".to_string(),
    ],
};
assert_eq!(msg.raw_content(), "250-version=0.4.7.1\r\n250 OK\r\n");

Trait Implementations§

Source§

impl Clone for ControlMessage

Source§

fn clone(&self) -> ControlMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ControlMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.