Skip to content

Commit

Permalink
Use SomeEnum instead of Option<SomeEnum> in cfg
Browse files Browse the repository at this point in the history
Simplify code by adding `None` to the enums we use for configuration
  • Loading branch information
nyurik committed Oct 20, 2023
1 parent 196df9e commit 18824c9
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 297 deletions.
26 changes: 13 additions & 13 deletions martin/src/args/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::args::connections::Arguments;
use crate::args::connections::State::{Ignore, Take};
use crate::args::environment::Env;
use crate::pg::{PgConfig, PgSslCerts, POOL_SIZE_DEFAULT};
use crate::utils::OneOrMany;
use crate::utils::{NoneBoolObj, NoneOneMany};

#[derive(clap::Args, Debug, PartialEq, Default)]
#[command(about, version)]
Expand All @@ -30,7 +30,7 @@ impl PgArgs {
self,
cli_strings: &mut Arguments,
env: &impl Env<'a>,
) -> Option<OneOrMany<PgConfig>> {
) -> NoneOneMany<PgConfig> {
let connections = Self::extract_conn_strings(cli_strings, env);
let default_srid = self.get_default_srid(env);
let certs = self.get_certs(env);
Expand All @@ -48,20 +48,20 @@ impl PgArgs {
},
max_feature_count: self.max_feature_count,
pool_size: self.pool_size,
auto_publish: None,
auto_publish: NoneBoolObj::None,
tables: None,
functions: None,
})
.collect();

match results.len() {
0 => None,
1 => Some(OneOrMany::One(results.into_iter().next().unwrap())),
_ => Some(OneOrMany::Many(results)),
0 => NoneOneMany::None,
1 => NoneOneMany::One(results.into_iter().next().unwrap()),
_ => NoneOneMany::Many(results),
}
}

pub fn override_config<'a>(self, pg_config: &mut OneOrMany<PgConfig>, env: &impl Env<'a>) {
pub fn override_config<'a>(self, pg_config: &mut NoneOneMany<PgConfig>, env: &impl Env<'a>) {
if self.default_srid.is_some() {
info!("Overriding configured default SRID to {} on all Postgres connections because of a CLI parameter", self.default_srid.unwrap());
pg_config.iter_mut().for_each(|c| {
Expand Down Expand Up @@ -224,10 +224,10 @@ mod tests {
let config = PgArgs::default().into_config(&mut args, &FauxEnv::default());
assert_eq!(
config,
Some(OneOrMany::One(PgConfig {
NoneOneMany::One(PgConfig {
connection_string: some("postgres://localhost:5432"),
..Default::default()
}))
})
);
assert!(args.check().is_ok());
}
Expand All @@ -248,15 +248,15 @@ mod tests {
let config = PgArgs::default().into_config(&mut args, &env);
assert_eq!(
config,
Some(OneOrMany::One(PgConfig {
NoneOneMany::One(PgConfig {
connection_string: some("postgres://localhost:5432"),
default_srid: Some(10),
ssl_certificates: PgSslCerts {
ssl_root_cert: Some(PathBuf::from("file")),
..Default::default()
},
..Default::default()
}))
})
);
assert!(args.check().is_ok());
}
Expand All @@ -282,7 +282,7 @@ mod tests {
let config = pg_args.into_config(&mut args, &env);
assert_eq!(
config,
Some(OneOrMany::One(PgConfig {
NoneOneMany::One(PgConfig {
connection_string: some("postgres://localhost:5432"),
default_srid: Some(20),
ssl_certificates: PgSslCerts {
Expand All @@ -291,7 +291,7 @@ mod tests {
ssl_root_cert: Some(PathBuf::from("root")),
},
..Default::default()
}))
})
);
assert!(args.check().is_ok());
}
Expand Down
16 changes: 8 additions & 8 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ impl Args {

let mut cli_strings = Arguments::new(self.meta.connection);
let pg_args = self.pg.unwrap_or_default();
if let Some(pg_config) = &mut config.postgres {
// config was loaded from a file, we can only apply a few CLI overrides to it
pg_args.override_config(pg_config, env);
} else {
if config.postgres.is_none() {
config.postgres = pg_args.into_config(&mut cli_strings, env);
} else {
// config was loaded from a file, we can only apply a few CLI overrides to it
pg_args.override_config(&mut config.postgres, env);
}

if !cli_strings.is_empty() {
Expand All @@ -85,7 +85,7 @@ impl Args {
}
}

pub fn parse_file_args(cli_strings: &mut Arguments, extension: &str) -> Option<FileConfigEnum> {
pub fn parse_file_args(cli_strings: &mut Arguments, extension: &str) -> FileConfigEnum {
let paths = cli_strings.process(|v| match PathBuf::try_from(v) {
Ok(v) => {
if v.is_dir() {
Expand All @@ -107,7 +107,7 @@ mod tests {
use super::*;
use crate::pg::PgConfig;
use crate::test_utils::{some, FauxEnv};
use crate::utils::OneOrMany;
use crate::utils::NoneOneMany;

fn parse(args: &[&str]) -> Result<(Config, MetaArgs)> {
let args = Args::parse_from(args);
Expand Down Expand Up @@ -143,10 +143,10 @@ mod tests {

let args = parse(&["martin", "postgres://connection"]).unwrap();
let cfg = Config {
postgres: Some(OneOrMany::One(PgConfig {
postgres: NoneOneMany::One(PgConfig {
connection_string: some("postgres://connection"),
..Default::default()
})),
}),
..Default::default()
};
let meta = MetaArgs {
Expand Down
70 changes: 25 additions & 45 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::source::{TileInfoSources, TileSources};
use crate::sprites::SpriteSources;
use crate::srv::SrvConfig;
use crate::Error::{ConfigLoadError, ConfigParseError, NoSources};
use crate::{IdResolver, OneOrMany, Result};
use crate::{IdResolver, NoneOneMany, Result};

pub type UnrecognizedValues = HashMap<String, serde_yaml::Value>;

Expand All @@ -31,17 +31,17 @@ pub struct Config {
#[serde(flatten)]
pub srv: SrvConfig,

#[serde(skip_serializing_if = "Option::is_none")]
pub postgres: Option<OneOrMany<PgConfig>>,
#[serde(default, skip_serializing_if = "NoneOneMany::is_none")]
pub postgres: NoneOneMany<PgConfig>,

#[serde(skip_serializing_if = "Option::is_none")]
pub pmtiles: Option<FileConfigEnum>,
#[serde(default, skip_serializing_if = "FileConfigEnum::is_none")]
pub pmtiles: FileConfigEnum,

#[serde(skip_serializing_if = "Option::is_none")]
pub mbtiles: Option<FileConfigEnum>,
#[serde(default, skip_serializing_if = "FileConfigEnum::is_none")]
pub mbtiles: FileConfigEnum,

#[serde(skip_serializing_if = "Option::is_none")]
pub sprites: Option<FileConfigEnum>,
#[serde(default, skip_serializing_if = "FileConfigEnum::is_none")]
pub sprites: FileConfigEnum,

#[serde(flatten)]
pub unrecognized: UnrecognizedValues,
Expand All @@ -53,40 +53,22 @@ impl Config {
let mut res = UnrecognizedValues::new();
copy_unrecognized_config(&mut res, "", &self.unrecognized);

let mut any = if let Some(pg) = &mut self.postgres {
for pg in pg.iter_mut() {
res.extend(pg.finalize()?);
}
!pg.is_empty()
} else {
false
};

any |= if let Some(cfg) = &mut self.pmtiles {
res.extend(cfg.finalize("pmtiles.")?);
!cfg.is_empty()
} else {
false
};
for pg in self.postgres.iter_mut() {
res.extend(pg.finalize()?);
}

any |= if let Some(cfg) = &mut self.mbtiles {
res.extend(cfg.finalize("mbtiles.")?);
!cfg.is_empty()
} else {
false
};
res.extend(self.pmtiles.finalize("pmtiles.")?);
res.extend(self.mbtiles.finalize("mbtiles.")?);
res.extend(self.sprites.finalize("sprites.")?);

any |= if let Some(cfg) = &mut self.sprites {
res.extend(cfg.finalize("sprites.")?);
!cfg.is_empty()
if self.postgres.is_empty()
&& self.pmtiles.is_empty()
&& self.mbtiles.is_empty()
&& self.sprites.is_empty()
{
Err(NoSources)
} else {
false
};

if any {
Ok(res)
} else {
Err(NoSources)
}
}

Expand All @@ -102,18 +84,16 @@ impl Config {
let create_mbt_src = &mut MbtSource::new_box;
let mut sources: Vec<Pin<Box<dyn Future<Output = Result<TileInfoSources>>>>> = Vec::new();

if let Some(v) = self.postgres.as_mut() {
for s in v.iter_mut() {
sources.push(Box::pin(s.resolve(idr.clone())));
}
for s in self.postgres.iter_mut() {
sources.push(Box::pin(s.resolve(idr.clone())));
}

if self.pmtiles.is_some() {
if !self.pmtiles.is_empty() {
let val = resolve_files(&mut self.pmtiles, idr.clone(), "pmtiles", create_pmt_src);
sources.push(Box::pin(val));
}

if self.mbtiles.is_some() {
if !self.mbtiles.is_empty() {
let val = resolve_files(&mut self.mbtiles, idr.clone(), "mbtiles", create_mbt_src);
sources.push(Box::pin(val));
}
Expand Down
Loading

0 comments on commit 18824c9

Please sign in to comment.