Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions src/action.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use color_eyre::Result;
use std::marker::PhantomData;

pub trait Action<'a>: Send + 'a {
type Context: 'a;
pub trait Action: Send {
type Context;

fn act(&self, ctx: &Self::Context) -> Result<()>;
}

pub struct ClosureAction<'a, Context, F>(F, PhantomData<&'a Context>)
pub struct ClosureAction<Context, F>(F, PhantomData<Context>)
where
F: Fn(&Context) -> Result<()> + Send + 'a,
Context: 'a + Sync;
F: Fn(&Context) -> Result<()> + Send,
Context: Sync;

impl<'a, Context, F> ClosureAction<'a, Context, F>
impl<Context, F> ClosureAction<Context, F>
where
F: Fn(&Context) -> Result<()> + Send + 'a,
Context: 'a + Sync,
F: Fn(&Context) -> Result<()> + Send,
Context: Sync,
{
pub fn new(f: F) -> Self {
Self(f, PhantomData)
}
}

impl<'a, Context, F> Action<'a> for ClosureAction<'a, Context, F>
impl<Context, F> Action for ClosureAction<Context, F>
where
F: Fn(&Context) -> Result<()> + Send + 'a,
Context: 'a + Sync,
F: Fn(&Context) -> Result<()> + Send,
Context: Sync + Send,
{
type Context = Context;

Expand All @@ -34,26 +34,24 @@ where
}
}

pub struct ActionWrapper<'a, ActionContext, ActionT, F, Cushion>
pub struct ActionWrapper<ActionContext, ActionT, F, Cushion>
where
F: Fn(&Cushion) -> ActionContext + Send + 'a,
ActionT: Action<'a, Context = ActionContext>,
ActionContext: 'a,
Cushion: 'a + Sync,
F: Fn(&Cushion) -> ActionContext + Send,
ActionT: Action<Context = ActionContext>,
Cushion: Sync,
{
f: F,
action: ActionT,

_marker: PhantomData<&'a Cushion>,
_marker: PhantomData<Cushion>,
}

impl<'a, ActionContext, ActionT, F, Cushion> Action<'a>
for ActionWrapper<'a, ActionContext, ActionT, F, Cushion>
impl<ActionContext, ActionT, F, Cushion> Action
for ActionWrapper<ActionContext, ActionT, F, Cushion>
where
F: Fn(&Cushion) -> ActionContext + Send + 'a,
ActionT: Action<'a, Context = ActionContext>,
ActionContext: 'a,
Cushion: 'a + Sync,
F: Fn(&Cushion) -> ActionContext + Send,
ActionT: Action<Context = ActionContext>,
Cushion: Sync + Send,
{
type Context = Cushion;

Expand All @@ -62,12 +60,11 @@ where
}
}

impl<'a, ActionContext, ActionT, F, Cushion> ActionWrapper<'a, ActionContext, ActionT, F, Cushion>
impl<ActionContext, ActionT, F, Cushion> ActionWrapper<ActionContext, ActionT, F, Cushion>
where
F: Fn(&Cushion) -> ActionContext + Send + 'a,
ActionT: Action<'a, Context = ActionContext>,
ActionContext: 'a,
Cushion: 'a + Sync,
F: Fn(&Cushion) -> ActionContext + Send,
ActionT: Action<Context = ActionContext>,
Cushion: Sync,
{
pub fn new(action: ActionT, transformer: F) -> Self {
Self {
Expand Down
49 changes: 23 additions & 26 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@
// 本当はasyncのほうがいいかも?
use std::marker::PhantomData;

pub trait Filter<'a>: Send + 'a {
type Context: 'a;
pub trait Filter: Send {
type Context;

fn predicate(&self, ctx: &Self::Context, input: &str) -> bool;
}

pub struct ClosureFilter<'a, Context, F>(F, PhantomData<&'a Context>)
pub struct ClosureFilter<Context, F>(F, PhantomData<Context>)
where
F: Fn(&Context, &str) -> bool,
Context: 'a;
F: Fn(&Context, &str) -> bool;

impl<'a, Context, F> ClosureFilter<'a, Context, F>
impl<Context, F> ClosureFilter<Context, F>
where
F: Fn(&Context, &str) -> bool,
Context: 'a,
{
pub fn new(f: F) -> Self {
Self(f, PhantomData)
}
}

impl<'a, Context, F> Filter<'a> for ClosureFilter<'a, Context, F>
impl<Context, F> Filter for ClosureFilter<Context, F>
where
F: Fn(&Context, &str) -> bool + Send + 'a,
Context: 'a + Sync,
F: Fn(&Context, &str) -> bool + Send,
Context: Sync + Send,
{
type Context = Context;

Expand All @@ -35,25 +33,24 @@ where
}
}

pub struct FilterWrapper<'a, FilterContext, FilterT, F, Cushion>
pub struct FilterWrapper<FilterContext, FilterT, F, Cushion>
where
F: Fn(&Cushion) -> FilterContext + Send + 'a,
FilterT: Filter<'a, Context = FilterContext>,
FilterContext: 'a,
F: Fn(&Cushion) -> FilterContext + Send,
FilterT: Filter<Context = FilterContext>,
{
f: F,
filter: FilterT,

_marker: PhantomData<(&'a FilterContext, Cushion)>,
_marker: PhantomData<(FilterContext, Cushion)>,
}

impl<'a, FilterContext, FilterT, F, Cushion> Filter<'a>
for FilterWrapper<'a, FilterContext, FilterT, F, Cushion>
impl<FilterContext, FilterT, F, Cushion> Filter
for FilterWrapper<FilterContext, FilterT, F, Cushion>
where
F: Fn(&Cushion) -> FilterContext + Send + 'a,
FilterT: Filter<'a, Context = FilterContext>,
FilterContext: 'a + Sync,
Cushion: 'a + Send,
F: Fn(&Cushion) -> FilterContext + Send,
FilterT: Filter<Context = FilterContext>,
FilterContext: Sync + Send,
Cushion: Send,
{
type Context = Cushion;

Expand All @@ -62,12 +59,12 @@ where
}
}

impl<'a, FilterContext, FilterT, F, Cushion> FilterWrapper<'a, FilterContext, FilterT, F, Cushion>
impl<FilterContext, FilterT, F, Cushion> FilterWrapper<FilterContext, FilterT, F, Cushion>
where
F: Fn(&Cushion) -> FilterContext + Send + 'a,
FilterT: Filter<'a, Context = FilterContext>,
FilterContext: 'a + Sync,
Cushion: 'a + Send,
F: Fn(&Cushion) -> FilterContext + Send,
FilterT: Filter<Context = FilterContext>,
FilterContext: Sync,
Cushion: Send,
{
pub fn new(filter: FilterT, transformer: F) -> Self {
Self {
Expand Down
92 changes: 44 additions & 48 deletions src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ use crate::ui::UI;

pub mod batcher;

pub struct Launcher<'a, Cushion, UIT, UIContext>
pub struct Launcher<Cushion, UIT, UIContext>
where
UIT: UI<'a, Context = UIContext>,
UIContext: 'a + Send,
Cushion: 'a + Sync,
UIT: UI<Context = UIContext>,
UIContext: Send,
Cushion: Sync + Send,
{
batcher: Batcher<'a, Cushion, UIContext>,
batcher: Batcher<Cushion, UIContext>,

actions: Vec<Box<dyn Action<'a, Context = Cushion>>>,
actions: Vec<Box<dyn Action<Context = Cushion>>>,
ui: Option<UIT>,
}

impl<'a, Cushion, UIT, UIContext> Default for Launcher<'a, Cushion, UIT, UIContext>
impl<Cushion, UIT, UIContext> Default for Launcher<Cushion, UIT, UIContext>
where
UIT: UI<'a, Context = UIContext> + 'a,
UIContext: 'a + Send,
Cushion: 'a + Sync,
UIT: UI<Context = UIContext>,
UIContext: Send,
Cushion: Sync + Send,
{
fn default() -> Self {
Self {
Expand All @@ -49,77 +49,74 @@ where
/// * `std::convert::Into::into`
///
/// as the transformer function
impl<'a, Cushion, UIT, UIContext> Launcher<'a, Cushion, UIT, UIContext>
impl<Cushion, UIT, UIContext> Launcher<Cushion, UIT, UIContext>
where
UIT: UI<'a, Context = UIContext> + Sync + 'a,
UIContext: 'a + Send,
Cushion: 'a + Send + Sync,
UIT: UI<Context = UIContext>,
UIContext: Send,
Cushion: Send + Sync + 'static,
{
pub fn add_source<SourceContext, F>(
self,
source: Source<'a, SourceContext>,
transformer: F,
) -> Self
pub fn add_source<SourceContext, F>(self, source: Source<SourceContext>, transformer: F) -> Self
where
F: Fn(SourceContext) -> Cushion + Send + 'a,
SourceContext: 'a,
F: Fn(SourceContext) -> Cushion + Send + 'static,
SourceContext: 'static,
{
self.add_raw_source(transform_source(source, transformer))
}

pub fn add_raw_source(mut self, source: Source<'a, Cushion>) -> Self {
pub fn add_raw_source(mut self, source: Source<Cushion>) -> Self {
self.batcher.add_raw_source(source);
self
}

pub fn add_filter<FilterContext, FilterT, F>(self, filter: FilterT, transformer: F) -> Self
where
F: Fn(&Cushion) -> FilterContext + Send + 'a,
FilterContext: 'a + Sync,
FilterT: Filter<'a, Context = FilterContext> + 'a,
FilterT: Filter<Context = FilterContext> + 'static,
FilterContext: Sync + Send + 'static,
F: Fn(&Cushion) -> FilterContext + Send + 'static,
{
self.add_raw_filter(FilterWrapper::new(filter, transformer))
}

pub fn add_raw_filter<FilterT>(mut self, filter: FilterT) -> Self
where
FilterT: Filter<'a, Context = Cushion> + 'a,
FilterT: Filter<Context = Cushion> + 'static,
{
self.batcher.add_raw_filter(filter);
self
}

pub fn add_sorter<SorterContext, SorterT, F>(self, sorter: SorterT, transformer: F) -> Self
where
F: Fn(&Cushion) -> SorterContext + Send + 'a,
SorterContext: 'a + Sync,
SorterT: Sorter<'a, Context = SorterContext> + 'a,
Cushion: 'a + Send,
SorterT: Sorter<Context = SorterContext> + 'static,
SorterContext: Sync + Send + 'static,
F: Fn(&Cushion) -> SorterContext + Send + 'static,
{
self.add_raw_sorter(SorterWrapper::new(sorter, transformer))
}

pub fn add_raw_sorter<SorterT>(mut self, sorter: SorterT) -> Self
where
SorterT: Sorter<'a, Context = Cushion> + 'a,
SorterT: Sorter<Context = Cushion> + 'static,
{
self.batcher.add_raw_sorter(sorter);
self
}

pub fn add_action<ActionContext, ActionT, F>(self, action: ActionT, transformer: F) -> Self
pub fn add_action<ActionContext: 'static, ActionT, F>(
self,
action: ActionT,
transformer: F,
) -> Self
where
F: Fn(&Cushion) -> ActionContext + Send + 'a,
ActionT: Action<'a, Context = ActionContext> + 'a,
ActionContext: 'a,
Cushion: 'a + Sync,
ActionT: Action<Context = ActionContext> + 'static,
F: Fn(&Cushion) -> ActionContext + Send + 'static,
{
self.add_raw_action(ActionWrapper::new(action, transformer))
}

pub fn add_raw_action<ActionT>(mut self, action: ActionT) -> Self
where
ActionT: Action<'a, Context = Cushion> + 'a,
ActionT: Action<Context = Cushion> + 'static,
{
self.actions.push(Box::new(action));

Expand All @@ -128,41 +125,40 @@ where

pub fn set_ui<F>(mut self, ui: UIT, transformer: F) -> Self
where
F: Fn(&Cushion) -> UIContext + Send + Sync + 'a,
F: Fn(&Cushion) -> UIContext + Send + Sync + 'static,
{
self.ui = Some(ui);
self.batcher.cusion_to_ui = Some(Box::new(transformer));
self.batcher.cushion_to_ui = Some(Box::new(transformer));
self
}

pub fn add_generator<Item, GenT, F>(self, generator: GenT, transformer: F) -> Self
where
Item: 'a,
F: Fn(Item) -> Cushion + Sync + Send + 'a,
GenT: Generator<Item = Item> + Sync + Send + 'a,
Cushion: Sync + 'a,
GenT: Generator<Item = Item> + Sync + Send + 'static,
Item: 'static,
F: Fn(Item) -> Cushion + Sync + Send + 'static,
{
self.add_raw_generator(GenWrapper::new(generator, transformer))
}

pub fn add_raw_generator<GenT>(mut self, generator: GenT) -> Self
where
GenT: Generator<Item = Cushion> + Sync + Send + 'a,
GenT: Generator<Item = Cushion> + Sync + Send + 'static,
{
self.batcher.add_raw_generator(generator);
self
}

pub async fn run(self) -> Result<()> {
let cusion: Option<Cushion> = self
let cushion: Option<Cushion> = self
.ui
.ok_or_eyre("UI must be set before calling run")?
.run(self.batcher)
.await?;

if let Some(cusion) = cusion {
if let Some(cushion) = cushion {
for ai in self.actions {
ai.act(&cusion)?;
ai.act(&cushion)?;
}
}

Expand All @@ -171,7 +167,7 @@ where

/// If `filter_and` is true and more than one filter is provided,
/// the launcher will display only entries that satisfy all filter predicates.
/// The default value is false.
/// The default value is true.
pub fn filter_and(mut self, flag: bool) -> Self {
self.batcher.filter_and = flag;
self
Expand Down
Loading