diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:33 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:33 +0000 |
commit | 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 (patch) | |
tree | a4f824bd33cb075dd5aa3eb5a0a94af221bbe83a /third_party/rust/litrs/src/bool/mod.rs | |
parent | Adding debian version 124.0.1-1. (diff) | |
download | firefox-086c044dc34dfc0f74fbe41f4ecb402b2cd34884.tar.xz firefox-086c044dc34dfc0f74fbe41f4ecb402b2cd34884.zip |
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/litrs/src/bool/mod.rs')
-rw-r--r-- | third_party/rust/litrs/src/bool/mod.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/third_party/rust/litrs/src/bool/mod.rs b/third_party/rust/litrs/src/bool/mod.rs new file mode 100644 index 0000000000..d7b54a1b9f --- /dev/null +++ b/third_party/rust/litrs/src/bool/mod.rs @@ -0,0 +1,55 @@ +use std::fmt; + +use crate::{ParseError, err::{perr, ParseErrorKind::*}}; + + +/// A bool literal: `true` or `false`. Also see [the reference][ref]. +/// +/// Notice that, strictly speaking, from Rust point of view "boolean literals" are not +/// actual literals but [keywords]. +/// +/// [ref]: https://doc.rust-lang.org/reference/expressions/literal-expr.html#boolean-literal-expressions +/// [keywords]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BoolLit { + False, + True, +} + +impl BoolLit { + /// Parses the input as a bool literal. Returns an error if the input is + /// invalid or represents a different kind of literal. + pub fn parse(s: &str) -> Result<Self, ParseError> { + match s { + "false" => Ok(Self::False), + "true" => Ok(Self::True), + _ => Err(perr(None, InvalidLiteral)), + } + } + + /// Returns the actual Boolean value of this literal. + pub fn value(self) -> bool { + match self { + Self::False => false, + Self::True => true, + } + } + + /// Returns the literal as string. + pub fn as_str(&self) -> &'static str { + match self { + Self::False => "false", + Self::True => "true", + } + } +} + +impl fmt::Display for BoolLit { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad(self.as_str()) + } +} + + +#[cfg(test)] +mod tests; |