cargo_readme/config/
project.rs

1use std::env;
2use std::path::{Path, PathBuf};
3
4use crate::config::manifest::{Manifest, ManifestLib};
5
6/// Get the project root from given path or defaults to current directory
7///
8/// The given path is appended to the current directory if is a relative path, otherwise it is used
9/// as is. If no path is given, the current directory is used.
10/// A `Cargo.toml` file must be present is the root directory.
11pub fn get_root(given_root: Option<&str>) -> Result<PathBuf, String> {
12    let current_dir = env::current_dir().map_err(|e| format!("{}", e))?;
13    let root = match given_root {
14        Some(root) => {
15            let root = Path::new(root);
16            if root.is_absolute() {
17                root.to_path_buf()
18            } else {
19                current_dir.join(root)
20            }
21        }
22        None => current_dir,
23    };
24
25    if !root.join("Cargo.toml").is_file() {
26        return Err(format!(
27            "`{:?}` does not look like a Rust/Cargo project",
28            root
29        ));
30    }
31
32    Ok(root)
33}
34
35/// Find the default entrypoiny to read the doc comments from
36///
37/// Try to read entrypoint in the following order:
38/// - src/lib.rs
39/// - src/main.rs
40/// - file defined in the `[lib]` section of Cargo.toml
41/// - file defined in the `[[bin]]` section of Cargo.toml, if there is only one
42///   - if there is more than one `[[bin]]`, an error is returned
43pub fn find_entrypoint(current_dir: &Path, manifest: &Manifest) -> Result<PathBuf, String> {
44    // try lib.rs
45    let lib_rs = current_dir.join("src/lib.rs");
46    if lib_rs.exists() {
47        return Ok(lib_rs);
48    }
49
50    // try main.rs
51    let main_rs = current_dir.join("src/main.rs");
52    if main_rs.exists() {
53        return Ok(main_rs);
54    }
55
56    // try lib defined in `Cargo.toml`
57    if let Some(ManifestLib {
58        path: ref lib,
59        doc: true,
60    }) = manifest.lib
61    {
62        return Ok(lib.to_path_buf());
63    }
64
65    // try bin defined in `Cargo.toml`
66    if manifest.bin.len() > 0 {
67        let mut bin_list: Vec<_> = manifest
68            .bin
69            .iter()
70            .filter(|b| b.doc == true)
71            .map(|b| b.path.clone())
72            .collect();
73
74        if bin_list.len() > 1 {
75            let paths = bin_list
76                .iter()
77                .map(|p| p.to_string_lossy())
78                .collect::<Vec<_>>()
79                .join(", ");
80            return Err(format!("Multiple binaries found, choose one: [{}]", paths));
81        }
82
83        if let Some(bin) = bin_list.pop() {
84            return Ok(bin);
85        }
86    }
87
88    // if no entrypoint is found, return an error
89    Err("No entrypoint found".to_owned())
90}