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 anyhow::Result;
12use std::{
13    fs,
14    path::{Path, PathBuf},
15};
16#[derive(Debug, Clone)]
17pub struct FileInfo {
18    pub name: String,
19    pub path: PathBuf,
20    pub extension: Option<String>,
21}
22
23impl FileInfo {
24    pub fn new(name: String, path: &Path) -> Self {
25        Self {
26            name,
27            path: path.to_path_buf(),
28            extension: path.extension().map(|e| e.to_string_lossy().into_owned()),
29        }
30    }
31}
32
33pub fn find_files_recurse(path: &PathBuf, filter: &str, ignore_list: &[String]) -> Result<Vec<FileInfo>> {
34    let mut libs: Vec<_> = vec![];
35    for entry in fs::read_dir(path)? {
36        let entry = entry?;
37        let path = entry.path().clone();
38        let name = entry.file_name().to_string_lossy().into_owned();
39
40        if path.is_dir() && !ignore_list.contains(&name) {
41            let sub_libs = find_files_recurse(&path, filter, ignore_list)?;
42            for lib in sub_libs {
43                libs.push(lib);
44            }
45        } else if path.is_file() && name.contains(filter) && !ignore_list.contains(&name) {
46            libs.push(FileInfo::new(name, path.as_path()));
47        }
48    }
49
50    Ok(libs)
51}