Move standard traits and structs to core

This commit is contained in:
Armin Friedl 2021-11-17 21:15:59 +01:00
parent 838b329553
commit 198e28460d
17 changed files with 56 additions and 46 deletions

1
src/core/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod traits;

23
src/core/traits.rs Normal file
View file

@ -0,0 +1,23 @@
#[derive(Clone, Debug)]
pub struct Entry {
pub source: &'static str,
pub name: String,
pub description: String,
pub identifier: u64,
}
pub trait Source: Send + Sync {
fn name(&self) -> &'static str;
fn entries(&mut self) -> Vec<Entry>;
fn actions(&self, _entry: &Entry) -> Vec<String>
{ vec![] }
fn exec_default(&self, entry: &Entry)
{ self.exec_action(entry, 0) }
fn exec_action(&self, entry: &Entry, action: u8);
}
pub trait Matcher: Send + Sync {
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)>;
}

View file

@ -6,12 +6,13 @@ use winit::event_loop::{ControlFlow, EventLoop};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode,
WindowEvent::{CloseRequested, ReceivedCharacter}};
use crate::matcher::{FuseMatcher, PrefixMatcher, SkimMatcher, FlxMatcher};
use crate::matcher::{FuseMatcher, PrefixMatcher};
mod roftl;
mod ui;
mod sources;
mod matcher;
mod core;
use roftl::Roftl;
@ -27,11 +28,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_primary_source(sources::Window::new())
.with_primary_matcher(PrefixMatcher::new());
debug!{"Source primary roftl sources"}
roftl.source_primary();
debug!{"Source additional roftl sources"}
roftl.source_additional();
debug!{"Source roftl sources"}
roftl.source();
debug!{"Build window"}
let event_loop = EventLoop::new();

View file

@ -1,4 +1,4 @@
use crate::roftl::Matcher;
use super::core::traits::Matcher;
use flx_rs::Score;
use log::debug;

View file

@ -2,7 +2,7 @@ use std::ops::Range;
use fuse_rust::Fuse;
use crate::roftl::Matcher;
use super::core::traits::Matcher;
fn flatten_ranges(ranges: Vec<Range<usize>>) -> Vec<usize> {
ranges.into_iter().flat_map(|range| {range.collect::<Vec<usize>>().into_iter()})

View file

@ -1,3 +1,5 @@
use super::core;
mod prefix;
pub use prefix::PrefixMatcher;

View file

@ -1,4 +1,4 @@
use crate::roftl::Matcher;
use super::core::traits::Matcher;
pub struct PrefixMatcher;

View file

@ -1,6 +1,6 @@
use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2};
use crate::roftl::Matcher;
use super::core::traits::Matcher;
pub struct SkimMatcher {
matcher: SkimMatcherV2

View file

@ -1,4 +1,3 @@
use std::rc::Rc;
use std::sync::RwLock;
use std::sync::Arc;
use log::debug;
@ -6,33 +5,14 @@ use rayon::prelude::*;
use super::matcher::PrefixMatcher;
use super::core::traits::Matcher;
use super::core::traits::Source;
use super::core::traits::Entry;
pub type SourceRef = Box<dyn Source>;
pub type MatcherRef = Box<dyn Matcher>;
#[derive(Clone, Debug)]
pub struct Entry {
pub source: &'static str,
pub name: String,
pub description: String,
pub identifier: u64,
}
pub trait Source: Send + Sync {
fn name(&self) -> &'static str;
fn entries(&mut self) -> Vec<Entry>;
fn actions(&self, _entry: &Entry) -> Vec<String>
{ vec![] }
fn exec_default(&self, entry: &Entry)
{ self.exec_action(entry, 0) }
fn exec_action(&self, entry: &Entry, action: u8);
}
pub trait Matcher: Send + Sync {
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)>;
}
pub struct Roftl {
sources: Arc<RwLock<Vec<SourceRef>>>,
source_chan: Option<std::sync::mpsc::Receiver<Vec<Entry>>>,
@ -66,7 +46,7 @@ impl Roftl {
self
}
pub fn add_source(mut self, source: SourceRef) -> Self {
pub fn add_source(self, source: SourceRef) -> Self {
if self.sources.read().unwrap().par_iter().any(|s| s.name().eq(source.name())) {
panic! {"Source with name '{}' already exists", source.name()}
}
@ -85,7 +65,12 @@ impl Roftl {
self
}
pub fn source_primary(&mut self) {
pub fn source(&mut self) {
self.source_primary();
self.source_additional();
}
fn source_primary(&mut self) {
self.entries = self.primary_source
.par_iter_mut()
.flat_map(|s| s.entries())
@ -94,7 +79,7 @@ impl Roftl {
debug!("Sourced {} primary entries", self.entries.len());
}
pub fn source_additional(&mut self) {
fn source_additional(&mut self) {
let (tx, rx) = std::sync::mpsc::channel();
self.source_chan = Some(rx);
@ -153,7 +138,6 @@ impl Roftl {
.par_iter()
.enumerate()
.filter_map(|(idx, entry)| {
if entry.source == "window" { return None }
if input.is_empty() { return Some((0.0, idx, entry, vec![])) }
let match_result = matcher.try_match(&entry.name, input);

View file

@ -5,7 +5,7 @@ use log::{debug, error, trace};
use walkdir::WalkDir;
use nix::unistd::{getpid, setpgid};
use crate::roftl::{Entry, Source};
use super::core::traits::{Entry, Source};
#[derive(Debug)]
struct App {

View file

@ -1,3 +1,5 @@
use super::core;
mod test;
pub use test::TestSource;

View file

@ -3,7 +3,7 @@ use log::{debug, error};
use nix::unistd::{getpid, setpgid};
use walkdir::WalkDir;
use crate::roftl::{Entry, Source};
use super::core::traits::{Entry, Source};
pub struct ShellHost {
scripts: Vec<PathBuf>,

View file

@ -1,4 +1,4 @@
use crate::roftl::{Entry, Source};
use super::core::traits::{Entry, Source};
pub struct TestSource {
entries: Vec<Entry>,

View file

@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::ptr;
use crate::roftl::{Entry, Source};
use super::core::traits::{Entry, Source};
use log::debug;
use xcb::ffi::XCB_CURRENT_TIME;
use xcb_util::ewmh;

View file

@ -1,3 +1,5 @@
use super::core;
mod ui;
pub use ui::draw;
pub use ui::redraw_quick;

View file

@ -1,5 +1,4 @@
use log::debug;
use rayon::result;
use super::theme::{Theme, ThemedContextExt};

View file

@ -4,7 +4,7 @@ use std::ffi::c_void;
use log::debug;
use winit::{platform::unix::WindowExtUnix, window::Window};
use crate::roftl::Entry;
use super::core::traits::Entry;
use super::painter;