|
| 1 | +use crate::modifiers::VisibilityModifier; |
| 2 | +use crate::Generator; |
| 3 | +use crate::Indentation; |
| 4 | + |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct Usage { |
| 7 | + pub traits: Vec<String>, |
| 8 | + pub adaptations: Vec<UsageAdaptation>, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub enum UsageAdaptation { |
| 13 | + Alias { |
| 14 | + method: String, |
| 15 | + alias: String, |
| 16 | + visibility: Option<VisibilityModifier>, |
| 17 | + }, |
| 18 | + Visibility { |
| 19 | + method: String, |
| 20 | + visibility: VisibilityModifier, |
| 21 | + }, |
| 22 | + Precedence { |
| 23 | + method: String, |
| 24 | + insteadof: Vec<String>, |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +impl Usage { |
| 29 | + pub fn new(traits: Vec<String>) -> Self { |
| 30 | + Self { |
| 31 | + traits, |
| 32 | + adaptations: vec![], |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub fn with<T: ToString>(mut self, r#trait: T) -> Self { |
| 37 | + self.traits.push(r#trait.to_string()); |
| 38 | + |
| 39 | + self |
| 40 | + } |
| 41 | + |
| 42 | + pub fn alias<T: ToString>( |
| 43 | + mut self, |
| 44 | + method: T, |
| 45 | + alias: T, |
| 46 | + visibility: VisibilityModifier, |
| 47 | + ) -> Self { |
| 48 | + self.adaptations.push(UsageAdaptation::Alias { |
| 49 | + method: method.to_string(), |
| 50 | + alias: alias.to_string(), |
| 51 | + visibility: Some(visibility), |
| 52 | + }); |
| 53 | + |
| 54 | + self |
| 55 | + } |
| 56 | + |
| 57 | + pub fn rename<T: ToString>(mut self, method: T, alias: T) -> Self { |
| 58 | + self.adaptations.push(UsageAdaptation::Alias { |
| 59 | + method: method.to_string(), |
| 60 | + alias: alias.to_string(), |
| 61 | + visibility: None, |
| 62 | + }); |
| 63 | + |
| 64 | + self |
| 65 | + } |
| 66 | + |
| 67 | + pub fn public<T: ToString>(mut self, method: T) -> Self { |
| 68 | + self.adaptations.push(UsageAdaptation::Visibility { |
| 69 | + method: method.to_string(), |
| 70 | + visibility: VisibilityModifier::Public, |
| 71 | + }); |
| 72 | + |
| 73 | + self |
| 74 | + } |
| 75 | + |
| 76 | + pub fn protected<T: ToString>(mut self, method: T) -> Self { |
| 77 | + self.adaptations.push(UsageAdaptation::Visibility { |
| 78 | + method: method.to_string(), |
| 79 | + visibility: VisibilityModifier::Protected, |
| 80 | + }); |
| 81 | + |
| 82 | + self |
| 83 | + } |
| 84 | + |
| 85 | + pub fn private<T: ToString>(mut self, method: T) -> Self { |
| 86 | + self.adaptations.push(UsageAdaptation::Visibility { |
| 87 | + method: method.to_string(), |
| 88 | + visibility: VisibilityModifier::Private, |
| 89 | + }); |
| 90 | + |
| 91 | + self |
| 92 | + } |
| 93 | + |
| 94 | + pub fn visibility<T: ToString>(mut self, method: T, visibility: VisibilityModifier) -> Self { |
| 95 | + self.adaptations.push(UsageAdaptation::Visibility { |
| 96 | + method: method.to_string(), |
| 97 | + visibility, |
| 98 | + }); |
| 99 | + |
| 100 | + self |
| 101 | + } |
| 102 | + |
| 103 | + pub fn precede<T: ToString>(mut self, method: T, insteadof: Vec<T>) -> Self { |
| 104 | + self.adaptations.push(UsageAdaptation::Precedence { |
| 105 | + method: method.to_string(), |
| 106 | + insteadof: insteadof |
| 107 | + .into_iter() |
| 108 | + .map(|insteadof| insteadof.to_string()) |
| 109 | + .collect(), |
| 110 | + }); |
| 111 | + |
| 112 | + self |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl Generator for Usage { |
| 117 | + fn generate(&self, indentation: Indentation, level: usize) -> String { |
| 118 | + let mut code = indentation.indent("use", level); |
| 119 | + |
| 120 | + code.push(' '); |
| 121 | + code.push_str(&self.traits.join(", ")); |
| 122 | + |
| 123 | + if !self.adaptations.is_empty() { |
| 124 | + code.push_str(" {\n"); |
| 125 | + for adaptation in &self.adaptations { |
| 126 | + let adaptation = match adaptation { |
| 127 | + UsageAdaptation::Alias { |
| 128 | + method, |
| 129 | + alias, |
| 130 | + visibility, |
| 131 | + } => { |
| 132 | + let alias = if let Some(visibility) = visibility { |
| 133 | + format!("{} {}", visibility.generate(indentation, level), alias) |
| 134 | + } else { |
| 135 | + alias.to_string() |
| 136 | + }; |
| 137 | + |
| 138 | + indentation.indent(format!("{} as {}", method, alias), level + 1) |
| 139 | + } |
| 140 | + UsageAdaptation::Visibility { method, visibility } => indentation.indent( |
| 141 | + format!("{} as {}", method, visibility.generate(indentation, level)), |
| 142 | + level + 1, |
| 143 | + ), |
| 144 | + UsageAdaptation::Precedence { method, insteadof } => indentation.indent( |
| 145 | + format!("{} insteadof {}", method, insteadof.join(", ")), |
| 146 | + level + 1, |
| 147 | + ), |
| 148 | + }; |
| 149 | + |
| 150 | + code.push_str(&format!("{};\n", adaptation)); |
| 151 | + } |
| 152 | + |
| 153 | + code.push_str(&format!("{}}}\n", indentation.value(level))); |
| 154 | + } else { |
| 155 | + code.push_str(";\n"); |
| 156 | + } |
| 157 | + |
| 158 | + code |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl From<String> for Usage { |
| 163 | + fn from(r#trait: String) -> Self { |
| 164 | + Self::new(vec![r#trait]) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +impl From<&str> for Usage { |
| 169 | + fn from(r#trait: &str) -> Self { |
| 170 | + Self::new(vec![r#trait.to_string()]) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl<T: ToString> From<Vec<T>> for Usage { |
| 175 | + fn from(traits: Vec<T>) -> Self { |
| 176 | + traits |
| 177 | + .into_iter() |
| 178 | + .fold(Self::new(vec![]), |usage, r#trait| usage.with(r#trait)) |
| 179 | + } |
| 180 | +} |
0 commit comments