Skip to main content

TextDecoder

Struct TextDecoder 

Source
pub struct TextDecoder<'a> { /* private fields */ }
Available on crate feature text only.
Expand description

Stateful textproto reader.

Drives a Tokenizer and interprets scalar tokens as the requested Rust type. Recursion depth is enforced by the tokenizer’s fixed-size open-stack (see RECURSION_LIMIT).

Implementations§

Source§

impl<'a> TextDecoder<'a>

Source

pub fn new(input: &'a str) -> Self

Create a decoder over input.

Source

pub fn read_field_name(&mut self) -> Result<Option<&'a str>, ParseError>

Read the next field name, or return None at end-of-message / EOF.

The returned slice is borrowed directly from the input. For bracketed type names ([pkg.ext]) it includes the brackets — generated code matches against "[pkg.ext]" literally.

Does not validate that a : separator was present; the colon is optional before message values, so the check is deferred to after the caller dispatches on the name.

§Errors

Any tokenizer error — malformed name, delimiter mismatch, etc.

Source

pub fn unknown_field(&self) -> ParseError

Construct an unknown-field error pointing at the last name returned by read_field_name.

Generated merge_text wildcard arms use this when they want to fail fast on unknown fields rather than skip_value.

Source

pub fn read_i32(&mut self) -> Result<i32, ParseError>

Read an i32 value.

Accepts decimal, 0x hex, 0 octal. Rejects floats and out-of-range.

§Errors

ParseErrorKind::InvalidNumber if not a valid in-range integer, or ParseErrorKind::UnexpectedToken if the value is not a number at all.

Source

pub fn read_i64(&mut self) -> Result<i64, ParseError>

Read an i64 value.

§Errors

As read_i32.

Source

pub fn read_u32(&mut self) -> Result<u32, ParseError>

Read a u32 value. Rejects negatives.

§Errors

As read_i32.

Source

pub fn read_u64(&mut self) -> Result<u64, ParseError>

Read a u64 value. Rejects negatives.

§Errors

As read_i32.

Source

pub fn read_f32(&mut self) -> Result<f32, ParseError>

Read an f32 value.

Accepts any numeric form plus the case-insensitive literals nan, inf, infinity, each optionally with a leading -. Overflow saturates to ±∞ (matching C++ text-format behaviour).

§Errors

ParseErrorKind::InvalidNumber if the token is neither a number nor a recognised float literal.

Source

pub fn read_f64(&mut self) -> Result<f64, ParseError>

Read an f64 value. See read_f32.

§Errors

As read_f32.

Source

pub fn read_bool(&mut self) -> Result<bool, ParseError>

Read a bool value.

Accepts true, True, t, false, False, f, and 0/1 (in any integer base). These are the exact literals the C++ text parser accepts.

§Errors

ParseErrorKind::UnexpectedToken if the token is not a recognised boolean form.

Source

pub fn read_string(&mut self) -> Result<Cow<'a, str>, ParseError>

Read a string value. Unescapes and UTF-8-validates.

Borrows the input when the token is a single literal with no escapes.

§Errors

ParseErrorKind::InvalidString for malformed escapes, ParseErrorKind::InvalidUtf8 if the unescaped bytes are not valid UTF-8.

Source

pub fn read_bytes(&mut self) -> Result<Vec<u8>, ParseError>

Read a bytes value. Unescapes but does not UTF-8-validate.

§Errors

ParseErrorKind::InvalidString for malformed escapes.

Source

pub fn read_enum_by_name<E: Enumeration>(&mut self) -> Result<i32, ParseError>

Read an enum value by variant name or by number (open-enum semantics).

Returns the i32 wire value. Any in-range integer is accepted — the proto3 open-enum model preserves unknown numeric values.

§Errors

ParseErrorKind::UnknownEnumValue if the name is not a known variant; ParseErrorKind::InvalidNumber if a numeric form is out of i32 range.

Source

pub fn read_closed_enum_by_name<E: Enumeration>( &mut self, ) -> Result<E, ParseError>

Read a closed-enum value by variant name or by number.

Returns the enum variant directly. Unknown numeric values are rejected — the proto2 closed-enum model does not accept values outside the defined set in text format. (Binary decode routes them to unknown fields; text format has no analogous mechanism, so it errors.)

§Errors

ParseErrorKind::UnknownEnumValue if the name is not a known variant, or if a numeric form does not map to a defined variant.

Source

pub fn merge_message<M: TextFormat>( &mut self, msg: &mut M, ) -> Result<(), ParseError>

Enter a { or <, merge into msg, then consume the matching close.

§Errors

Any tokenizer or merge_text error, including ParseErrorKind::RecursionLimitExceeded if nesting exceeds RECURSION_LIMIT.

Source

pub fn read_repeated_into<T>( &mut self, out: &mut Vec<T>, read_one: impl FnMut(&mut Self) -> Result<T, ParseError>, ) -> Result<(), ParseError>

Read one-or-more values into out.

Handles both repeated-scalar forms: f: [1, 2, 3] (consumes [ and ]) and f: 1 (reads exactly one element). Generated code calls this once per f occurrence; the f: 1 f: 2 form is handled by the outer read_field_name loop seeing f twice.

§Errors

Any error from read_one or the tokenizer.

Source

pub fn read_any_expansion( &mut self, name: &'a str, ) -> Result<(&'a str, Vec<u8>), ParseError>

Parse an Any-expansion body: the [type_url] { fields } form.

name is the bracketed name as returned by read_field_name, e.g. "[type.googleapis.com/pkg.Foo]". The brackets are stripped here and the result is looked up in the global text-format Any map (installed via set_type_registry); the registered text_merge then consumes the { ... } body and re-encodes to wire bytes suitable for Any.value.

Returns (stripped_url, value_bytes).

§Errors

ParseErrorKind::UnknownField if the URL is not registered — this matches AnyFieldWithInvalidType in the conformance suite, which expects parse failure on an unknown URL.

Source

pub fn read_extension( &mut self, name: &str, extendee: &str, ) -> Result<Vec<UnknownField>, ParseError>

Parse an extension bracket body: the [pkg.ext] { ... } form.

name is the bracketed name as returned by read_field_name. The brackets are stripped and the result is looked up by full_name in the global text-format extension map (installed via set_type_registry); the registered text_merge consumes the value and produces unknown-field records at the extension’s field number.

§Errors

ParseErrorKind::UnknownField if the name is not registered or the registered entry extends a different message. Strict by default — protobuf-go’s prototext behaviour, and what the GroupFieldExtensionGroupName conformance test expects.

Source

pub fn skip_value(&mut self) -> Result<(), ParseError>

Consume a field’s value without interpreting it.

Used for unknown fields when the caller wants to skip rather than fail. Handles scalars, messages, and lists (recursively).

§Errors

Any tokenizer error in the skipped span.

Auto Trait Implementations§

§

impl<'a> Freeze for TextDecoder<'a>

§

impl<'a> RefUnwindSafe for TextDecoder<'a>

§

impl<'a> Send for TextDecoder<'a>

§

impl<'a> Sync for TextDecoder<'a>

§

impl<'a> Unpin for TextDecoder<'a>

§

impl<'a> UnsafeUnpin for TextDecoder<'a>

§

impl<'a> UnwindSafe for TextDecoder<'a>

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.