Skip to content

Commit

Permalink
simplify fetch_source logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 18, 2024
1 parent 73ad07d commit 4cfd735
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
10 changes: 10 additions & 0 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub struct FilterContext {
/// from a relative path.
base: Option<Url>,

/// User supplied source (`?source=` query parameter)
source: Option<Url>,

/// The maximum number of filters to run on this pipeline
filter_skip: Option<FilterSkip>,

Expand All @@ -63,6 +66,7 @@ impl FilterContext {
Self {
base: None,
filter_skip: None,
source: None,
extra_queries: HashMap::new(),
}
}
Expand All @@ -79,6 +83,10 @@ impl FilterContext {
&self.extra_queries
}

pub fn source(&self) -> Option<&Url> {
self.source.as_ref()
}

pub fn set_filter_skip(&mut self, filter_skip: FilterSkip) {
self.filter_skip = Some(filter_skip);
}
Expand All @@ -90,6 +98,7 @@ impl FilterContext {
pub fn subcontext(&self) -> Self {
Self {
base: self.base.clone(),
source: None,
filter_skip: None,
extra_queries: self.extra_queries.clone(),
}
Expand All @@ -98,6 +107,7 @@ impl FilterContext {
pub fn from_param(param: &crate::server::EndpointParam) -> Self {
Self {
base: param.base().cloned(),
source: param.source().cloned(),
filter_skip: param.filter_skip().cloned(),
extra_queries: param.extra_queries().clone(),
}
Expand Down
13 changes: 0 additions & 13 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ impl EndpointService {
}

pub async fn run(self, param: EndpointParam) -> Result<Feed> {
let source = self.find_source(&param.source)?;
let mut context = FilterContext::from_param(&param);
let feed = source
.fetch_feed(&context, Some(&self.client))
Expand Down Expand Up @@ -311,18 +310,6 @@ impl EndpointService {
Ok(feed)
}

fn find_source(&self, param: &Option<Url>) -> Result<Source> {
match &self.source {
Source::Dynamic => param
.as_ref()
.ok_or(Error::Message("missing source".into()))
.cloned()
.map(Source::from),
// ignore the source from param if it's already specified in config
source => Ok(source.clone()),
}
}

pub fn config_changed(&self, config: &EndpointServiceConfig) -> bool {
self.config != *config
}
Expand Down
34 changes: 14 additions & 20 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,24 @@ impl Source {
context: &FilterContext,
client: Option<&Client>,
) -> Result<Feed> {
if let Source::FromScratch(config) = self {
let feed = Feed::from(config);
return Ok(feed);
}

if let Source::Templated(config) = self {
let source = config.to_regular_source(context.extra_queries())?;
return Box::pin(source.fetch_feed(context, client)).await;
}
let client = client.ok_or_else(|| Error::Message("client not set".into()));

let source_url = match self {
Source::AbsoluteUrl(url) => url.clone(),
match self {
Source::Dynamic => {
let url = context.source().ok_or(Error::DynamicSourceUnspecified)?;
client?.fetch_feed(url).await
}
Source::AbsoluteUrl(url) => client?.fetch_feed(url).await,
Source::RelativeUrl(path) => {
let base = context.base_expected()?;
base.join(path)?
let url = context.base_expected()?.join(path)?;
client?.fetch_feed(&url).await
}
Source::Templated(_) | Source::FromScratch(_) | Source::Dynamic => {
unreachable!()
Source::FromScratch(config) => Ok(Feed::from(config)),
Source::Templated(template) => {
let source = template.to_regular_source(context.extra_queries())?;
Box::pin(source.fetch_feed(context, client.ok())).await
}
};

let client =
client.ok_or_else(|| Error::Message("client not set".into()))?;
client.fetch_feed(&source_url).await
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub enum Error {
#[error("Failed fetching source: {0}")]
FetchSource(Box<Error>),

#[error("Dynamic source unspecified")]
DynamicSourceUnspecified,

#[error("Source parameter {placeholder} failed to match validation: {validation} (input: {input})")]
SourceTemplateValidation {
placeholder: String,
Expand Down

0 comments on commit 4cfd735

Please sign in to comment.