From 2c1470189867d8e5410dbdfb3d1a326b14210104 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Sun, 15 Aug 2021 13:21:28 +0200 Subject: [PATCH] feat(library): Add function for finding plugin configs --- src/config.rs | 19 +++++++++++++++++++ src/lib.rs | 29 ++++++++++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..1fbf17a --- /dev/null +++ b/src/config.rs @@ -0,0 +1,19 @@ +use std::path::PathBuf; + +pub fn find<'a>(name: &'a str) -> impl Iterator + '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 + }) + }) +} diff --git a/src/lib.rs b/src/lib.rs index 7164340..71b1b63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,14 @@ mod codec; +pub mod config; pub use self::codec::*; use const_format::concatcp; 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_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 fn plugin_paths() -> impl Iterator> { - PLUGIN_PATHS.iter() - .map(|path| { - #[allow(deprecated)] - if let Some(path) = path.strip_prefix("~/") { - let path = std::env::home_dir() - .expect("user does not have home dir") - .join(path); - Cow::Owned(path) - } else { - Cow::Borrowed(Path::new(path)) - } - }) + PLUGIN_PATHS.iter().map(|path| { + #[allow(deprecated)] + if let Some(path) = path.strip_prefix("~/") { + let path = std::env::home_dir() + .expect("user does not have home dir") + .join(path); + Cow::Owned(path) + } else { + Cow::Borrowed(Path::new(path)) + } + }) } /// u32 value defining the generation of an indice.