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
489
490
491
492
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#![cfg_attr(feature = "fatal-warnings", deny(warnings))]
#![deny(clippy::correctness)]
#![warn(clippy::pedantic)]
#![allow(clippy::match_bool)]
#![allow(clippy::if_not_else)]
#![allow(clippy::stutter)]
#![allow(clippy::similar_names)]
#![allow(clippy::use_self)]
#![allow(clippy::single_match_else)]
#![allow(clippy::inline_always)]
#![allow(clippy::partialeq_ne_impl)]

use crate::markdown::{Markdown, MarkdownError};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use thiserror::Error;
use toml::Value;

mod inject_doc;
mod markdown;

pub use inject_doc::{inject_doc, InjectDocError};

#[derive(Error, Debug)]
pub enum ManifestError {
    #[error("failed to read manifest \"{0}\"")]
    ErrorReadingManifest(PathBuf),
    #[error("failed to parse toml")]
    ErrorParsingToml,
}

#[derive(PartialEq, Eq, Debug)]
pub struct Manifest {
    lib_path: Option<PathBuf>,
    readme_path: Option<PathBuf>,
    bin_path: HashMap<String, PathBuf>,
}

impl Manifest {
    pub fn from_file(file_path: impl AsRef<Path>) -> Result<Manifest, ManifestError> {
        let str: String = std::fs::read_to_string(&file_path)
            .map_err(|_| ManifestError::ErrorReadingManifest(file_path.as_ref().to_path_buf()))?;
        Manifest::from_str(&str)
    }

    pub fn from_str(str: &str) -> Result<Manifest, ManifestError> {
        let toml: toml::Value = toml::from_str(str).map_err(|_| ManifestError::ErrorParsingToml)?;

        let get_str = |value: &Value, field: &str| -> Option<String> {
            value.get(field).and_then(|p| p.as_str()).map(ToOwned::to_owned)
        };
        let get_str_table = |table: &str, field: &str| -> Option<&str> {
            toml.get(table).and_then(|v| v.get(field)).and_then(|p| p.as_str())
        };

        let mut bin_path = HashMap::new();

        if let Some(bin_table) = toml.get("bin").and_then(|v| v.as_array()) {
            for bin in bin_table {
                match (get_str(bin, "name"), get_str(bin, "path")) {
                    (Some(name), Some(path)) => {
                        bin_path.insert(name, Path::new(&path).to_path_buf());
                    }
                    _ => (),
                }
            }
        }

        toml.get("bin").and_then(|v| v.as_array()).map(|t| t.iter());

        Ok(Manifest {
            lib_path: get_str_table("lib", "path").map(|v| Path::new(v).to_path_buf()),
            readme_path: get_str_table("package", "readme").map(|v| Path::new(v).to_path_buf()),
            bin_path,
        })
    }
}

#[derive(Error, Debug)]
pub enum ProjectError {
    #[error("project root not found")]
    ProjectRootNotFound,
    #[error("manifest error: {0}")]
    ManifestError(ManifestError),
}

impl From<ManifestError> for ProjectError {
    fn from(e: ManifestError) -> ProjectError {
        ProjectError::ManifestError(e)
    }
}

#[derive(PartialEq, Eq, Debug)]
pub struct Project {
    manifest: Manifest,
    directory: PathBuf,
}

pub fn find_first_file_in_ancestors(dir_path: impl AsRef<Path>, filename: &str) -> Option<PathBuf> {
    for ancestor_dir in dir_path.as_ref().ancestors() {
        let file = ancestor_dir.join(filename);
        if file.is_file() {
            return Some(file);
        }
    }

    None
}

impl Project {
    /// Creates a [`Project`] from a path.  It will ancestor paths until it finds the root of the
    /// project.
    pub fn from_dir(dir_path: impl AsRef<Path>) -> Result<Project, ProjectError> {
        match find_first_file_in_ancestors(dir_path, "Cargo.toml") {
            None => Err(ProjectError::ProjectRootNotFound),
            Some(manifest_file) => Ok(Project {
                manifest: Manifest::from_file(&manifest_file)?,
                directory: manifest_file.parent().expect("this should never happen").to_path_buf(),
            }),
        }
    }

    pub fn get_lib_entryfile_path(&self) -> Option<PathBuf> {
        let default = || Path::new("src").join("lib.rs").to_path_buf();
        let rel_path = self.manifest.lib_path.clone().unwrap_or_else(default);
        let path = self.directory.join(rel_path).to_path_buf();

        match path.is_file() {
            true => Some(path),
            false => None,
        }
    }

    pub fn get_bin_default_entryfile_path(&self) -> Option<PathBuf> {
        let default = || Path::new("src").join("main.rs").to_path_buf();
        let rel_path = self.manifest.lib_path.clone().unwrap_or_else(default);
        let path = self.directory.join(rel_path).to_path_buf();

        match path.is_file() {
            true => Some(path),
            false => None,
        }
    }

    pub fn get_bin_entryfile_path(&self, name: &str) -> Option<PathBuf> {
        self.manifest.bin_path.get(name).and_then(|rel_path| {
            let path = self.directory.join(rel_path).to_path_buf();

            match path.is_file() {
                true => Some(path),
                false => None,
            }
        })
    }

    pub fn get_readme_path(&self) -> Option<PathBuf> {
        let default = || Path::new("README.md").to_path_buf();
        let rel_path = self.manifest.readme_path.clone().unwrap_or_else(default);
        let path = self.directory.join(rel_path).to_path_buf();

        match path.is_file() {
            true => Some(path),
            false => None,
        }
    }
}

#[derive(Error, Debug)]
pub enum DocError {
    #[error("cannot open source file \"{0}\"")]
    ErrorReadingSourceFile(PathBuf),
    #[error("cannot parse source file: {0}")]
    ErrorParsingSourceFile(syn::Error),
}

pub struct Doc {
    markdown: Markdown,
}

impl Doc {
    pub fn from_source_file(file_path: impl AsRef<Path>) -> Result<Option<Doc>, DocError> {
        let source: String = std::fs::read_to_string(file_path.as_ref())
            .map_err(|_| DocError::ErrorReadingSourceFile(file_path.as_ref().to_path_buf()))?;

        Doc::from_source_str(&source)
    }

    pub fn from_str(str: impl Into<String>) -> Doc {
        Doc { markdown: Markdown::from_str(str) }
    }

    fn is_toplevel_doc(attr: &syn::Attribute) -> bool {
        use syn::token::Bang;
        use syn::AttrStyle;

        attr.style == AttrStyle::Inner(Bang::default()) && attr.path.is_ident("doc")
    }

    pub fn from_source_str(source: &str) -> Result<Option<Doc>, DocError> {
        use syn::{parse_str, Lit, Meta, MetaNameValue};

        let ast: syn::File = parse_str(source).map_err(|e| DocError::ErrorParsingSourceFile(e))?;
        let mut lines: Vec<String> = Vec::with_capacity(1024);

        for attr in ast.attrs.iter() {
            if Doc::is_toplevel_doc(attr) {
                if let Ok(Meta::NameValue(MetaNameValue { lit: Lit::Str(lstr), .. })) =
                    attr.parse_meta()
                {
                    let string = &lstr.value();

                    match string.lines().count() {
                        0 => lines.push("".to_owned()),
                        1 => {
                            let line = string.strip_prefix(' ').unwrap_or(string);
                            lines.push(line.to_owned());
                        }

                        // Multiline comment.
                        _ => {
                            fn empty_line(str: &str) -> bool {
                                str.chars().all(|c| c.is_whitespace())
                            }

                            let x = string
                                .lines()
                                .enumerate()
                                .filter(|(i, l)| !(*i == 0 && empty_line(l)))
                                .map(|(_, l)| l);

                            lines.extend(x.map(|s| s.to_owned()));
                        }
                    }
                }
            }
        }

        match lines.is_empty() {
            true => Ok(None),
            false => Ok(Some(Doc { markdown: Markdown::from_lines(&lines) })),
        }
    }

    pub fn lines(&self) -> impl Iterator<Item = &str> {
        self.markdown.lines()
    }
}

#[derive(Error, Debug)]
pub enum ReadmeError {
    #[error("failed to read README file \"{0}\"")]
    ErrorReadingReadmeFromFile(PathBuf),
    #[error("failed to write README file \"{0}\"")]
    ErrorWritingMarkdownToFile(PathBuf),
    #[error("failed to write README")]
    ErrorWritingMarkdown,
}

impl From<MarkdownError> for ReadmeError {
    fn from(e: MarkdownError) -> ReadmeError {
        match e {
            MarkdownError::ErrorReadingMarkdownFromFile(p) => {
                ReadmeError::ErrorReadingReadmeFromFile(p)
            }
            MarkdownError::ErrorWritingMarkdownToFile(p) => {
                ReadmeError::ErrorWritingMarkdownToFile(p)
            }
            MarkdownError::ErrorWritingMarkdown => ReadmeError::ErrorWritingMarkdown,
        }
    }
}

#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum LineTerminator {
    Lf,
    CrLf,
}

pub struct Readme {
    markdown: Markdown,
}

impl Readme {
    pub fn from_file(file_path: impl AsRef<Path>) -> Result<Readme, ReadmeError> {
        Ok(Readme { markdown: Markdown::from_file(file_path)? })
    }

    pub fn from_str(str: impl Into<String>) -> Readme {
        Readme { markdown: Markdown::from_str(str) }
    }

    pub fn from_lines(lines: &[impl AsRef<str>]) -> Readme {
        Readme { markdown: Markdown::from_lines(lines) }
    }

    pub fn lines(&self) -> impl Iterator<Item = &str> {
        self.markdown.lines()
    }

    pub fn write_to_file(
        &self,
        file: impl AsRef<Path>,
        line_terminator: LineTerminator,
    ) -> Result<(), ReadmeError> {
        Ok(self.markdown.write_to_file(file, line_terminator)?)
    }

    pub fn write(
        &self,
        writer: impl std::io::Write,
        line_terminator: LineTerminator,
    ) -> Result<(), ReadmeError> {
        Ok(self.markdown.write(writer, line_terminator)?)
    }
}

pub fn infer_line_terminator(file_path: impl AsRef<Path>) -> std::io::Result<LineTerminator> {
    let content: String = std::fs::read_to_string(file_path.as_ref())?;

    let crlf_lines: usize = content.matches("\r\n").count();
    let lf_lines: usize = content.matches("\n").count() - crlf_lines;

    if crlf_lines > lf_lines {
        Ok(LineTerminator::CrLf)
    } else {
        Ok(LineTerminator::Lf)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use indoc::indoc;
    use pretty_assertions::assert_eq;
    use std::iter::FromIterator;

    #[test]
    fn test_manifest_from_str() {
        let str = indoc! { r#"
            [package]
            readme = "README.md"

            [lib]
            path = "src/lib.rs"
            "#
        };

        let expected_manifest = Manifest {
            lib_path: Some(Path::new("src").join("lib.rs").to_path_buf()),
            readme_path: Some(Path::new("README.md").to_path_buf()),
            bin_path: HashMap::new(),
        };

        assert_eq!(Manifest::from_str(str).unwrap(), expected_manifest);
    }

    #[test]
    fn test_manifest_from_str_multiple_bin() {
        let str = indoc! { r#"
            [package]

            [[bin]]
            name = "foo"
            path = "src/m.rs"

            [[bin]]
            name = "bar"
            path = "src/bar.rs"
            "#
        };

        let expected_manifest = Manifest {
            lib_path: None,
            readme_path: None,
            bin_path: HashMap::from_iter(
                [
                    ("foo".to_owned(), Path::new("src").join("m.rs")),
                    ("bar".to_owned(), Path::new("src").join("bar.rs")),
                ]
                .iter()
                .cloned(),
            ),
        };

        assert_eq!(Manifest::from_str(str).unwrap(), expected_manifest);
    }

    #[test]
    fn test_doc_from_source_str_no_doc() {
        let str = indoc! { r#"
            use std::fs;

            struct Nothing {}
            "#
        };

        assert!(Doc::from_source_str(str).unwrap().is_none());
    }

    #[test]
    fn test_doc_from_source_str_single_line_comment() {
        let str = indoc! { r#"
            #![cfg_attr(not(feature = "std"), no_std)]
            // normal comment

            //! This is the doc for the crate.
            //!This line doesn't start with space.
            //!
            //! And a nice empty line above us.
            //! Also a line ending in "

            struct Nothing {}
            "#
        };

        let doc = Doc::from_source_str(str).unwrap().unwrap();
        let lines: Vec<&str> = doc.lines().collect();

        let expected = vec![
            "This is the doc for the crate.",
            "This line doesn't start with space.",
            "",
            "And a nice empty line above us.",
            "Also a line ending in \"",
        ];

        assert_eq!(lines, expected);
    }

    #[test]
    fn test_doc_from_source_str_multi_line_comment() {
        let str = indoc! { r#"
            #![cfg_attr(not(feature = "std"), no_std)]
            /* normal comment */

            /*!
            This is the doc for the crate.
             This line start with space.

            And a nice empty line above us.
            */

            struct Nothing {}
            "#
        };

        let doc = Doc::from_source_str(&str).unwrap().unwrap();
        let lines: Vec<&str> = doc.lines().collect();

        let expected = vec![
            "This is the doc for the crate.",
            " This line start with space.",
            "",
            "And a nice empty line above us.",
        ];

        assert_eq!(lines, expected);
    }

    #[test]
    fn test_doc_from_source_str_single_line_keep_indentation() {
        let str = indoc! { r#"
            #![cfg_attr(not(feature = "std"), no_std)]
            // normal comment

            //! This is the doc for the crate.  This crate does:
            //!
            //!   1. nothing.
            //!   2. niente.

            struct Nothing {}
            "#
        };

        let doc = Doc::from_source_str(str).unwrap().unwrap();
        let lines: Vec<&str> = doc.lines().collect();

        let expected = vec![
            "This is the doc for the crate.  This crate does:",
            "",
            "  1. nothing.",
            "  2. niente.",
        ];

        assert_eq!(lines, expected);
    }
}