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
impl TokenParser
Sourcepub fn has_remaining(&self) -> bool
pub fn has_remaining(&self) -> bool
Check if there are more bytes to parse.
Sourcepub fn peek_token_type(&self) -> Option<TokenType>
pub fn peek_token_type(&self) -> Option<TokenType>
Peek at the next token type without consuming it.
Sourcepub fn next_token(&mut self) -> Result<Option<Token>, ProtocolError>
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.
Sourcepub fn next_token_with_metadata(
&mut self,
metadata: Option<&ColMetaData>,
) -> Result<Option<Token>, ProtocolError>
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.
Sourcepub fn skip_token(&mut self) -> Result<(), ProtocolError>
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.