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
use crate::*;

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::io::{Error, ErrorKind};
use std::path::{Component, Path, PathBuf};
use std::result::Result as StdResult;
use std::{env, fs};

use cargo_toml::Manifest;
use path_absolutize::*;

/// Represents *path info* on a Cargo project.
#[derive(Debug)]
pub struct Paths {
    /// *Base path* to a Cargo project directory
    pub root_path: PathBuf,

    /// Path to the *examples* in a Cargo project
    pub examples_path: PathBuf,

    /// Path to the *Cargo.toml* file
    pub cargo_toml_path: PathBuf,

    /// Parsed contents of the *Cargo.toml* manifest file
    pub manifest: Manifest,
}

/// Represents the *type* of an example file.
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ExampleType {
    /// This represents a *simple* example file, for ex. a `hello_world.rs`
    /// file in an `examples/` folder.
    Simple,

    /// This represents a `main.rs` file nested within a sub-folder in an
    /// `examples/` folder; the Rust book also calls this a **multi-file**
    /// example.
    MultiFile,

    /// This represents an example file with a *custom path* defined in the
    /// `Cargo.toml` file of a Cargo project.
    Custom,
}

/// Represents an *example file* in a Cargo project.
#[derive(Debug, Eq)]
pub struct ExampleFile {
    /// The *file stem* (i.e. filename without the extension) for an
    /// example file, or the *folder name* in the case of a `main.rs`
    /// example within a sub-folder.
    pub name: String,

    /// Path to example file
    pub path: PathBuf,

    /// Type of example file
    pub path_type: ExampleType,
}

/// *order* a sequence of `ExampleFile`s by the `name` field.

impl Ord for ExampleFile {
    fn cmp(&self, other: &Self) -> Ordering {
        self.name.cmp(&other.name)
    }
}

impl PartialOrd for ExampleFile {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Determine *equality* and *hash* using the `path` field.

impl PartialEq<Self> for ExampleFile {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
    }
}

impl Hash for ExampleFile {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.path.hash(state)
    }
}

impl PartialEq<PathBuf> for ExampleFile {
    fn eq(&self, other: &PathBuf) -> bool {
        &self.path == other
    }
}

impl TryFrom<PathBuf> for ExampleFile {
    type Error = Error;

    /// Try to create an `ExampleFile` from a `PathBuf` object.
    fn try_from(mut path: PathBuf) -> StdResult<Self, Self::Error> {
        let file_type = path.metadata()?.file_type();

        if file_type.is_dir() {
            path.push("main.rs");
            if path.is_file() {
                let mut comps = path.components();
                // discard the filename (main.rs)
                let _ = comps.next_back();
                // return the parent folder name
                let name = comps
                    .next_back()
                    .unwrap()
                    .as_os_str()
                    .to_str()
                    .unwrap()
                    .to_owned();

                return Ok(Self {
                    name,
                    path,
                    path_type: ExampleType::MultiFile,
                });
            }
        } else if file_type.is_file() && matches!(path.extension(), Some(e) if e == RUST_FILE_EXT) {
            let name = path.file_stem().unwrap().to_str().unwrap().to_owned();

            return Ok(Self {
                name,
                path,
                path_type: ExampleType::Simple,
            });
        }

        // TODO
        Err(Error::new(ErrorKind::InvalidData, "not an example file"))
    }
}

impl ExampleFile {
    /// Create an `ExampleFile` from a example `name` and a (possibly relative)
    /// `path`.
    ///
    /// # Arguments
    /// * `root` - The current working directory, used in case the path is
    ///            relative.
    /// * `name` - The name of the example file, without the extension
    /// * `path` - The path to the example file. This can be a relative path
    ///            and contain characters such as `.` and `..` for instance.
    pub fn from_name_and_path<P: AsRef<Path>>(root: &Path, name: String, path: P) -> Self {
        let abs_path = path.as_ref().absolutize_from(root).unwrap();
        let path = PathBuf::from(abs_path);

        Self {
            name,
            path,
            path_type: ExampleType::Custom,
        }
    }
}

impl Paths {
    /// Iterates backward from the current directory, and locates the base
    /// Cargo directory for the project -- which contains at the minimum a
    /// `Cargo.toml` file and an `examples` folder.
    ///
    /// # Returns
    /// Returns the path details on the first such directory path which matches
    /// the criteria.
    ///
    /// # Errors
    /// Raises an error if it cannot locate the base Cargo directory for the
    /// project.
    pub fn resolve() -> Result<Self> {
        let current_dir = env::current_dir()?;

        let examples_folder = Path::new(EXAMPLES_FOLDER);
        let cargo_toml_file = Path::new(CARGO_TOML);

        let examples_path = current_dir.join(&examples_folder);
        let cargo_toml_path = current_dir.join(&cargo_toml_file);

        if examples_path.is_dir() && cargo_toml_path.is_file() {
            let manifest_contents = fs::read(&cargo_toml_path)?;
            let manifest = Manifest::from_slice(&manifest_contents)?;

            return Ok(Self {
                root_path: current_dir,
                examples_path,
                cargo_toml_path,
                manifest,
            });
        }

        let mut comps = current_dir.components();

        while let Some(p) = comps.next_back() {
            match p {
                Component::Normal(_) | Component::CurDir | Component::ParentDir => {
                    let root_path = comps.as_path().to_path_buf();

                    let examples_path = root_path.join(&examples_folder);
                    let cargo_toml_path = root_path.join(&cargo_toml_file);

                    if examples_path.is_dir() && cargo_toml_path.is_file() {
                        let manifest_contents = fs::read(&cargo_toml_path)?;
                        let manifest = Manifest::from_slice(&manifest_contents)?;

                        return Ok(Self {
                            root_path,
                            examples_path,
                            cargo_toml_path,
                            manifest,
                        });
                    }
                }
                _ => {}
            };
        }

        Err(Box::new(Error::new(
            ErrorKind::NotFound,
            format!(
                "could not find `{CARGO_TOML}` with an `{EXAMPLES_FOLDER}` folder \
                in `{cwd}` or any parent directory",
                cwd = current_dir.to_str().unwrap()
            ),
        )))
    }

    /// Return a mapping of *example name* to a list of *required features* for an example.
    pub fn example_to_required_features(&self) -> Result<HashMap<String, String>> {
        let mut name_to_required_features: HashMap<String, String> =
            HashMap::with_capacity(self.manifest.example.len());
        let manifest = &self.manifest;

        for example in manifest.example.iter() {
            match example.name {
                Some(ref name) if !example.required_features.is_empty() => {
                    name_to_required_features
                        .insert(name.clone(), example.required_features.join(" "));
                }
                _ => (),
            };
        }

        Ok(name_to_required_features)
    }

    /// Returns file paths (`PathBuf` objects) of each *example* file in the Cargo project.
    pub fn example_file_paths(&self) -> Result<HashSet<ExampleFile>> {
        let mut files = HashSet::new();

        let manifest = &self.manifest;
        let root = &self.root_path;

        for example in manifest.example.iter() {
            if let Some(ref path) = example.path {
                if let Some(ref name) = example.name {
                    // I debated whether to add this for Mac/Linux, however it
                    // seems like `cargo run --example` doesn't support
                    // backslashes (\) in `example.path` in Cargo.toml either,
                    // so it's likely not worth the effort in this case.

                    // #[cfg(not(target_family = "windows"))]
                    // let path = path.replace('\\', "/");

                    let f = ExampleFile::from_name_and_path(root, name.to_owned(), path);
                    files.insert(f);
                }
            }
        }

        for entry in fs::read_dir(&self.examples_path)?.filter_map(StdResult::ok) {
            let path: PathBuf = entry.path();

            if let Ok(f) = ExampleFile::try_from(path) {
                files.insert(f);
            }
        }

        Ok(files)
    }
}