count-md 0.2.0

A simple, configurable command-line tool and Rust library for Unicode-aware, Markdown-aware, HTML-aware word counting in Markdown documents
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
use std::{
    io::{self, BufReader, Read, Write},
    path::{Path, PathBuf},
};

use clap::{ArgAction, Parser};
use rayon::prelude::*;

use count_md::{count_with_options, Options};

fn main() -> Result<(), Error> {
    let args = Args::parse();

    let (inputs, output) = args.paths.resolve()?;

    let contents = match inputs {
        Input::Stdin(mut stdin) => {
            let mut buf = String::new();
            stdin
                .read_to_string(&mut buf)
                .map_err(|source| Error::Read {
                    src: String::from("<stdin>"),
                    source,
                })?;
            vec![(String::from("<stdin>"), buf)]
        }
        Input::Files(items) => items
            .into_iter()
            .map(|(path, mut input)| -> Result<(String, String), Error> {
                let mut buf = String::new();
                input
                    .read_to_string(&mut buf)
                    .map_err(|source| Error::Read {
                        src: String::from("<stdin>"),
                        source,
                    })?;
                Ok((path.display().to_string(), buf))
            })
            .collect::<Result<Vec<_>, Error>>()?,
    };

    let resolved_options = options_from(&args);

    // This can be multithreaded, using Rayon to parallelize the counting. That
    // should make it *much* faster, since right now it is single-threaded.
    let (total, pairs) = contents
        .par_iter()
        .fold(
            || (0, vec![]),
            |(sum, mut pairs), (path, content)| {
                let count = count_with_options(content, resolved_options);
                let new_sum = sum + count;
                pairs.push((path, count));
                (new_sum, pairs)
            },
        )
        .reduce(
            || (0, vec![]),
            |(total, mut pairs), (subtotal, subpairs)| {
                // This copy should be quite cheap: it copies a reference and a
                // `u64` from `subpairs` into `pairs`. It will be O(N) on the
                // size of the `subpairs`.
                //
                // With enough elements, that could be noticeable. That is the
                // tradeoff for parallelizing this! However, in most cases, the
                // number of files in question will be relatively small; even
                // with *thousands* of files, this should be very fast.
                pairs.extend(&subpairs);
                (total + subtotal, pairs)
            },
        );

    report(pairs, total, output)
}

// This could in principle be async, but it would not much matter from what I
// can see: it needs to report and flush *all* of the data. (Test it, of course,
// just to be sure!)
fn report(
    pairs: Vec<(&impl std::fmt::Display, u64)>,
    total: u64,
    output: Output,
) -> Result<(), Error> {
    let (dest, mut buf) = match output {
        Output::File { path, buf } => (path.display().to_string(), buf),
        Output::Stdout(stdout) => (String::from("<stdout>"), stdout),
    };

    for (path, count) in pairs {
        writeln!(buf, "{path} has {count} words").map_err(|source| Error::Write {
            dest: dest.clone(),
            source,
        })?;
    }

    writeln!(buf, "Total: {total}").map_err(|source| Error::Write {
        dest: dest.clone(),
        source,
    })?;

    buf.flush()
        .map_err(|source| Error::Flush { dest, source })?;

    Ok(())
}

// Note: this might be able to be eliminated entirely, since there is only the
// one variant and I am otherwise just dumping strings.
#[derive(Debug, thiserror::Error)]
enum Error {
    #[error("could not open file at '{path}' {reason}")]
    CouldNotOpenFile {
        path: PathBuf,
        reason: FileOpenReason,
        source: std::io::Error,
    },

    #[error("`--force` is only allowed with `--output`")]
    InvalidArgs,

    #[error("invalid file path with no parent directory: '{path}'")]
    InvalidDirectory { path: PathBuf },

    #[error("could not create directory '{dir}' to write file '{path}")]
    CreateDirectory {
        dir: PathBuf,
        path: PathBuf,
        source: std::io::Error,
    },

    #[error(transparent)]
    CheckFileExists { source: std::io::Error },

    #[error("the file '{0}' already exists")]
    FileExists(PathBuf),

    #[error("could not write to '{dest}': {source}")]
    Write {
        dest: String,
        source: std::io::Error,
    },

    #[error("could not flush to '{dest}': {source}")]
    Flush {
        dest: String,
        source: std::io::Error,
    },

    #[error("could not read from '{src}': {source}")]
    Read { src: String, source: io::Error },
}

#[derive(Debug)]
enum FileOpenReason {
    Read,
    Write,
}

impl std::fmt::Display for FileOpenReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FileOpenReason::Read => write!(f, "to read it"),
            FileOpenReason::Write => write!(f, "to write to it"),
        }
    }
}

fn options_from(args: &Args) -> Options {
    if args.all {
        return Options::all();
    }

    let mut options = Options::empty();

    if args.metadata {
        options |= Options::IncludeMetadata;
    }

    if args.blockquotes {
        options |= Options::IncludeBlockquotes;
    }

    if args.headings {
        options |= Options::IncludeHeadings;
    }

    if args.footnotes {
        options |= Options::IncludeFootnotes;
    }

    if args.tables {
        options |= Options::IncludeTables;
    }

    if args.inline_code {
        options |= Options::IncludeInlineCode;
    }

    if args.block_code {
        options |= Options::IncludeBlockCode;
    }

    if args.block_html {
        options |= Options::IncludeBlockHtml;
    }

    options
}

#[derive(Parser)]
struct Args {
    #[clap(flatten)]
    paths: Paths,

    /// Include every possible option.
    #[clap(
        long,
        conflicts_with_all = [
            "metadata",
            "blockquotes",
            "headings",
            "footnotes",
            "tables",
            "inline_code",
            "block_code",
            "block_html"
        ]
    )]
    all: bool,

    /// Include YAML or TOML metadata.
    #[clap(
        long,
        default_value = "false",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    metadata: bool,

    /// Include blockquotes.
    #[clap(
        long,
        default_value = "false",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    blockquotes: bool,

    /// Include headings.
    #[clap(
        long,
        default_value = "true",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    headings: bool,

    /// Include footnotes.
    #[clap(
        long,
        default_value = "true",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    footnotes: bool,

    /// Include tables.
    #[clap(
        long,
        default_value = "true",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    tables: bool,

    /// Include inline code.
    #[clap(
        long,
        default_value = "true",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    inline_code: bool,

    /// Include block code.
    #[clap(
        long,
        default_value = "false",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    block_code: bool,

    /// Include block HTML.
    #[clap(
        long,
        default_value = "true",
        default_missing_value = "true",
        num_args(0..=1),
        require_equals(true),
        action = ArgAction::Set
    )]
    block_html: bool,
}

#[derive(clap::Args, Debug, PartialEq, Clone)]
struct Paths {
    /// Files to count text in. Will use `stdin` if none are supplied.
    files: Vec<PathBuf>,

    /// Where to print the output. Will use `stdout` if not supplied.
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// If the supplied `output` file is present, overwrite it.
    #[arg(long, default_missing_value("true"), num_args(0..=1), require_equals(true))]
    force: Option<bool>,
}

impl Paths {
    fn resolve(&self) -> Result<(Input, Output), Error> {
        let dest_cfg = match (&self.output, self.force.unwrap_or(false)) {
            (Some(buf), force) => DestCfg::Path { buf, force },
            (None, false) => DestCfg::Stdout,
            (None, true) => return Err(Error::InvalidArgs)?,
        };
        let inputs = if self.files.is_empty() {
            Input::Stdin(Box::new(BufReader::new(io::stdin())) as Box<dyn Read>)
        } else {
            to_input_buffers(&self.files)?
        };
        let output = output_buffer(&dest_cfg)?;
        Ok((inputs, output))
    }
}

enum Output {
    File { path: PathBuf, buf: Box<dyn Write> },
    Stdout(Box<dyn Write>),
}

impl std::fmt::Debug for Output {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Output::File { path, .. } => write!(f, "{path:?}"),
            Output::Stdout(..) => f.write_str("stdin"),
        }
    }
}

impl std::fmt::Display for Output {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Output::File { path, .. } => write!(f, "{}", path.display()),
            Output::Stdout(..) => f.write_str("stdin"),
        }
    }
}

pub(crate) enum DestCfg<'p> {
    Path { buf: &'p Path, force: bool },
    Stdout,
}

enum Input {
    Files(Vec<(PathBuf, Box<dyn Read>)>),
    Stdin(Box<dyn Read>),
}

fn to_input_buffers(paths: &[PathBuf]) -> Result<Input, Error> {
    paths
        .iter()
        .map(|path| {
            std::fs::File::open(path)
                .map_err(|source| Error::CouldNotOpenFile {
                    path: path.to_owned(),
                    reason: FileOpenReason::Read,
                    source,
                })
                .map(|file| {
                    (
                        path.to_owned(),
                        Box::new(BufReader::new(file)) as Box<dyn Read>,
                    )
                })
        })
        .collect::<Result<Vec<_>, Error>>()
        .map(|inputs| Input::Files(inputs))
}

fn output_buffer(dest_cfg: &DestCfg) -> Result<Output, Error> {
    match *dest_cfg {
        DestCfg::Stdout => Ok(Output::Stdout(Box::new(std::io::stdout()))),

        DestCfg::Path { buf: path, force } => {
            let dir = path.parent().ok_or_else(|| Error::InvalidDirectory {
                path: path.to_owned(),
            })?;

            std::fs::create_dir_all(dir).map_err(|source| Error::CreateDirectory {
                dir: dir.to_owned(),
                path: path.to_owned(),
                source,
            })?;

            // TODO: can I, without doing a TOCTOU, avoid overwriting an existing
            // file? (That's mostly academic, but since the point of this is to
            // learn, I want to learn that.)
            let file_exists = path
                .try_exists()
                .map_err(|source| Error::CheckFileExists { source })?;

            if file_exists && !force {
                return Err(Error::FileExists(path.to_owned()));
            }

            let file = std::fs::File::create(path).map_err(|source| Error::CouldNotOpenFile {
                path: path.to_owned(),
                reason: FileOpenReason::Write,
                source,
            })?;

            Ok(Output::File {
                path: path.to_owned(),
                buf: Box::new(file),
            })
        }
    }
}