Coverage Report

Created: 2026-10-28 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/jsonc-parser-0.34.0/src/tokens.rs
Line
Count
Source
1
use super::common::Range;
2
use super::common::Ranged;
3
use std::borrow::Cow;
4
5
/// A token found while scanning.
6
#[derive(Debug, PartialEq, Clone)]
7
pub enum Token<'a> {
8
  OpenBrace,
9
  CloseBrace,
10
  OpenBracket,
11
  CloseBracket,
12
  Comma,
13
  Colon,
14
  String(Cow<'a, str>),
15
  Word(&'a str),
16
  Boolean(bool),
17
  Number(&'a str),
18
  Null,
19
  CommentLine(&'a str),
20
  CommentBlock(&'a str),
21
}
22
23
impl<'a> Token<'a> {
24
0
  pub fn as_str(&self) -> &str {
25
0
    match self {
26
0
      Token::OpenBrace => "{",
27
0
      Token::CloseBrace => "}",
28
0
      Token::OpenBracket => "[",
29
0
      Token::CloseBracket => "]",
30
0
      Token::Comma => ",",
31
0
      Token::Colon => ":",
32
0
      Token::String(value) => value,
33
0
      Token::Word(value) => value,
34
0
      Token::Boolean(value) => {
35
0
        if *value {
36
0
          "true"
37
        } else {
38
0
          "false"
39
        }
40
      }
41
0
      Token::Number(value) => value,
42
0
      Token::Null => "null",
43
0
      Token::CommentLine(value) => value,
44
0
      Token::CommentBlock(value) => value,
45
    }
46
0
  }
47
48
  /// The text of a token that can be an unquoted property name, which in JSON5 includes `true`, `false` and `null`.
49
7.12k
  pub(crate) fn as_loose_property_name(&self) -> Option<&'a str> {
50
7.12k
    match self {
51
40
      Token::Word(value) | Token::Number(value) => Some(value),
52
0
      Token::Boolean(true) => Some("true"),
53
1
      Token::Boolean(false) => Some("false"),
54
1
      Token::Null => Some("null"),
55
7.08k
      _ => None,
56
    }
57
7.12k
  }
58
59
  /// Whether this token can begin a JSON value.
60
74.8k
  pub(crate) fn is_value_start(&self) -> bool {
61
74.6k
    matches!(
62
74.8k
      self,
63
      Token::OpenBrace | Token::OpenBracket | Token::String(_) | Token::Boolean(_) | Token::Number(_) | Token::Null
64
    )
65
74.8k
  }
66
}
67
68
/// A token with positional information.
69
pub struct TokenAndRange<'a> {
70
  pub range: Range,
71
  pub token: Token<'a>,
72
}
73
74
impl<'a> Ranged for TokenAndRange<'a> {
75
0
  fn range(&self) -> Range {
76
0
    self.range
77
0
  }
78
}