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/failuredivider: Single character indicating line typecontent: The actual message content
§Divider Types
| Divider | Method | Meaning |
|---|---|---|
(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: u16The 3-digit status code from the response.
Common codes:
250: Success251: Operation unnecessary5xx: Various error conditions650: Asynchronous event notification
divider: charThe 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: StringThe 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
impl ParsedLine
Sourcepub fn parse(line: &str) -> Result<Self, Error>
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());Sourcepub fn is_final(&self) -> bool
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());Sourcepub fn is_continuation(&self) -> bool
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());Sourcepub fn is_data(&self) -> bool
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
impl Clone for ParsedLine
Source§fn clone(&self) -> ParsedLine
fn clone(&self) -> ParsedLine
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more