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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! A crate for creating templates from Rust source files.
//!
//! # Features
//!
//! ### Valid Rust
//!
//! - All templates can be compilable Rust source files
//! - Linting & language servers work
//!
//! ### Extensible
//!
//! - Implement [`Rule`] to add new rules
//!
//! # Example
//!
//! ```rust
//! use replacer::{Rule, StringRule, TemplateBuilder};
//!
//! fn main() -> anyhow::Result<()> {
//!     let template = TemplateBuilder::new()
//!         .rule(StringRule::new("replace", "world")?)
//!         .build();
//!
//!     assert_eq!(template.apply("Hello $$replace$$!")?, "Hello world!");
//!
//!     Ok(())
//! }
//! ```
//!
//! # Rules
//!
//! ### [`StringRule`]
//!
//! ```rust
//! let some_str = "Hello $$replace_with_world$$!";
//! # assert_eq!(some_str, "Hello $$replace_with_world$$!");
//!
//! // Also works in comments, hello $$replace_with_world$$
//! ```
//!
//! ### [`TypeRule`]
//!
//! ```rust
//! // Unfortunately the type needs to be wrapped with angle brackets here
//! let some_type = <replacer::rust_type!(replace_with_type; String;)>::new();
//! # assert_eq!(some_type, "");
//!
//! let some_generic_type: Vec<replacer::rust_type!(replace_with_type_in_vec; i32;)> = vec![];
//! # assert_eq!(some_generic_type, vec![]);
//! ```
//!
//! ### [`StructRule`]
//!
//! ```rust
//! replacer::rust_struct!(replace_with_struct; Point; x: i32, y: i32;);
//! ```

use anyhow::Result;
use regex::{Captures, Regex};

/// Template macro for replacing a Rust type with a placeholder type that can be compiled.
///
/// ```rust
/// let some_type = <replacer::rust_type!(replace_with_type; String;)>::new();
/// # assert_eq!(some_type, "");
/// ```
#[macro_export]
macro_rules! rust_type {
    ($_name:ident; $placeholder:ty;) => {
        $placeholder
    };
}

/// Template macro for replacing a Rust struct with a placeholder struct that can be compiled.
///
/// ```rust
/// replacer::rust_struct!(replace_with_struct; Point; x: i32, y: i32;);
/// ```
#[macro_export]
macro_rules! rust_struct {
    ($_name:ident; $placeholder:ident; $($element: ident: $ty: ty),*;) => {
        struct $placeholder { $($element: $ty),* }
    };
}

/// Generic way to add rules for a single file.
///
/// This trait can be implemented on a struct or enum for custom template handling.
pub trait Rule {
    /// Convert the matched values to a string.
    fn convert(&self, template: &str) -> Result<String>;
}

/// Replace a string inside another string.
///
/// This will look for any code containing the `${..}` sequence where `..` is
/// filled with the matches.
/// ```rust
/// # use replacer::{Rule, StringRule};
/// # fn main() -> anyhow::Result<()> {
/// let rule = StringRule::new("replace", "world")?;
/// assert_eq!(rule.convert("Hello $$replace$$!")?, "Hello world!");
/// # Ok(())
/// # }
/// ```
pub struct StringRule {
    /// The keyword that will be matched with.
    /// This is the `${..}` part in the string.
    matches: String,
    /// What the keyword will be replaced with.
    replace_with: String,
}

impl Rule for StringRule {
    fn convert(&self, template: &str) -> Result<String> {
        Ok(template.replace(&self.matches, &self.replace_with))
    }
}

impl StringRule {
    /// Setup a new rule.
    pub fn new(matches: &str, replace_with: &str) -> Result<Self> {
        Ok(Self {
            matches: format!("$${}$$", matches),
            replace_with: replace_with.to_string(),
        })
    }
}

/// Replace a Rust type.
/// ```rust
/// # use replacer::{Rule, TypeRule};
/// # fn main() -> anyhow::Result<()> {
/// let rule = TypeRule::new("replace_with_type", "PathBuf")?;
/// assert_eq!(rule.convert("let some_type = <replacer::rust_type!(replace_with_type; String;)>::new();")?, "let some_type = <PathBuf>::new();");
/// # Ok(())
/// # }
/// ```
pub struct TypeRule {
    /// What the keyword will be replaced with.
    replace_with: String,
    /// Regex used to find the macro.
    regex: Regex,
}

impl Rule for TypeRule {
    fn convert(&self, template: &str) -> Result<String> {
        let replace_with: &str = &self.replace_with;
        let replace = self.regex.replace_all(template, replace_with);

        Ok(replace.into_owned())
    }
}

impl TypeRule {
    /// Setup a new rule.
    pub fn new(matches: &str, replace_with: &str) -> Result<Self> {
        let regex = Regex::new(&format!(r"replacer::rust_type!\({};[^;]+;\)", matches))?;

        Ok(Self {
            replace_with: replace_with.to_string(),
            regex,
        })
    }
}

/// Replace a Rust struct.
/// ```rust
/// # use replacer::{Rule, StructRule};
/// # fn main() -> anyhow::Result<()> {
/// let rule = StructRule::new("replace_with_struct", "Point2D")?;
/// assert_eq!(rule.convert("replacer::rust_struct!(replace_with_struct; Point; x: i32, y: i32;}")?,
///     "struct Point2D { x: i32, y: i32 }");
/// # Ok(())
/// # }
/// ```
pub struct StructRule {
    /// What the keyword will be replaced with.
    replace_with: String,
    /// Regex used to find the macro.
    regex: Regex,
}

impl Rule for StructRule {
    fn convert(&self, template: &str) -> Result<String> {
        let replace_with: &str = &self.replace_with;
        let replace = self.regex.replace_all(template, |caps: &Captures| {
            format!("struct {} {{{} }}", replace_with, &caps[1])
        });

        Ok(replace.into_owned())
    }
}

impl StructRule {
    /// Setup a new rule.
    pub fn new(matches: &str, replace_with: &str) -> Result<Self> {
        let regex = Regex::new(&format!(
            r"replacer::rust_struct!\s*[\({{]{};[^;]+;([^;]+);[\)}}]",
            matches
        ))?;

        Ok(Self {
            replace_with: replace_with.to_string(),
            regex,
        })
    }
}

/// Builder for the [`Template`] struct.
#[derive(Default)]
pub struct TemplateBuilder {
    rules: Vec<Box<dyn Rule>>,
}

impl TemplateBuilder {
    /// Start building a new [`Template`] struct.
    pub fn new() -> Self {
        Self { rules: vec![] }
    }

    /// Add a new rule that can be applied in batch.
    ///
    /// A rule is defined by anything that implements the [`Rule`] trait.
    ///
    /// ```rust
    /// # use replacer::{Rule, StringRule, TemplateBuilder};
    /// # fn main() -> anyhow::Result<()> {
    /// let template = TemplateBuilder::new()
    ///     .rule(StringRule::new("replace", "world")?)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn rule<R>(mut self, rule: R) -> Self
    where
        R: Rule + 'static,
    {
        self.rules.push(Box::new(rule));

        self
    }

    /// Create the [`Template`] struct.
    pub fn build(self) -> Template {
        Template { rules: self.rules }
    }
}

/// Internal representation of the template file.
///
/// Use [`TemplateBuilder`] to instaniate a new Template.
///
/// ```rust
/// # use replacer::{Rule, StringRule, TemplateBuilder};
/// # fn main() -> anyhow::Result<()> {
/// let template = TemplateBuilder::new()
///     .rule(StringRule::new("replace", "world")?)
///     .build();
///
/// assert_eq!(template.apply("Hello $$replace$$")?, "Hello world");
/// # Ok(())
/// # }
/// ```
pub struct Template {
    rules: Vec<Box<dyn Rule>>,
}

impl Template {
    /// Apply all rules sequentially or return the first error.
    pub fn apply(&self, code: &str) -> Result<String> {
        self.rules
            .iter()
            .fold(Ok(code.to_string()), |code, rule| match code {
                // Apply the rule and return the string if there are no errors
                Ok(code) => rule.convert(&code),
                // Propagate errors further
                Err(err) => Err(err),
            })
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;

    use super::*;

    #[test]
    fn string_rule() -> Result<()> {
        assert_eq!(
            StringRule::new("replace", "world")?.convert("Hello $$replace$$!")?,
            "Hello world!"
        );
        assert_eq!(
            StringRule::new("replace", "world")?.convert("Hello world!")?,
            "Hello world!"
        );
        assert_eq!(
            StringRule::new("replace", "world")?.convert("Hello $$replace$$, bye $$replace$$!")?,
            "Hello world, bye world!"
        );

        Ok(())
    }

    #[test]
    fn type_rule() -> Result<()> {
        assert_eq!(
            TypeRule::new("replace", "i32")?
                .convert("let some_type = <replacer::rust_type!(replace; String;)>::new();")?,
            "let some_type = <i32>::new();"
        );
        assert_eq!(
            TypeRule::new("replace", "i32")?.convert("Hello world!")?,
            "Hello world!"
        );
        assert_eq!(
            TypeRule::new("replace", "i32")?
                .convert("let some_type = Map<replacer::rust_type!(replace; String;), replacer::rust_type!(replace; String;)>::new();")?,
            "let some_type = Map<i32, i32>::new();"
        );

        Ok(())
    }

    #[test]
    fn struct_rule() -> Result<()> {
        assert_eq!(
            StructRule::new("replace", "Point2D")?
                .convert("replacer::rust_struct! {replace; Point; x: i32, y: i32;}")?,
            "struct Point2D { x: i32, y: i32 }"
        );
        assert_eq!(
            StructRule::new("replace", "i32")?.convert("Hello world!")?,
            "Hello world!"
        );

        Ok(())
    }
}