TokenParser

Struct TokenParser 

Source
pub struct TokenParser { /* private fields */ }
Expand description

Token stream parser.

Parses a stream of TDS tokens from a byte buffer.

§Basic vs Context-Aware Parsing

Some tokens (like Done, Error, LoginAck) can be parsed without context. Use next_token() for these.

Other tokens (like ColMetaData, Row, NbcRow) require column metadata to parse correctly. Use next_token_with_metadata() for these.

§Example

let mut parser = TokenParser::new(data);
let mut metadata = None;

while let Some(token) = parser.next_token_with_metadata(metadata.as_ref())? {
    match token {
        Token::ColMetaData(meta) => {
            metadata = Some(meta);
        }
        Token::Row(row) => {
            // Process row using metadata
        }
        Token::Done(done) => {
            if !done.has_more() {
                break;
            }
        }
        _ => {}
    }
}

Implementations§

Source§

impl TokenParser

Source

pub fn new(data: Bytes) -> Self

Create a new token parser from bytes.

Source

pub fn remaining(&self) -> usize

Get remaining bytes in the buffer.

Source

pub fn has_remaining(&self) -> bool

Check if there are more bytes to parse.

Source

pub fn peek_token_type(&self) -> Option<TokenType>

Peek at the next token type without consuming it.

Source

pub fn next_token(&mut self) -> Result<Option<Token>, ProtocolError>

Parse the next token from the stream.

This method can only parse context-independent tokens. For tokens that require column metadata (ColMetaData, Row, NbcRow), use next_token_with_metadata().

Returns None if no more tokens are available.

Source

pub fn next_token_with_metadata( &mut self, metadata: Option<&ColMetaData>, ) -> Result<Option<Token>, ProtocolError>

Parse the next token with optional column metadata context.

When metadata is provided, this method can parse Row and NbcRow tokens. Without metadata, those tokens will return an error.

Returns None if no more tokens are available.

Source

pub fn skip_token(&mut self) -> Result<(), ProtocolError>

Skip the current token without fully parsing it.

This is useful for skipping unknown or uninteresting tokens.

Source

pub fn position(&self) -> usize

Get the current position in the buffer.

Source

pub fn reset(&mut self)

Reset the parser to the beginning.

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> 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, 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.