ParsedLine

Struct ParsedLine 

Source
pub struct ParsedLine {
    pub status_code: u16,
    pub divider: char,
    pub content: String,
}
Expand description

A parsed line from a Tor control protocol response.

Represents a single line of a control protocol response, broken down into its component parts: status code, divider character, and content.

§Protocol Format

Each response line has the format: STATUS DIVIDER CONTENT

  • status_code: 3-digit numeric code indicating success/failure
  • divider: Single character indicating line type
  • content: The actual message content

§Divider Types

DividerMethodMeaning
(space)is_final()Final line of response
-is_continuation()More lines follow
+is_data()Multi-line data block follows

§Example

use stem_rs::protocol::ParsedLine;

// Parse a success response
let line = ParsedLine::parse("250 OK").unwrap();
assert_eq!(line.status_code, 250);
assert_eq!(line.divider, ' ');
assert_eq!(line.content, "OK");
assert!(line.is_final());

// Parse a continuation line
let cont = ParsedLine::parse("250-version=0.4.7.1").unwrap();
assert!(cont.is_continuation());

// Parse a data line
let data = ParsedLine::parse("250+getinfo").unwrap();
assert!(data.is_data());

// Parse an error response
let err = ParsedLine::parse("515 Authentication failed").unwrap();
assert_eq!(err.status_code, 515);

Fields§

§status_code: u16

The 3-digit status code from the response.

Common codes:

  • 250: Success
  • 251: Operation unnecessary
  • 5xx: Various error conditions
  • 650: Asynchronous event notification
§divider: char

The divider character indicating the line type.

  • ' ' (space): Final line of the response
  • '-': Continuation line (more lines follow)
  • '+': Data line (multi-line data block follows)
§content: String

The content of the response line after the status code and divider.

For success responses, this is typically “OK”. For error responses, this contains the error description. For data responses, this may contain key=value pairs or other data.

Implementations§

Source§

impl ParsedLine

Source

pub fn parse(line: &str) -> Result<Self, Error>

Parses a raw control protocol response line into its components.

Takes a line from the control socket and extracts the status code, divider character, and content. The line may optionally include trailing \r\n characters which are stripped.

§Arguments
  • line - The raw line to parse, with or without trailing CRLF
§Returns

A ParsedLine containing the extracted components.

§Errors

Returns Error::Protocol if:

  • The line is shorter than 3 characters
  • The first 3 characters are not a valid numeric status code
§Example
use stem_rs::protocol::ParsedLine;

// Parse with CRLF (as received from socket)
let line = ParsedLine::parse("250 OK\r\n").unwrap();
assert_eq!(line.status_code, 250);
assert_eq!(line.content, "OK");

// Parse without CRLF
let line = ParsedLine::parse("250-version=0.4.7.1").unwrap();
assert_eq!(line.content, "version=0.4.7.1");

// Error: line too short
assert!(ParsedLine::parse("25").is_err());

// Error: invalid status code
assert!(ParsedLine::parse("abc OK").is_err());
Source

pub fn is_final(&self) -> bool

Returns true if this is the final line of a response.

A final line has a space character as its divider, indicating no more lines follow in this response.

§Example
use stem_rs::protocol::ParsedLine;

let final_line = ParsedLine::parse("250 OK").unwrap();
assert!(final_line.is_final());

let cont_line = ParsedLine::parse("250-more data").unwrap();
assert!(!cont_line.is_final());
Source

pub fn is_continuation(&self) -> bool

Returns true if this is a continuation line.

A continuation line has a - character as its divider, indicating more lines follow in this response.

§Example
use stem_rs::protocol::ParsedLine;

let cont_line = ParsedLine::parse("250-version=0.4.7.1").unwrap();
assert!(cont_line.is_continuation());

let final_line = ParsedLine::parse("250 OK").unwrap();
assert!(!final_line.is_continuation());
Source

pub fn is_data(&self) -> bool

Returns true if this is a data line.

A data line has a + character as its divider, indicating a multi-line data block follows. The data block is terminated by a line containing only a period (.).

§Example
use stem_rs::protocol::ParsedLine;

let data_line = ParsedLine::parse("250+getinfo").unwrap();
assert!(data_line.is_data());

let final_line = ParsedLine::parse("250 OK").unwrap();
assert!(!data_line.is_final());

Trait Implementations§

Source§

impl Clone for ParsedLine

Source§

fn clone(&self) -> ParsedLine

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 ParsedLine

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ParsedLine

Source§

fn eq(&self, other: &ParsedLine) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ParsedLine

Source§

impl StructuralPartialEq for ParsedLine

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.