Skip to content

Commit

Permalink
assume borrowed FilterContext
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 30, 2024
1 parent 5a8f466 commit cc35528
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
38 changes: 26 additions & 12 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ pub struct FilterContext {
logs: Option<Vec<String>>,
}

pub struct SubContext<'a> {
context: &'a mut FilterContext,
saved_filter_skip: Option<FilterSkip>,
}

impl AsMut<FilterContext> for SubContext<'_> {
fn as_mut(&mut self) -> &mut FilterContext {
self.context
}
}

impl Drop for SubContext<'_> {
fn drop(&mut self) {
self.context.filter_skip = self.saved_filter_skip.take();
}
}

impl FilterContext {
#[cfg(test)]
pub fn new() -> Self {
Expand Down Expand Up @@ -99,13 +116,11 @@ impl FilterContext {
self.base = Some(base);
}

pub fn subcontext(&self) -> Self {
Self {
base: self.base.clone(),
source: None,
filter_skip: None,
extra_queries: self.extra_queries.clone(),
logs: self.logs.clone(),
pub fn subcontext(&mut self) -> SubContext<'_> {
let saved_filter_skip = self.filter_skip.take();
SubContext {
context: self,
saved_filter_skip,
}
}

Expand All @@ -118,11 +133,10 @@ impl FilterContext {
}
}

pub fn enable_logging(self) -> Self {
Self {
logs: Some(Vec::new()),
..self
}
pub fn enable_logging(&mut self) {
if self.logs.is_none() {
self.logs = Some(Vec::new());
};
}

pub fn logs(&self) -> Option<&[String]> {
Expand Down
7 changes: 4 additions & 3 deletions src/filter/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ impl FeedFilter for Merge {
async fn run(&self, ctx: &mut FilterContext, mut feed: Feed) -> Result<Feed> {
let (new_feeds, errors) = self.fetch_sources(ctx).await?;

let mut subctx = ctx.subcontext();
for new_feed in new_feeds {
let ctx = ctx.subcontext();
let filtered_new_feed = self.filters.run(ctx, new_feed).await?;
feed.merge(filtered_new_feed)?;
let new_feed = self.filters.run(subctx.as_mut(), new_feed).await?;
feed.merge(new_feed)?;
}
drop(subctx);

for (source, error) in errors {
let post = post_from_error(source, error, ctx);
Expand Down
10 changes: 7 additions & 3 deletions src/filter_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ impl FilterPipelineConfig {
}

impl FilterPipeline {
pub async fn run(&self, context: FilterContext, feed: Feed) -> Result<Feed> {
pub async fn run(
&self,
context: &mut FilterContext,
feed: Feed,
) -> Result<Feed> {
self.inner.lock().await.run(context, feed).await
}

Expand Down Expand Up @@ -127,12 +131,12 @@ impl Inner {

async fn run(
&self,
mut context: FilterContext,
context: &mut FilterContext,
mut feed: Feed,
) -> Result<Feed> {
for (i, filter) in self.filters.iter().enumerate() {
if context.allows_filter(i) {
feed = self.step(i, filter, &mut context, feed).await?;
feed = self.step(i, filter, context, feed).await?;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod fetch;
use std::fs;
use std::path::PathBuf;

use builtin::Console;
use rquickjs::loader::{
BuiltinLoader, BuiltinResolver, FileResolver, Loader, Resolver, ScriptLoader,
};
Expand Down
2 changes: 1 addition & 1 deletion src/otf_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl OnTheFlyFilter {
pub async fn run(
&mut self,
query: OnTheFlyFilterQuery,
context: FilterContext,
context: &mut FilterContext,
feed: Feed,
) -> Result<Feed, Error> {
let pipeline = self.update(query).await?;
Expand Down

0 comments on commit cc35528

Please sign in to comment.