STKLR 0.0.42

STKLR: pronounced 'stickler'. Is a cli tool to automatically link functions, enums, structs, traits etc in rust-doc docstrings. I couldn't find a tool like this when I needed it so... here we are.
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use super::consts::*;

use anyhow::Result;
use core::fmt::Display;
use glob::glob;
use log::debug;
use std::{
    collections::HashMap,
    fs,
    hash::Hash,
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
};

/// Are all the available changes to a line done? not done? etc.
#[derive(Default, Debug, Clone, Hash)]
pub enum Linked {
    Complete,
    Loaded,
    Partial,
    Irrelevant,
    #[default]
    Unprocessed,
}
/// A way to describe lines of code based on what they are/do etc.
#[derive(PartialEq, Default, Debug, Clone, Hash)]
#[allow(non_camel_case_types)]
pub enum Flavour {
    RUST_DOCS,
    RUST_FN,
    RUST_TY,
    RUST_ENUM,
    RUST_STRUCT,
    RUST_IMPORT,
    RUST_USE,
    RUST_TRAIT,
    #[default]
    Tasteless,
}
/// A line from a source file exactly as is.
#[derive(Default, Debug, Clone, Hash)]
pub struct RawLine {
    pub line_num: usize, //NOTE: indentionally duplicate data
    pub all_linked: Linked,
    pub contents: String,
    pub flavour: Flavour,
    pub idents: Vec<String>,
    pub source_file: PathBuf,
}

/// A line from a source file with its contents modified by this app.
#[derive(PartialEq, Eq, PartialOrd, Debug, Clone, Hash)]
pub struct AdjustedLine {
    pub line_num: usize,
    pub contents: String,
    pub source_file: PathBuf,
}

impl From<RawLine> for AdjustedLine {
    fn from(line: RawLine) -> Self {
        Self {
            line_num: line.line_num,
            contents: line.contents,
            source_file: line.source_file,
        }
    }
}

/// All the source code combined!
#[derive(Default, Debug, Clone)]
pub struct SourceTree {
    pub source_files: Vec<RawSourceCode>,
    pub named_idents: Vec<String>,
}

impl SourceTree {
    /// Populates the idents we care about...
    fn populate_idents(mut self) -> Self {
        self.source_files.iter().for_each(|sf| {
            sf.named_idents
                .iter()
                .for_each(|e| self.named_idents.push(e.to_string()));
        });

        self
    }

    pub fn setup_tree(paths: &Option<Vec<String>>) -> SourceTree {
        if let Some(paths) = paths {
            SourceTree::new_from_paths(paths)
        } else {
            SourceTree::new_from_cwd()
        }
    }
    /// Creates a new [`SourceTree`] from a slice/vec of paths.
    pub fn new_from_paths(paths: &[String]) -> Self {
        SourceTree {
            source_files: paths.iter().map(RawSourceCode::new_from_file).collect(),
            named_idents: Vec::new(),
        }
        .populate_idents()
    }
    /// Creates a new [`SourceTree`] `Result` the glob search the current working directory the app is run
    /// in.
    pub fn new_from_cwd() -> Self {
        let path = std::env::current_dir().expect("Unable to ascertain current working directory, this is likely a permissions error with your OS.");

        Self::new_from_dir(format!("{}", path.as_path().display()))
    }
    /// Creates a new [`SourceTree`] `Result` a given directory.
    pub fn new_from_dir<P>(dir: P) -> Self
    where
        P: Display + AsRef<Path>,
    {
        let search_path = format!("{}/**/*.rs", dir);
        SourceTree {
            source_files: {
                glob(&search_path)
                    .unwrap()
                    .filter_map(Result::ok)
                    .filter(|f| !f.display().to_string().contains("target/"))
                    .map(|p| RawSourceCode::new_from_file(&p))
                    .collect::<Vec<RawSourceCode>>()
            },
            named_idents: Vec::new(),
        }
        .populate_idents()
    }
    /// Commits changes to disk, essentially writing the [`AdjustedLine`] back to a `Result` of
    /// the same name, line-by-line.
    pub fn write_changes(file: PathBuf, changes: &mut [AdjustedLine], write_flag: bool) {
        debug!("SourceTree::write_changes was called");
        changes.sort_by(|a, b| a.line_num.cmp(&b.line_num));
        let output: Vec<String> = changes
            .iter_mut()
            .map(|adj_line| adj_line.contents.to_owned())
            .collect();

        if write_flag {
            fs::write(&file, output.join("\n")).unwrap(); //FIXME:
            debug!("Write successful.")
        } else {
            changes.iter().for_each(|e| println!("{}", e));
        }
    }
}

#[derive(Default, Debug, Clone)]
pub struct RawSourceCode {
    pub m: HashMap<usize, RawLine>,
    pub file: PathBuf,
    pub ident_locs: Vec<usize>,
    pub doc_locs: Vec<usize>,
    pub total_lines: usize,
    pub named_idents: Vec<String>,
}

impl RawSourceCode {
    pub fn new_from_file<P>(file: P) -> Self
    where
        PathBuf: From<P>,
        P: AsRef<Path> + Copy,
    {
        let mut raw_source_file = RawSourceCode {
            m: HashMap::new(),
            file: PathBuf::from(file),
            ident_locs: Vec::new(),
            doc_locs: Vec::new(),
            total_lines: 0,
            named_idents: Vec::new(),
        };

        if let Ok(lines) = crate::read_lines(file) {
            lines
                .collect::<Vec<_>>()
                .iter()
                .enumerate()
                .for_each(|(e, l)| {
                    if let Ok(l) = l {
                        let mut raw_line = RawLine {
                            all_linked: Linked::Unprocessed,
                            contents: l.into(),
                            line_num: e,
                            source_file: file.into(),
                            ..Default::default()
                        };
                        raw_line.find_docs();
                        raw_line.find_idents();
                        raw_source_file
                            .named_idents
                            .extend(raw_line.idents.iter().cloned());
                        raw_source_file.m.insert(e, raw_line);
                    }
                });
        }
        raw_source_file.total_lines = raw_source_file.m.len();
        raw_source_file.named_idents.dedup();
        raw_source_file.named_idents.retain(|x| !x.is_empty());
        raw_source_file
    }

    /// Checks whether `self` [`should_be_modified`] and if so, [`process`] `Result` the passed
    /// `idents` is called.
    pub fn make_adjustments(&self, idents: &[String]) -> Vec<AdjustedLine> {
        self.m
            .iter()
            .filter(|(_, raw_line)| raw_line.should_be_modified(idents))
            .map(|(_, f)| -> RawLine { f.to_owned().process_changes(idents) })
            .map(|rpl| -> AdjustedLine { rpl.into() })
            .collect::<Vec<AdjustedLine>>()
    }
}

#[derive(Debug, Default)]
pub struct ReportCard {
    pub source_files: Vec<RawSourceCode>,
    pub named_idents: Vec<String>,
    pub num_funcs: usize,
    pub num_pub_funcs: usize,

    pub num_structs: usize,
    pub num_pub_structs: usize,

    pub num_enums: usize,
    pub num_pub_enums: usize,

    pub num_types: usize,
    pub num_pub_types: usize,

    pub num_traits: usize,
    pub num_pub_traits: usize,

    pub num_macros: usize,
}

impl ReportCard {
    pub fn from_source_tree(st: SourceTree) -> Self {
        let mut rc = ReportCard::default();
        st.source_files
            .iter()
            .map(|rsc| rc.process(rsc))
            .collect::<()>();

        rc.source_files = st.source_files;
        rc
    }

    pub fn process(&mut self, rsc: &RawSourceCode) {
        rsc.iter()
            .flat_map(|(_k, v)| v.report(self))
            .collect::<()>();
    }

    //TODO: DRY this up...
    pub fn pretty_print(&self) {
        println!("REPORT:");
        println!(" fns    : {}", self.num_funcs + self.num_pub_funcs);
        println!(" structs: {}", self.num_structs + self.num_pub_structs);
        println!(" enums  : {}", self.num_enums + self.num_pub_enums);
        println!(" types  : {}", self.num_types + self.num_pub_types);
        println!(" traits : {}", self.num_traits + self.num_pub_traits);

        //TODO: % of things that're public.
        //println!("% public:\n");
    }
}

impl RawLine {
    /// Will return true for a SINGLE instance of when a modification should be made, how many may
    /// really be in there is the domain of [`process`]
    fn should_be_modified(&self, idents: &[String]) -> bool {
        // NOTE: this isn't as bad as you'd initially think, you're out at the first branch if it's
        // not a docstring, or, out at the first 'hit'.
        if matches!(self.flavour, Flavour::RUST_DOCS) {
            for i in idents {
                if self.contents.contains(i)
                    || self.contents.contains(&format!("{}s", i))
                    || self.contents.contains(&format!("{}.", i))
                    || self.contents.contains(&format!("{}'s", i)) && self.contents.contains("///")
                    || self.contents.contains("//!")
                {
                    return true;
                }
            }
        }
        false
    }

    /// Used by the report functionality.
    fn pub_or_private(&self) -> bool {
        self.contents.contains("pub")
    }
    /// WIP!
    /// Produce a report on the source at hand..
    fn report(&self, rc: &mut ReportCard) -> Result<()> {
        match self.flavour {
            Flavour::RUST_FN => {
                if self.pub_or_private() {
                    rc.num_pub_funcs += 1
                } else {
                    rc.num_funcs += 1
                }
            }
            Flavour::RUST_TY => {
                if self.pub_or_private() {
                    rc.num_pub_types += 1
                } else {
                    rc.num_types += 1
                }
            }

            Flavour::RUST_ENUM => {
                if self.pub_or_private() {
                    rc.num_pub_enums += 1
                } else {
                    rc.num_enums += 1
                }
            }
            Flavour::RUST_TRAIT => {
                if self.pub_or_private() {
                    rc.num_pub_types += 1
                } else {
                    rc.num_traits += 1
                }
            }
            Flavour::RUST_STRUCT => {
                if self.pub_or_private() {
                    rc.num_pub_structs += 1
                } else {
                    rc.num_structs += 1
                }
            }
            _ => (),
        }

        Ok(())
    }
    /// Actually [`process`] the modifications to a [`RawLine`]'s contents.
    fn process_changes(mut self, idents: &[String]) -> Self {
        for id in idents {
            // TODO: how to not capture this ';' in the first place?
            self.contents = self.contents.replace(";", "");
            let split_n_proc = &self
                .contents
                .split_whitespace()
                .map(|sp| {
                    // Handle the always, i.e: i8 should always be `i8` etc...
                    if ALWAYS.contains(&sp) {
                        dbg!("ALWAYS");
                        debug!("{} found always in: {}", id, sp);
                        format!(" `{}`", id)
                    }
                    // Handle the captures.
                    else if sp.contains(id) && sp.len() > 3 {
                        debug!("{} found cap in: {}", id, sp);
                        format!(" [`{}`]", id)
                    }
                    // Unchanged...
                    else {
                        debug!("No change to: {} ", sp);
                        format!(" {}", sp)
                    }
                    //
                })
                .collect::<String>();
            self.contents = split_n_proc.to_string();
        }
        self
    }
    /// Finds the things we're interested in.
    fn find_idents(&mut self) {
        let text = self.contents.to_owned();

        // DRYness, is goodness.
        macro_rules! generate_ident_find_loops {
            ($($CONST:ident),*) => {
                $(for caps in $CONST.captures_iter(&text) {
                    if let Some(v) = caps.name("ident") {
                        let cap = v.as_str().to_string();
                        //TODO: You've got more flavours, use them...
                        self.flavour = Flavour::$CONST;
                        if cap != "" && !NEVERS.iter().any(|c| c == &cap) && cap.len() > 2{
                            self.idents.push(cap.clone());

                            // DEBUG CAPTURES LINE-BY-LINE
                            //eprintln!("{}", "-".repeat(40));
                            //eprintln!("{:?}", self.source_file);
                            //eprintln!("{}", cap);
                            //eprintln!("{}", self);
                            //eprintln!("{}", "-".repeat(40));
                            //std::thread::sleep(std::time::Duration::from_millis(200));
                            // For when we have gargage captures, this seems to be the best way to
                            // find them, where they come from etc..

                        }
                    }
                })*
            };
        }

        // generate a matcher for all of these.
        generate_ident_find_loops!(
            RUST_FN,
            RUST_TY,
            RUST_ENUM,
            RUST_STRUCT,
            RUST_IMPORT,
            RUST_USE,
            RUST_TRAIT
        );
    }

    /// Find and classify docstrings.
    // NOTE: also regex controlled, see ./src/search/consts.rs
    // TODO: add support for '//!' module docstrings.
    fn find_docs(&mut self) {
        let text = self.contents.to_owned();
        for caps in RUST_DOCSTRING.captures_iter(&text) {
            if let Some(_) = caps.name("ident") {
                self.flavour = Flavour::RUST_DOCS;
            }
        }
    }
}

// Boilerplates....
impl Display for AdjustedLine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", &self.line_num, &self.contents,)
    }
}
impl Display for RawLine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", &self.line_num, &self.contents)
    }
}

impl Deref for RawSourceCode {
    type Target = HashMap<usize, RawLine>;
    fn deref(&self) -> &Self::Target {
        &self.m
    }
}
impl DerefMut for RawSourceCode {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.m
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trial_on_source() {
        let t1 = std::time::Instant::now();

        let st = SourceTree::new_from_dir("/media/jer/ARCHIVE/scrapers/rustwari");

        for rsc in st.source_files.iter() {
            debug!("{}", rsc.file.display());
            let new_m = rsc
                .make_adjustments(&rsc.named_idents)
                .into_iter()
                .map(|adj| (adj.line_num, adj.contents))
                .collect::<HashMap<usize, String>>();

            let output = (0..rsc.total_lines)
                .into_iter()
                .map(|n| -> String {
                    if let Some(new) = new_m.get(&n) {
                        new.to_owned()
                    } else {
                        let new = rsc.get(&n).unwrap().contents.to_owned();
                        new
                    }
                })
                .collect::<Vec<String>>();

            _ = std::fs::write(&rsc.file, output.join("\n"));
        }

        debug!(
            "{} FILES IN: {}s",
            st.source_files.len(),
            t1.elapsed().as_secs_f64()
        );
    }
}