cargo-countlines 0.1.1

A tool to count SLOC
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
use std::{
    collections::{HashMap, hash_map::Entry},
    path::{Path, PathBuf},
};

use futures::StreamExt;
use rayon::iter::{ParallelBridge, ParallelIterator};
use split_async::split;
use std::fs::File as StdFile;
use std::io::BufRead as _;
use std::io::BufReader as StdBufReader;
use tokio::io::BufReader as TokioBufReader;
use tokio::{fs::File as TokioFile, io::AsyncBufReadExt, runtime::Runtime};

use globset::GlobSet;
use indicatif::{ProgressBar, ProgressStyle};
use log::{info, warn};
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

use crate::{
    AppError, Mode,
    languages::{Language, LanguageId, Languages},
};

#[derive(Error, Debug)]
pub enum CountError {
    #[error("walkdir error")]
    WalkDir(#[from] walkdir::Error),

    #[error("io error in file {path}")]
    Io { path: PathBuf, err: std::io::Error },
}

pub struct Config {
    pub abs_root: PathBuf,
    pub rel_root: PathBuf, // relative to cwd
    pub languages: Languages,
    pub exclude: GlobSet, // all glob patterns are absolute
    pub ignore_hidden: bool,
    pub quiet: bool,
    pub max_depth: Option<usize>,
    pub follow_links: bool,
    pub machine_readable: bool,
    pub mode: Mode,
}

#[derive(Clone)]
pub struct Counts {
    pub files: usize,
    pub code: usize,
    pub comment: usize,
    pub blank: usize,
    pub invalid: usize,
}

impl Counts {
    fn merge(&mut self, other: &Counts) {
        self.files += other.files;
        self.code += other.code;
        self.comment += other.comment;
        self.blank += other.blank;
        self.invalid += other.invalid;
    }
}

fn sync_count(path: &Path, lang: &Language) -> Result<Counts, std::io::Error> {
    let mut code = 0;
    let mut comment = 0;
    let mut blank = 0;
    let mut invalid = 0;

    let line_comments = lang
        .line_comments
        .as_ref()
        .map(|c| c.as_ref())
        .unwrap_or(&[]);
    let block_comments = lang
        .block_comments
        .as_ref()
        .map(|c| c.as_ref())
        .unwrap_or(&[]);

    let mut in_block_comment = None;
    for line in StdBufReader::new(StdFile::open(path)?).lines() {
        let line = match line {
            Ok(l) => l,
            Err(_err) => {
                invalid += 1;
                continue;
            }
        };
        let line = line.trim();

        if line.is_empty() {
            blank += 1;
            continue;
        }

        if let Some(end_token) = in_block_comment {
            comment += 1;
            if line.ends_with(end_token) {
                in_block_comment = None;
            }
            continue;
        }

        if line_comments.iter().any(|lc| line.starts_with(lc)) {
            comment += 1;
            continue;
        }

        if let Some((_, end_token)) = block_comments
            .iter()
            .find(|(start_token, _)| line.starts_with(start_token))
        {
            if !line.ends_with(end_token) {
                in_block_comment = Some(end_token);
            }
            comment += 1;
            continue;
        }

        code += 1;
    }

    Ok(Counts {
        files: 1,
        code,
        comment,
        blank,
        invalid,
    })
}

async fn async_count(path: &Path, lang: &Language) -> Result<Counts, std::io::Error> {
    let mut code = 0;
    let mut comment = 0;
    let mut blank = 0;
    let mut invalid = 0;

    let line_comments = lang
        .line_comments
        .as_ref()
        .map(|c| c.as_ref())
        .unwrap_or(&[]);
    let block_comments = lang
        .block_comments
        .as_ref()
        .map(|c| c.as_ref())
        .unwrap_or(&[]);

    let mut in_block_comment = None;
    let mut iter = TokioBufReader::new(TokioFile::open(path).await?).lines();
    loop {
        let line = match iter.next_line().await {
            Ok(l) => l,
            Err(_err) => {
                invalid += 1;
                continue;
            }
        };
        let Some(line) = line else { break };
        let line = line.trim();

        if line.is_empty() {
            blank += 1;
            continue;
        }

        if let Some(end_token) = in_block_comment {
            comment += 1;
            if line.ends_with(end_token) {
                in_block_comment = None;
            }
            continue;
        }

        if line_comments.iter().any(|lc| line.starts_with(lc)) {
            comment += 1;
            continue;
        }

        if let Some((_, end_token)) = block_comments
            .iter()
            .find(|(start_token, _)| line.starts_with(start_token))
        {
            if !line.ends_with(end_token) {
                in_block_comment = Some(end_token);
            }
            comment += 1;
            continue;
        }

        code += 1;
    }

    Ok(Counts {
        files: 1,
        code,
        comment,
        blank,
        invalid,
    })
}

enum EntryResult {
    Some { lang_id: LanguageId, counts: Counts },
    None, // file didn't match
    Err(CountError),
}

#[derive(Default)]
pub struct OutputCounts {
    pub counts: HashMap<LanguageId, Counts>,
    pub unmatched_files: usize,
    pub error_files: usize,
}

impl OutputCounts {
    fn append_counts(&mut self, lang_id: LanguageId, counts: &Counts) {
        match self.counts.entry(lang_id) {
            Entry::Occupied(mut occupied_entry) => {
                occupied_entry.get_mut().merge(counts);
            }
            Entry::Vacant(vacant_entry) => {
                vacant_entry.insert(counts.clone());
            }
        }
    }

    fn merge(&mut self, other: &Self) {
        for (lang_id, counts) in &other.counts {
            self.append_counts(*lang_id, counts);
        }
        self.unmatched_files += other.unmatched_files;
        self.error_files += other.error_files;
    }
}

// === Walk internals ===

fn make_walk_iter(config: &Config) -> impl Iterator<Item = Result<DirEntry, walkdir::Error>> {
    let mut iter = WalkDir::new(&config.abs_root);
    if let Some(max_depth) = config.max_depth {
        iter = iter.max_depth(max_depth);
    }
    if config.follow_links {
        iter = iter.follow_links(true);
    }
    let iter = iter.into_iter().filter_entry(|entry| {
        // `as_encoded_bytes` returns a "self-synchronizing superset of UTF-8"
        if config.ignore_hidden && entry.file_name().as_encoded_bytes().starts_with(&[b'.']) {
            return false;
        }
        !config.exclude.is_match(entry.path())
    });

    iter
}

#[split]
async fn walk_loop_body(
    entry: Result<DirEntry, walkdir::Error>,
    config: &Config,
    pbar: Option<&ProgressBar>,
) -> EntryResult {
    let entry = match entry {
        Ok(e) if e.file_type().is_file() => e,
        Ok(_) => return EntryResult::None, // dir or symlink
        Err(err) => return EntryResult::Err(err.into()),
    };

    info!("{:?}", entry.path());
    pbar.map(|pbar| {
        pbar.inc(1);

        // display path relative to cwd
        // default to absolute path if `stip_prefix` fails
        let display_path = entry
            .path()
            .strip_prefix(&config.abs_root)
            .map(|rel_path| config.rel_root.join(rel_path).to_string_lossy().to_string())
            .unwrap_or_else(|_| entry.path().to_string_lossy().to_string());

        pbar.set_message(display_path);
    });

    for (lang_id, lang) in (&config.languages).into_iter().enumerate() {
        for ext in &lang.extensions {
            // `as_encoded_bytes` returns a "self-synchronizing superset of UTF-8"
            // This means that if the last few bytes match the ASCII values for a file extension,
            // then we can safely assume that's what they are
            if entry
                .file_name()
                .as_encoded_bytes()
                .ends_with(ext.as_bytes())
            {
                let counts: Result<_, _> = choose!(count)(entry.path(), lang).await;
                return match counts {
                    Ok(counts) => EntryResult::Some { lang_id, counts },
                    Err(err) => {
                        warn!("error in file {:?}", entry.path());
                        EntryResult::Err(CountError::Io {
                            path: entry.into_path(),
                            err,
                        })
                    }
                };
            }
        }
    }

    EntryResult::None
}

fn sync_walk(config: &Config, pbar: Option<&ProgressBar>) -> Result<OutputCounts, CountError> {
    let iter = make_walk_iter(config);

    let output = iter
        .map(|entry| sync_walk_loop_body(entry, config, pbar))
        .fold(OutputCounts::default(), |mut output, entry_result| {
            match entry_result {
                EntryResult::Some { lang_id, counts } => output.append_counts(lang_id, &counts),
                EntryResult::None => output.unmatched_files += 1,
                EntryResult::Err(_err) => output.error_files += 1,
            }
            output
        });

    Ok(output)
}

async fn async_walk(
    config: &Config,
    pbar: Option<&ProgressBar>,
) -> Result<OutputCounts, CountError> {
    let iter = make_walk_iter(config);

    let output = futures::stream::iter(iter)
        .map(|entry| async_walk_loop_body(entry, config, pbar))
        .buffer_unordered(20)
        .fold(OutputCounts::default(), async |mut output, entry_result| {
            match entry_result {
                EntryResult::Some { lang_id, counts } => output.append_counts(lang_id, &counts),
                EntryResult::None => output.unmatched_files += 1,
                EntryResult::Err(_err) => output.error_files += 1,
            }
            output
        })
        .await;

    Ok(output)
}

fn parallel_walk(config: &Config, pbar: Option<&ProgressBar>) -> Result<OutputCounts, CountError> {
    let iter = make_walk_iter(config);

    let output = iter
        .par_bridge()
        .map(|entry| sync_walk_loop_body(entry, config, pbar))
        .fold(
            || OutputCounts::default(),
            |mut output, entry_result| {
                match entry_result {
                    EntryResult::Some { lang_id, counts } => output.append_counts(lang_id, &counts),
                    EntryResult::None => output.unmatched_files += 1,
                    EntryResult::Err(_err) => output.error_files += 1,
                }
                output
            },
        )
        .reduce(
            || OutputCounts::default(),
            |mut output1, output2| {
                output1.merge(&output2);
                output1
            },
        );

    Ok(output)
}

pub fn run_count(config: &Config) -> Result<OutputCounts, AppError> {
    let rt = Runtime::new()?;

    let pbar = (!config.quiet).then(|| {
        let pbar = ProgressBar::no_length();
        pbar.set_style(
            ProgressStyle::with_template("[{elapsed_precise}] {human_pos} {msg}").unwrap(),
        );
        pbar
    });

    let output = match config.mode {
        Mode::Sync => sync_walk(config, pbar.as_ref()),
        Mode::Async => {
            let async_output = async_walk(config, pbar.as_ref());
            rt.block_on(async_output)
        }
        Mode::Parallel => parallel_walk(config, pbar.as_ref()),
    };

    pbar.as_ref().map(|pbar| pbar.finish_and_clear());

    Ok(output?)
}