feat(library): Add function for finding plugin configs

This commit is contained in:
Michael Aaron Murphy 2021-08-15 13:21:28 +02:00
parent ebb3e83b73
commit 2c14701898
2 changed files with 35 additions and 13 deletions

19
src/config.rs Normal file
View file

@ -0,0 +1,19 @@
use std::path::PathBuf;
pub fn find<'a>(name: &'a str) -> impl Iterator<Item = PathBuf> + 'a {
crate::plugin_paths()
.filter_map(|path| path.read_dir().ok())
.flat_map(move |dir| {
dir.filter_map(Result::ok).filter_map(move |entry| {
if entry.file_name() == name {
let path = entry.path();
let config_path = path.join("config.ron");
if config_path.exists() {
return Some(config_path);
}
}
None
})
})
}

View file

@ -1,10 +1,14 @@
mod codec; mod codec;
pub mod config;
pub use self::codec::*; pub use self::codec::*;
use const_format::concatcp; use const_format::concatcp;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{borrow::Cow, path::{Path, PathBuf}}; use std::{
borrow::Cow,
path::{Path, PathBuf},
};
pub const LOCAL: &str = "~/.local/share/pop-launcher"; pub const LOCAL: &str = "~/.local/share/pop-launcher";
pub const LOCAL_PLUGINS: &str = concatcp!(LOCAL, "/plugins"); pub const LOCAL_PLUGINS: &str = concatcp!(LOCAL, "/plugins");
@ -18,18 +22,17 @@ pub const DISTRIBUTION_PLUGINS: &str = concatcp!(DISTRIBUTION, "/plugins");
pub const PLUGIN_PATHS: &[&str] = &[LOCAL_PLUGINS, SYSTEM_PLUGINS, DISTRIBUTION_PLUGINS]; pub const PLUGIN_PATHS: &[&str] = &[LOCAL_PLUGINS, SYSTEM_PLUGINS, DISTRIBUTION_PLUGINS];
pub fn plugin_paths() -> impl Iterator<Item = Cow<'static, Path>> { pub fn plugin_paths() -> impl Iterator<Item = Cow<'static, Path>> {
PLUGIN_PATHS.iter() PLUGIN_PATHS.iter().map(|path| {
.map(|path| { #[allow(deprecated)]
#[allow(deprecated)] if let Some(path) = path.strip_prefix("~/") {
if let Some(path) = path.strip_prefix("~/") { let path = std::env::home_dir()
let path = std::env::home_dir() .expect("user does not have home dir")
.expect("user does not have home dir") .join(path);
.join(path); Cow::Owned(path)
Cow::Owned(path) } else {
} else { Cow::Borrowed(Path::new(path))
Cow::Borrowed(Path::new(path)) }
} })
})
} }
/// u32 value defining the generation of an indice. /// u32 value defining the generation of an indice.