lpk/src/ignore.rs

54 lines
1.2 KiB
Rust

use std::{
fs,
path::{Path, PathBuf},
};
use log::info;
/// Parses .pakignore file contents
fn parse(ignore_path: &Path, content: &str) -> Vec<PathBuf> {
// Get tree base path
let base_path = ignore_path.parent().unwrap();
// Split pakignore into lines
let lines = content.split('\n');
let mut ignore = Vec::new();
// Add pakignore to ignored list
ignore.push(ignore_path.to_path_buf());
for line in lines {
// Trim whitespace
let line = line.trim();
// Skip empty lines and comments
if line.is_empty() || line.starts_with('#') {
continue;
}
// Convert the &str to a PathBuf
let path = base_path.join(PathBuf::from(line));
info!("Adding to ignore list: {}", path.to_str().unwrap());
// Add to list
ignore.push(path);
}
ignore
}
/// Reads and parses a .pakignore file
pub fn read(path: &Path) -> Vec<PathBuf> {
match fs::read_to_string(path) {
Ok(content) => parse(path, &content),
Err(_) => Vec::new(),
}
}
/// Check if a path is ignored
pub fn is_ignored(ignored: &Vec<PathBuf>, file: &Path) -> bool {
ignored.into_iter().any(|path| path == file)
}