| 1 | //! Would-be-getter rename attempt sucessful result and details. |
| 2 | |
| 3 | use std::fmt::{self, Display}; |
| 4 | |
| 5 | /// Would-be-getter rename attempt sucessful result and details. |
| 6 | /// |
| 7 | /// Holds details about what happened and assumptions on the return type. |
| 8 | #[derive(Debug, PartialEq)] |
| 9 | #[non_exhaustive] |
| 10 | pub struct NewName { |
| 11 | pub(crate) new_name: String, |
| 12 | pub(crate) returns_bool: ReturnsBool, |
| 13 | pub(crate) rule: NewNameRule, |
| 14 | } |
| 15 | |
| 16 | impl NewName { |
| 17 | /// Returns the new name. |
| 18 | pub fn as_str(&self) -> &str { |
| 19 | self.new_name.as_str() |
| 20 | } |
| 21 | |
| 22 | /// Consumes the [`NewName`] and returns the inner new name [`String`]. |
| 23 | pub fn unwrap(self) -> String { |
| 24 | self.new_name |
| 25 | } |
| 26 | |
| 27 | /// Returns current knowledge about the getter returning exactly one `bool`. |
| 28 | pub fn returns_bool(&self) -> ReturnsBool { |
| 29 | self.returns_bool |
| 30 | } |
| 31 | |
| 32 | /// Returns the renaming rule that was used to rename the getter. |
| 33 | pub fn rule(&self) -> NewNameRule { |
| 34 | self.rule |
| 35 | } |
| 36 | |
| 37 | /// Returns whether renaming required fixing the name to comply with rules. |
| 38 | /// |
| 39 | /// Ex. `get_mut_structure` -> `structure_mut`. |
| 40 | pub fn is_fixed(&self) -> bool { |
| 41 | self.rule.is_fixed() |
| 42 | } |
| 43 | |
| 44 | /// Returns whether renaming required substituing (part) of the name. |
| 45 | /// |
| 46 | /// Ex. `get_mute` -> `is_muted`. |
| 47 | pub fn is_substituted(&self) -> bool { |
| 48 | self.rule.is_substituted() |
| 49 | } |
| 50 | |
| 51 | /// Returns whether renaming used the regular strategy. |
| 52 | /// |
| 53 | /// Ex.: |
| 54 | /// * `get_name` -> `name`. |
| 55 | /// * `get_active` -> `is_active`. |
| 56 | pub fn is_regular(&self) -> bool { |
| 57 | self.rule.is_regular() |
| 58 | } |
| 59 | |
| 60 | /// Returns whether renaming didn't use the `is` prefix for `bool` getter. |
| 61 | /// |
| 62 | /// Ex.: |
| 63 | /// * `get_has_entry` -> `has_entry`. |
| 64 | pub fn is_no_prefix(&self) -> bool { |
| 65 | self.rule.is_no_prefix() |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Display for NewName { |
| 70 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 71 | write!(f, "{}{} {}", self.returns_bool, self.rule, self.new_name) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl<T: AsRef<str>> PartialEq<T> for NewName { |
| 76 | fn eq(&self, other: &T) -> bool { |
| 77 | self.as_str() == other.as_ref() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Rule that applied to get the [`NewName`]. |
| 82 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 83 | #[non_exhaustive] |
| 84 | pub enum NewNameRule { |
| 85 | /// Fixed name to comply to rules. Ex. `get_mut_structure` -> `structure_mut`. |
| 86 | Fixed, |
| 87 | /// Regular rule: removal of the prefix or replacement with `is`. |
| 88 | Regular, |
| 89 | /// No prefix for `bool` getter. Ex. `get_has_entry` -> `has_entry`. |
| 90 | NoPrefix, |
| 91 | /// Applied substitution. Ex. `get_mute` -> `is_muted`. |
| 92 | Substituted, |
| 93 | } |
| 94 | |
| 95 | impl NewNameRule { |
| 96 | /// Returns whether renaming required fixing the name to comply with rules. |
| 97 | /// |
| 98 | /// Ex. `get_mut_structure` -> `structure_mut`. |
| 99 | pub fn is_fixed(&self) -> bool { |
| 100 | matches!(self, NewNameRule::Fixed) |
| 101 | } |
| 102 | |
| 103 | /// Returns whether renaming required substituing (part) of the name. |
| 104 | /// |
| 105 | /// Ex. `get_mute` -> `is_muted`. |
| 106 | pub fn is_substituted(&self) -> bool { |
| 107 | matches!(self, NewNameRule::Substituted) |
| 108 | } |
| 109 | |
| 110 | /// Returns whether renaming used the regular strategy. |
| 111 | /// |
| 112 | /// Ex.: |
| 113 | /// * `get_name` -> `name`. |
| 114 | /// * `get_active` -> `is_active`. |
| 115 | pub fn is_regular(&self) -> bool { |
| 116 | matches!(self, NewNameRule::Regular) |
| 117 | } |
| 118 | |
| 119 | /// Returns whether renaming didn't use the `is` prefix for `bool` getter. |
| 120 | /// |
| 121 | /// Ex.: |
| 122 | /// * `get_has_entry` -> `has_entry`. |
| 123 | pub fn is_no_prefix(&self) -> bool { |
| 124 | matches!(self, NewNameRule::NoPrefix) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl Display for NewNameRule { |
| 129 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 130 | use NewNameRule::*; |
| 131 | match self { |
| 132 | Fixed => f.write_str("fixed as"), |
| 133 | Substituted => f.write_str("substituted with"), |
| 134 | NoPrefix => f.write_str("kept as"), |
| 135 | Regular => f.write_str("renamed as"), |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// Indicates current knowledge about the getter returning exaclty one `bool`. |
| 141 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 142 | pub enum ReturnsBool { |
| 143 | True, |
| 144 | False, |
| 145 | Maybe, |
| 146 | } |
| 147 | |
| 148 | impl ReturnsBool { |
| 149 | pub fn is_true(&self) -> bool { |
| 150 | matches!(self, ReturnsBool::True) |
| 151 | } |
| 152 | |
| 153 | pub fn is_false(&self) -> bool { |
| 154 | matches!(self, ReturnsBool::False) |
| 155 | } |
| 156 | |
| 157 | pub fn is_maybe(&self) -> bool { |
| 158 | matches!(self, ReturnsBool::Maybe) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | impl From<bool> for ReturnsBool { |
| 163 | fn from(returns_bool: bool) -> Self { |
| 164 | if returns_bool { |
| 165 | ReturnsBool::True |
| 166 | } else { |
| 167 | ReturnsBool::False |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl Display for ReturnsBool { |
| 173 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 174 | use ReturnsBool::*; |
| 175 | match self { |
| 176 | False => Ok(()), |
| 177 | True => f.write_str("-> bool "), |
| 178 | Maybe => f.write_str("-> ? "), |
| 179 | } |
| 180 | } |
| 181 | } |