core/str/error.rs
1//! Defines utf8 error type.
2
3#[cfg(not(feature = "ferrocene_certified"))]
4use crate::error::Error;
5#[cfg(not(feature = "ferrocene_certified"))]
6use crate::fmt;
7
8/// Errors which can occur when attempting to interpret a sequence of [`u8`]
9/// as a string.
10///
11/// As such, the `from_utf8` family of functions and methods for both [`String`]s
12/// and [`&str`]s make use of this error, for example.
13///
14/// [`String`]: ../../std/string/struct.String.html#method.from_utf8
15/// [`&str`]: super::from_utf8
16///
17/// # Examples
18///
19/// This error type’s methods can be used to create functionality
20/// similar to `String::from_utf8_lossy` without allocating heap memory:
21///
22/// ```
23/// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
24/// loop {
25/// match std::str::from_utf8(input) {
26/// Ok(valid) => {
27/// push(valid);
28/// break
29/// }
30/// Err(error) => {
31/// let (valid, after_valid) = input.split_at(error.valid_up_to());
32/// unsafe {
33/// push(std::str::from_utf8_unchecked(valid))
34/// }
35/// push("\u{FFFD}");
36///
37/// if let Some(invalid_sequence_length) = error.error_len() {
38/// input = &after_valid[invalid_sequence_length..]
39/// } else {
40/// break
41/// }
42/// }
43/// }
44/// }
45/// }
46/// ```
47#[cfg_attr(not(feature = "ferrocene_certified"), derive(Copy, Eq, PartialEq, Clone, Debug))]
48#[stable(feature = "rust1", since = "1.0.0")]
49pub struct Utf8Error {
50 pub(super) valid_up_to: usize,
51 pub(super) error_len: Option<u8>,
52}
53
54impl Utf8Error {
55 /// Returns the index in the given string up to which valid UTF-8 was
56 /// verified.
57 ///
58 /// It is the maximum index such that `from_utf8(&input[..index])`
59 /// would return `Ok(_)`.
60 ///
61 /// # Examples
62 ///
63 /// Basic usage:
64 ///
65 /// ```
66 /// use std::str;
67 ///
68 /// // some invalid bytes, in a vector
69 /// let sparkle_heart = vec![0, 159, 146, 150];
70 ///
71 /// // std::str::from_utf8 returns a Utf8Error
72 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
73 ///
74 /// // the second byte is invalid here
75 /// assert_eq!(1, error.valid_up_to());
76 /// ```
77 #[stable(feature = "utf8_error", since = "1.5.0")]
78 #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
79 #[must_use]
80 #[inline]
81 pub const fn valid_up_to(&self) -> usize {
82 self.valid_up_to
83 }
84
85 /// Provides more information about the failure:
86 ///
87 /// * `None`: the end of the input was reached unexpectedly.
88 /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
89 /// If a byte stream (such as a file or a network socket) is being decoded incrementally,
90 /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
91 ///
92 /// * `Some(len)`: an unexpected byte was encountered.
93 /// The length provided is that of the invalid byte sequence
94 /// that starts at the index given by `valid_up_to()`.
95 /// Decoding should resume after that sequence
96 /// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
97 /// lossy decoding.
98 ///
99 /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
100 #[stable(feature = "utf8_error_error_len", since = "1.20.0")]
101 #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
102 #[must_use]
103 #[inline]
104 pub const fn error_len(&self) -> Option<usize> {
105 // FIXME(const-hack): This should become `map` again, once it's `const`
106 match self.error_len {
107 Some(len) => Some(len as usize),
108 None => None,
109 }
110 }
111}
112
113#[stable(feature = "rust1", since = "1.0.0")]
114#[cfg(not(feature = "ferrocene_certified"))]
115impl fmt::Display for Utf8Error {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 if let Some(error_len) = self.error_len {
118 write!(
119 f,
120 "invalid utf-8 sequence of {} bytes from index {}",
121 error_len, self.valid_up_to
122 )
123 } else {
124 write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
125 }
126 }
127}
128
129#[stable(feature = "rust1", since = "1.0.0")]
130#[cfg(not(feature = "ferrocene_certified"))]
131impl Error for Utf8Error {}
132
133/// An error returned when parsing a `bool` using [`from_str`] fails
134///
135/// [`from_str`]: super::FromStr::from_str
136#[derive(Debug, Clone, PartialEq, Eq)]
137#[non_exhaustive]
138#[stable(feature = "rust1", since = "1.0.0")]
139#[cfg(not(feature = "ferrocene_certified"))]
140pub struct ParseBoolError;
141
142#[stable(feature = "rust1", since = "1.0.0")]
143#[cfg(not(feature = "ferrocene_certified"))]
144impl fmt::Display for ParseBoolError {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 "provided string was not `true` or `false`".fmt(f)
147 }
148}
149
150#[stable(feature = "rust1", since = "1.0.0")]
151#[cfg(not(feature = "ferrocene_certified"))]
152impl Error for ParseBoolError {}