realme/value/
key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::expr::Expression;
use crate::errors::RealmeError;

/// Trait for types that can be converted into an `Expression`.
///
/// This trait allows various types to be used as keys when accessing values
/// in a nested structure.
pub trait Key: Copy {
    /// Converts the implementing type into an `Expression`.
    ///
    /// # Returns
    /// - `Ok(Expression)` if the conversion is successful.
    /// - `Err(RealmeError)` if the conversion fails.
    fn to_key(&self) -> Result<Expression, RealmeError>;
}

/// Implements `Key` for string slices.
///
/// String slices are parsed into `Expression`s.
impl Key for &str {
    fn to_key(&self) -> Result<Expression, RealmeError> {
        self.parse()
    }
}

// /// Implements `Key` for `String`.
// ///
// /// `String`s are directly converted into `Expression::Identifier`.
// impl Key for String {
//     fn to_key(&self) -> Result<Expression, RealmeError> {
//         Ok(Expression::Identifier(self.clone()))
//     }
// }

/// Implements `Key` for `isize`.
///
/// `isize` values are converted to strings and then to
/// `Expression::Identifier`.
impl Key for isize {
    fn to_key(&self) -> Result<Expression, RealmeError> {
        Ok(Expression::Identifier(self.to_string()))
    }
}

// impl Key for Expression {
//     fn to_key(&self) -> Result<Expression, RealmeError> {
//         Ok(self.clone())
//     }
// }

impl Key for &Expression {
    fn to_key(&self) -> Result<Expression, RealmeError> {
        Ok((*self).clone())
    }
}