dfraw_json_parser/parser/metadata/
token.rs

1/// The `TokenComplexity` enum is used to determine how a token is parsed.
2#[allow(clippy::module_name_repetitions)]
3pub enum TokenComplexity {
4    /// The token affects raws by itself with no arguments
5    None,
6    /// The token affects raws and requires a single argument
7    Simple,
8    /// The token affects raws and requires multiple arguments
9    Complex,
10}
11
12#[allow(clippy::module_name_repetitions)]
13#[typetag::serde(tag = "type")]
14/// The `RawObjectToken` trait is implemented by all raw object tokens. This trait is used
15/// to provide a common interface for all raw object tokens, so that they can be
16/// stored in a single vector. It also provides a common interface for parsing.
17pub trait RawObjectToken: RawObjectTokenToAny + std::fmt::Debug + Send + Sync {
18    /// Get the complexity of the token.
19    ///
20    /// The complexity helps determine how the token is parsed.
21    fn get_complexity(&self) -> TokenComplexity;
22    /// Parse a token from a key and value (if any).
23    ///
24    /// Should create a new token of Self and return it.
25    ///
26    /// Arguments:
27    ///
28    /// * `key`: The key of the tag. The first part of a tag, before the colon.
29    /// * `value`: The value of the tag. The second part of a tag, after the colon.
30    /// The `value` might be empty, if there is no value after the colon.
31    ///
32    /// Returns:
33    ///
34    /// * `Option<Self>`: The token that was parsed, or None if the token could not be parsed.
35    fn parse_token(key: &str, value: &str) -> Option<Self>
36    where
37        Self: Sized;
38}
39
40impl std::fmt::Display for dyn RawObjectToken {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{self:?}")
43    }
44}
45
46/// The `RawObjectTokenToAny` trait is implemented by all raw object tokens. This trait is
47/// used to be able to downcast a raw object token to `Any`, so it can be downcast to
48/// a specific raw object token type.
49pub trait RawObjectTokenToAny: 'static {
50    fn as_any(&self) -> &dyn std::any::Any;
51}
52
53impl<T: 'static> RawObjectTokenToAny for T {
54    fn as_any(&self) -> &dyn std::any::Any {
55        self
56    }
57}