Skip to content

Commit

Permalink
define dynamic source as a source variant
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 18, 2024
1 parent d178443 commit 73ad07d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
22 changes: 11 additions & 11 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}

Expand All @@ -76,7 +76,7 @@ impl EndpointConfig {
)]
pub struct EndpointServiceConfig {
#[serde(default)]
pub source: Option<SourceConfig>,
pub source: SourceConfig,
#[serde(default)]
pub filters: FilterPipelineConfig,
#[serde(default)]
Expand All @@ -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: Source,
on_the_fly_filter: Option<Arc<Mutex<OnTheFlyFilter>>>,
filters: Arc<FilterPipeline>,
client: Arc<Client>,
Expand Down Expand Up @@ -235,7 +235,7 @@ impl EndpointService {
self
}

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

Expand All @@ -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 {
Expand Down Expand Up @@ -313,13 +313,13 @@ impl EndpointService {

fn find_source(&self, param: &Option<Url>) -> Result<Source> {
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()),
}
}

Expand All @@ -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;
}

Expand Down
30 changes: 12 additions & 18 deletions src/server/web/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -92,11 +90,11 @@ pub async fn render_endpoint_page(

fn source_control_fragment(
path: &str,
source: &Option<Source>,
source: &Source,
param: &Result<EndpointParam, String>,
) -> Option<Markup> {
) -> Markup {
match source {
None => Some(html! {
Source::Dynamic => html! {
input
.hx-included.grow
type="text"
Expand All @@ -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),
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/server/web/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -74,12 +74,12 @@ fn url_path(url: impl TryInto<Url>) -> Option<String> {
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! {
Expand All @@ -94,20 +94,20 @@ 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 {
a href=(url) { (host) }
}
}
}
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" }
}
Expand Down
10 changes: 8 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -141,6 +143,7 @@ impl Templated {
JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash,
)]
pub enum Source {
Dynamic,
AbsoluteUrl(Url),
RelativeUrl(String),
Templated(Templated),
Expand Down Expand Up @@ -192,6 +195,7 @@ impl TryFrom<SourceConfig> for Source {
validate_placeholders(&config)?;
Ok(Source::Templated(config))
}
SourceConfig::Dynamic => Ok(Source::Dynamic),
}
}
}
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 73ad07d

Please sign in to comment.