From 73ad07d0a3a39b1fadded885c8bcf001822f74c6 Mon Sep 17 00:00:00 2001 From: shouya <526598+shouya@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:52:32 +0900 Subject: [PATCH] define dynamic source as a source variant --- src/server/endpoint.rs | 22 +++++++++++----------- src/server/web/endpoint.rs | 30 ++++++++++++------------------ src/server/web/list.rs | 14 +++++++------- src/source.rs | 10 ++++++++-- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/server/endpoint.rs b/src/server/endpoint.rs index 8913a66..38cc5d3 100644 --- a/src/server/endpoint.rs +++ b/src/server/endpoint.rs @@ -45,7 +45,7 @@ impl EndpointConfig { path: path.to_string(), note: Some("Default On-the-fly filter endpoint".to_string()), config: EndpointServiceConfig { - source: None, + source: SourceConfig::Dynamic, filters: FilterPipelineConfig::default(), on_the_fly_filters: true, client: None, @@ -66,8 +66,8 @@ impl EndpointConfig { EndpointService::from_config(self.config).await } - pub(crate) fn source(&self) -> Option<&SourceConfig> { - self.config.source.as_ref() + pub(crate) fn source(&self) -> &SourceConfig { + &self.config.source } } @@ -76,7 +76,7 @@ impl EndpointConfig { )] pub struct EndpointServiceConfig { #[serde(default)] - pub source: Option, + pub source: SourceConfig, #[serde(default)] pub filters: FilterPipelineConfig, #[serde(default)] @@ -97,7 +97,7 @@ pub struct EndpointServiceConfig { pub struct EndpointService { // used for detecting changes in the config for partial update config: EndpointServiceConfig, - source: Option, + source: Source, on_the_fly_filter: Option>>, filters: Arc, client: Arc, @@ -235,7 +235,7 @@ impl EndpointService { self } - pub fn source(&self) -> &Option { + pub fn source(&self) -> &Source { &self.source } @@ -262,7 +262,7 @@ impl EndpointService { let default_cache_ttl = Duration::from_secs(15 * 60); let client = config.client.unwrap_or_default().build(default_cache_ttl)?; - let source = config.source.map(|s| s.try_into()).transpose()?; + let source = config.source.try_into()?; let on_the_fly_filter = if config.on_the_fly_filters { Some(Default::default()) } else { @@ -313,13 +313,13 @@ impl EndpointService { fn find_source(&self, param: &Option) -> Result { match &self.source { - // ignore the source from param if it's already specified in config - Some(source) => Ok(source.clone()), - None => param + 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()), } } @@ -340,7 +340,7 @@ impl EndpointService { } if self.config.source != config.source { - let source = config.source.map(|s| s.try_into()).transpose()?; + let source = config.source.try_into()?; self.source = source; } diff --git a/src/server/web/endpoint.rs b/src/server/web/endpoint.rs index 30d4cce..63124c4 100644 --- a/src/server/web/endpoint.rs +++ b/src/server/web/endpoint.rs @@ -65,11 +65,9 @@ pub async fn render_endpoint_page( } section .source-and-config { - @if let Some(source) = source { - section .source-control { - (source); - div.loading { (sprite("loader")) } - } + section .source-control { + (source); + div.loading { (sprite("loader")) } } details { @@ -92,11 +90,11 @@ pub async fn render_endpoint_page( fn source_control_fragment( path: &str, - source: &Option, + source: &Source, param: &Result, -) -> Option { +) -> Markup { match source { - None => Some(html! { + Source::Dynamic => html! { input .hx-included.grow type="text" @@ -111,20 +109,16 @@ fn source_control_fragment( hx-target="main" hx-select="main" {} - }), - Some(Source::AbsoluteUrl(url)) => Some(html! { - div title="Source" .source { (url) } - }), - Some(Source::RelativeUrl(url)) => Some(html! { - div title="Source" .source { (url) } - }), - Some(Source::Templated(templated)) => Some(html! { + }, + Source::AbsoluteUrl(url) => html! {div title="Source" .source { (url) }}, + Source::RelativeUrl(url) => html! {div title="Source" .source { (url) }}, + Source::Templated(templated) => html! { div .source-template-container { @let queries = param.as_ref().ok().map(|p| p.extra_queries()); (source_template_fragment(templated, path, queries)); } - }), - Some(Source::FromScratch(scratch)) => Some(from_scratch_fragment(scratch)), + }, + Source::FromScratch(scratch) => from_scratch_fragment(scratch), } } diff --git a/src/server/web/list.rs b/src/server/web/list.rs index ea575fb..8820e0a 100644 --- a/src/server/web/list.rs +++ b/src/server/web/list.rs @@ -34,7 +34,7 @@ pub fn render_endpoint_list_page(root_config: &RootConfig) -> Markup { fn endpoint_list_entry_fragment(endpoint: &EndpointConfig) -> Markup { html! { - li ."my-.5" { + li { p { a href={"/_/endpoint/" (endpoint.path.trim_start_matches('/'))} { (endpoint.path) @@ -74,12 +74,12 @@ fn url_path(url: impl TryInto) -> Option { Some(url.path().to_owned()) } -fn short_source_repr(source: Option<&SourceConfig>) -> Markup { +fn short_source_repr(source: &SourceConfig) -> Markup { match source { - None => html! { + SourceConfig::Dynamic => html! { span .tag.dynamic { "dynamic" } }, - Some(SourceConfig::Simple(url)) if url.starts_with("/") => { + SourceConfig::Simple(url) if url.starts_with("/") => { let path = url_path(url.as_str()); let path = path.map(|p| format!("/_/{p}")); html! { @@ -94,7 +94,7 @@ fn short_source_repr(source: Option<&SourceConfig>) -> Markup { } } } - Some(SourceConfig::Simple(url)) => { + SourceConfig::Simple(url) => { let host = url_host(url.as_str()).unwrap_or_else(|| "...".into()); html! { span .tag.simple { @@ -102,12 +102,12 @@ fn short_source_repr(source: Option<&SourceConfig>) -> Markup { } } } - Some(SourceConfig::FromScratch(_)) => { + SourceConfig::FromScratch(_) => { html! { span .tag.scratch title="Made from scratch" { "scratch" } } } - Some(SourceConfig::Templated(_source)) => { + SourceConfig::Templated(_source) => { html! { span .tag.templated title="Templated source" { "templated" } } diff --git a/src/source.rs b/src/source.rs index f0b3845..ef9c6b6 100644 --- a/src/source.rs +++ b/src/source.rs @@ -19,11 +19,13 @@ lazy_static::lazy_static! { } #[derive( - JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, + JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, Default, )] #[serde(untagged)] /// # Feed source pub enum SourceConfig { + #[default] + Dynamic, /// # Simple source /// /// A source that is a simple URL. A relative path (e.g. "/feed.xml") @@ -141,6 +143,7 @@ impl Templated { JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, )] pub enum Source { + Dynamic, AbsoluteUrl(Url), RelativeUrl(String), Templated(Templated), @@ -192,6 +195,7 @@ impl TryFrom for Source { validate_placeholders(&config)?; Ok(Source::Templated(config)) } + SourceConfig::Dynamic => Ok(Source::Dynamic), } } } @@ -270,7 +274,9 @@ impl Source { let base = context.base_expected()?; base.join(path)? } - Source::Templated(_) | Source::FromScratch(_) => unreachable!(), + Source::Templated(_) | Source::FromScratch(_) | Source::Dynamic => { + unreachable!() + } }; let client =