ion_rs/
raw_symbol_token_ref.rs

1use crate::raw_symbol_token::RawSymbolToken;
2use crate::types::SymbolId;
3use crate::Symbol;
4
5/// Like RawSymbolToken, but the Text variant holds a borrowed reference instead of a String.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum RawSymbolTokenRef<'a> {
8    SymbolId(SymbolId),
9    Text(&'a str),
10}
11
12/// Implemented by types that can be viewed as a [RawSymbolTokenRef] without allocations.
13pub trait AsRawSymbolTokenRef {
14    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef;
15}
16
17impl<'a> AsRawSymbolTokenRef for RawSymbolTokenRef<'a> {
18    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
19        match self {
20            RawSymbolTokenRef::SymbolId(sid) => RawSymbolTokenRef::SymbolId(*sid),
21            RawSymbolTokenRef::Text(text) => RawSymbolTokenRef::Text(text),
22        }
23    }
24}
25
26impl AsRawSymbolTokenRef for SymbolId {
27    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
28        RawSymbolTokenRef::SymbolId(*self)
29    }
30}
31
32impl AsRawSymbolTokenRef for String {
33    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
34        RawSymbolTokenRef::Text(self.as_str())
35    }
36}
37
38impl AsRawSymbolTokenRef for &str {
39    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
40        RawSymbolTokenRef::Text(self)
41    }
42}
43
44impl AsRawSymbolTokenRef for Symbol {
45    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
46        match self.text() {
47            Some(text) => RawSymbolTokenRef::Text(text),
48            None => RawSymbolTokenRef::SymbolId(0),
49        }
50    }
51}
52
53impl<T> AsRawSymbolTokenRef for &T
54where
55    T: AsRawSymbolTokenRef,
56{
57    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
58        (*self).as_raw_symbol_token_ref()
59    }
60}
61
62impl AsRawSymbolTokenRef for RawSymbolToken {
63    fn as_raw_symbol_token_ref(&self) -> RawSymbolTokenRef {
64        match self {
65            RawSymbolToken::SymbolId(sid) => RawSymbolTokenRef::SymbolId(*sid),
66            RawSymbolToken::Text(text) => RawSymbolTokenRef::Text(text.as_str()),
67        }
68    }
69}