sancus_lib/
file_info.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6//
7// SPDX-License-Identifier: MIT OR Apache-2.0
8//
9// SPDX-FileCopyrightText: 2024 X-Software GmbH <opensource@x-software.com>
10
11use std::{
12    fs,
13    path::{Path, PathBuf},
14};
15#[derive(Debug, Clone)]
16pub struct FileInfo {
17    pub name: String,
18    pub path: PathBuf,
19    pub extension: Option<String>,
20}
21
22impl FileInfo {
23    pub fn new(name: String, path: &Path) -> Self {
24        Self {
25            name,
26            path: path.to_path_buf(),
27            extension: path.extension().map(|e| e.to_string_lossy().into_owned()),
28        }
29    }
30}
31
32pub fn find_files_recurse(path: &PathBuf, filter: &str, ignore_list: &[String]) -> std::io::Result<Vec<FileInfo>> {
33    let mut libs: Vec<_> = vec![];
34    for entry in fs::read_dir(path)? {
35        let entry = entry?;
36        let path = entry.path().clone();
37        let name = entry.file_name().to_string_lossy().into_owned();
38
39        if path.is_dir() && !ignore_list.contains(&name) {
40            let sub_libs = find_files_recurse(&path, filter, ignore_list)?;
41            for lib in sub_libs {
42                libs.push(lib);
43            }
44        } else if path.is_file() && name.contains(filter) && !ignore_list.contains(&name) {
45            libs.push(FileInfo::new(name, path.as_path()));
46        }
47    }
48
49    Ok(libs)
50}