pub struct TextDecoder<'a> { /* private fields */ }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>
impl<'a> TextDecoder<'a>
Sourcepub fn read_field_name(&mut self) -> Result<Option<&'a str>, ParseError>
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.
Sourcepub fn unknown_field(&self) -> ParseError
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.
Sourcepub fn read_i32(&mut self) -> Result<i32, ParseError>
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.
Sourcepub fn read_u32(&mut self) -> Result<u32, ParseError>
pub fn read_u32(&mut self) -> Result<u32, ParseError>
Sourcepub fn read_u64(&mut self) -> Result<u64, ParseError>
pub fn read_u64(&mut self) -> Result<u64, ParseError>
Sourcepub fn read_f32(&mut self) -> Result<f32, ParseError>
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.
Sourcepub fn read_f64(&mut self) -> Result<f64, ParseError>
pub fn read_f64(&mut self) -> Result<f64, ParseError>
Sourcepub fn read_bool(&mut self) -> Result<bool, ParseError>
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.
Sourcepub fn read_string(&mut self) -> Result<Cow<'a, str>, ParseError>
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.
Sourcepub fn read_bytes(&mut self) -> Result<Vec<u8>, ParseError>
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.
Sourcepub fn read_enum_by_name<E: Enumeration>(&mut self) -> Result<i32, ParseError>
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.
Sourcepub fn read_closed_enum_by_name<E: Enumeration>(
&mut self,
) -> Result<E, ParseError>
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.
Sourcepub fn merge_message<M: TextFormat>(
&mut self,
msg: &mut M,
) -> Result<(), ParseError>
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.
Sourcepub fn read_repeated_into<T>(
&mut self,
out: &mut Vec<T>,
read_one: impl FnMut(&mut Self) -> Result<T, ParseError>,
) -> Result<(), ParseError>
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.
Sourcepub fn read_any_expansion(
&mut self,
name: &'a str,
) -> Result<(&'a str, Vec<u8>), ParseError>
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.
Sourcepub fn read_extension(
&mut self,
name: &str,
extendee: &str,
) -> Result<Vec<UnknownField>, ParseError>
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.
Sourcepub fn skip_value(&mut self) -> Result<(), ParseError>
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.