workspacer_register/existing_x_macro.rs
1// ---------------- [ File: workspacer-register/src/existing_x_macro.rs ]
2crate::ix!();
3
4#[derive(Builder,Getters,Debug)]
5#[builder(setter(into))]
6#[getset(get="pub")]
7pub struct ExistingXMacro {
8 text: String, // e.g. "x!{command_runner}"
9 range: TextRange, // so we can remove it from the file
10
11 /// Any immediately preceding line comments that should stay with this macro
12 /// (e.g. doc comments or inline `// comment` lines).
13 /// We collect them so that, when we move the macro to the top block, we can
14 /// keep those comments attached to the macro in the new location.
15 #[builder(default = "None")]
16 leading_comments: Option<String>,
17}
18
19
20// A small struct to represent any macro we plan to unify at the top block,
21// whether it came from the old file or from the new_top_block text.
22#[derive(Clone,Builder,Getters,Debug)]
23#[builder(setter(into),build_fn(name = "private_build"))]
24#[getset(get="pub")]
25pub struct TopBlockMacro {
26 stem: String,
27
28 #[builder(default = "None")]
29 leading_comments: Option<String>, // e.g. `// top block\n`
30}
31
32impl TopBlockMacroBuilder {
33
34 pub fn build(&self) -> Result<TopBlockMacro, &'static str> {
35 // We must have a valid stem
36 let stem_str = self.stem.clone().ok_or("stem is required")?;
37
38 // Convert any leading_comments to a trimmed string
39 // then if it’s empty => store None
40 let leading_comments = match self.leading_comments.clone()
41 {
42 None => None,
43 Some(None) => None,
44 Some(Some(c)) => {
45 let trimmed = c.trim().to_string();
46 match trimmed.is_empty() {
47 true => None,
48 false => Some(trimmed),
49 }
50 },
51 };
52
53 Ok(TopBlockMacro {
54 stem: stem_str,
55 leading_comments,
56 })
57 }
58}