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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Would-be-getter rename attempt sucessful result and details.

use std::fmt::{self, Display};

/// Would-be-getter rename attempt sucessful result and details.
///
/// Holds details about what happened and assumptions on the return type.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub struct NewName {
    pub(crate) new_name: String,
    pub(crate) returns_bool: ReturnsBool,
    pub(crate) rule: NewNameRule,
}

impl NewName {
    /// Returns the new name.
    pub fn as_str(&self) -> &str {
        self.new_name.as_str()
    }

    /// Consumes the [`NewName`] and returns the inner new name [`String`].
    pub fn unwrap(self) -> String {
        self.new_name
    }

    /// Returns current knowledge about the getter returning exactly one `bool`.
    pub fn returns_bool(&self) -> ReturnsBool {
        self.returns_bool
    }

    /// Returns the renaming rule that was used to rename the getter.
    pub fn rule(&self) -> NewNameRule {
        self.rule
    }

    /// Returns whether renaming required fixing the name to comply with rules.
    ///
    /// Ex. `get_mut_structure` -> `structure_mut`.
    pub fn is_fixed(&self) -> bool {
        self.rule.is_fixed()
    }

    /// Returns whether renaming required substituing (part) of the name.
    ///
    /// Ex. `get_mute` -> `is_muted`.
    pub fn is_substituted(&self) -> bool {
        self.rule.is_substituted()
    }

    /// Returns whether renaming used the regular strategy.
    ///
    /// Ex.:
    /// * `get_name` -> `name`.
    /// * `get_active` -> `is_active`.
    pub fn is_regular(&self) -> bool {
        self.rule.is_regular()
    }

    /// Returns whether renaming didn't use the `is` prefix for `bool` getter.
    ///
    /// Ex.:
    /// * `get_has_entry` -> `has_entry`.
    pub fn is_no_prefix(&self) -> bool {
        self.rule.is_no_prefix()
    }
}

impl Display for NewName {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}{} {}", self.returns_bool, self.rule, self.new_name)
    }
}

impl<T: AsRef<str>> PartialEq<T> for NewName {
    fn eq(&self, other: &T) -> bool {
        self.as_str() == other.as_ref()
    }
}

/// Rule that applied to get the [`NewName`].
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum NewNameRule {
    /// Fixed name to comply to rules. Ex. `get_mut_structure` -> `structure_mut`.
    Fixed,
    /// Regular rule: removal of the prefix or replacement with `is`.
    Regular,
    /// No prefix for `bool` getter. Ex. `get_has_entry` -> `has_entry`.
    NoPrefix,
    /// Applied substitution. Ex. `get_mute` -> `is_muted`.
    Substituted,
}

impl NewNameRule {
    /// Returns whether renaming required fixing the name to comply with rules.
    ///
    /// Ex. `get_mut_structure` -> `structure_mut`.
    pub fn is_fixed(&self) -> bool {
        matches!(self, NewNameRule::Fixed)
    }

    /// Returns whether renaming required substituing (part) of the name.
    ///
    /// Ex. `get_mute` -> `is_muted`.
    pub fn is_substituted(&self) -> bool {
        matches!(self, NewNameRule::Substituted)
    }

    /// Returns whether renaming used the regular strategy.
    ///
    /// Ex.:
    /// * `get_name` -> `name`.
    /// * `get_active` -> `is_active`.
    pub fn is_regular(&self) -> bool {
        matches!(self, NewNameRule::Regular)
    }

    /// Returns whether renaming didn't use the `is` prefix for `bool` getter.
    ///
    /// Ex.:
    /// * `get_has_entry` -> `has_entry`.
    pub fn is_no_prefix(&self) -> bool {
        matches!(self, NewNameRule::NoPrefix)
    }
}

impl Display for NewNameRule {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use NewNameRule::*;
        match self {
            Fixed => f.write_str("fixed as"),
            Substituted => f.write_str("substituted with"),
            NoPrefix => f.write_str("kept as"),
            Regular => f.write_str("renamed as"),
        }
    }
}

/// Indicates current knowledge about the getter returning exaclty one `bool`.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ReturnsBool {
    True,
    False,
    Maybe,
}

impl ReturnsBool {
    pub fn is_true(&self) -> bool {
        matches!(self, ReturnsBool::True)
    }

    pub fn is_false(&self) -> bool {
        matches!(self, ReturnsBool::False)
    }

    pub fn is_maybe(&self) -> bool {
        matches!(self, ReturnsBool::Maybe)
    }
}

impl From<bool> for ReturnsBool {
    fn from(returns_bool: bool) -> Self {
        if returns_bool {
            ReturnsBool::True
        } else {
            ReturnsBool::False
        }
    }
}

impl Display for ReturnsBool {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use ReturnsBool::*;
        match self {
            False => Ok(()),
            True => f.write_str("-> bool "),
            Maybe => f.write_str("-> ? "),
        }
    }
}