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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
pub enum Item {
    Root,

    Recv,
    Lhs,
    Rhs,
    Value,
    MethodCall,
    Body,
    Args,
    Expr,
    ElseBody,
    Scope,
    Name,
    Superclass,
    Const,
    Definee,
    Iterator,
    Iteratee,
    Pattern,
    Left,
    Right,
    IfTrue,
    IfFalse,
    Cond,
    DefaultValue,
    Ensure,
    Guard,
    As,
    Re,
    Key,
    ExcList,
    ExcVar,
    Match,
    Else,
    Var,
    Options,
    To,
    From,

    MlhsItems,
    Arglist,
    Elements,
    Stmts,
    WhenBodies,
    InBodies,
    Parts,
    Indexes,
    Pairs,
    RescueBodies,

    Idx(usize),
}

#[derive(Debug)]
pub(crate) struct ItemFromStringError {
    unknown_pattern: String,
}

impl std::fmt::Display for ItemFromStringError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "unknown pattern {}", self.unknown_pattern)
    }
}

impl Item {
    pub(crate) fn from_string(s: &str) -> Result<Self, ItemFromStringError> {
        let result = match &s[..] {
            "root" => Self::Root,

            "recv" => Self::Recv,
            "lhs" => Self::Lhs,
            "rhs" => Self::Rhs,
            "value" => Self::Value,
            "method_call" => Self::MethodCall,
            "body" => Self::Body,
            "args" => Self::Args,
            "expr" => Self::Expr,
            "else_body" => Self::ElseBody,
            "scope" => Self::Scope,
            "name" => Self::Name,
            "superclass" => Self::Superclass,
            "const" => Self::Const,
            "definee" => Self::Definee,
            "iterator" => Self::Iterator,
            "iteratee" => Self::Iteratee,
            "pattern" => Self::Pattern,
            "left" => Self::Left,
            "right" => Self::Right,
            "if_true" => Self::IfTrue,
            "if_false" => Self::IfFalse,
            "cond" => Self::Cond,
            "default_value" => Self::DefaultValue,
            "ensure" => Self::Ensure,
            "guard" => Self::Guard,
            "as" => Self::As,
            "re" => Self::Re,
            "key" => Self::Key,
            "exc_list" => Self::ExcList,
            "exc_var" => Self::ExcVar,
            "match" => Self::Match,
            "else" => Self::Else,
            "var" => Self::Var,
            "options" => Self::Options,
            "to" => Self::To,
            "from" => Self::From,

            "mlhs_items" => Self::MlhsItems,
            "arglist" => Self::Arglist,
            "elements" => Self::Elements,
            "stmts" => Self::Stmts,
            "when_bodies" => Self::WhenBodies,
            "in_bodies" => Self::InBodies,
            "parts" => Self::Parts,
            "indexes" => Self::Indexes,
            "pairs" => Self::Pairs,
            "rescue_bodies" => Self::RescueBodies,

            other if other.parse::<usize>().is_ok() => {
                let idx = other.parse::<usize>().unwrap();
                Self::Idx(idx)
            }

            unknown_pattern => {
                return Err(ItemFromStringError {
                    unknown_pattern: unknown_pattern.to_owned(),
                })
            }
        };
        Ok(result)
    }
}