Skip to content
/ martin Public
forked from maplibre/martin

Commit

Permalink
Minor refactor and cleanup (maplibre#716)
Browse files Browse the repository at this point in the history
* moved `IdResolver` to a separate file
* added `just fmt2` build target to format code using nightly mode
* moved all Actix `Data<AppState>` into individual state objects, e.g.
`Data<Sources>` (allows other source types, separate from `Sources`
type)
* move all Source-related code to a new `Sources` struct (a simple
wrapper over a HashMap)
  • Loading branch information
nyurik authored Jun 15, 2023
1 parent 99ea3ff commit 7e20602
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 246 deletions.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ lint: fmt clippy
fmt:
cargo fmt --all -- --check

# Run Nightly cargo fmt, ordering imports
fmt2:
cargo +nightly fmt -- --config imports_granularity=Module,group_imports=StdExternalCrate

# Run cargo clippy
clippy:
cargo clippy --workspace --all-targets --all-features --bins --tests --lib --benches -- -D warnings
Expand Down
3 changes: 2 additions & 1 deletion martin-mbtiles/src/mbtiles_queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::errors::MbtResult;
use sqlx::{query, SqliteExecutor};

use crate::errors::MbtResult;

pub async fn is_deduplicated_type<T>(conn: &mut T) -> MbtResult<bool>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
Expand Down
26 changes: 16 additions & 10 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ use crate::file_config::{resolve_files, FileConfigEnum};
use crate::mbtiles::MbtSource;
use crate::pg::PgConfig;
use crate::pmtiles::PmtSource;
use crate::source::{IdResolver, Sources};
use crate::source::Sources;
use crate::srv::SrvConfig;
use crate::utils::{OneOrMany, Result};
use crate::utils::{IdResolver, OneOrMany, Result};
use crate::Error::{ConfigLoadError, ConfigParseError, NoSources};

pub struct AllSources {
pub sources: Sources,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Config {
#[serde(flatten)]
Expand Down Expand Up @@ -73,7 +77,7 @@ impl Config {
}
}

pub async fn resolve(&mut self, idr: IdResolver) -> Result<Sources> {
pub async fn resolve(&mut self, idr: IdResolver) -> Result<AllSources> {
let create_pmt_src = &mut PmtSource::new_box;
let create_mbt_src = &mut MbtSource::new_box;

Expand All @@ -93,13 +97,15 @@ impl Config {
sources.push(Box::pin(val));
}

Ok(try_join_all(sources)
.await?
.into_iter()
.fold(HashMap::new(), |mut acc, hashmap| {
acc.extend(hashmap);
acc
}))
Ok(AllSources {
sources: try_join_all(sources).await?.into_iter().fold(
Sources::default(),
|mut acc, hashmap| {
acc.extend(hashmap);
acc
},
),
})
}
}

Expand Down
47 changes: 26 additions & 21 deletions martin/src/file_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use serde_yaml::Value;

use crate::config::{copy_unrecognized_config, Unrecognized};
use crate::file_config::FileError::{InvalidFilePath, InvalidSourceFilePath, IoError};
use crate::utils::sorted_opt_map;
use crate::source::{Source, Sources, Xyz};
use crate::utils::{sorted_opt_map, Error, IdResolver, OneOrMany};
use crate::OneOrMany::{Many, One};
use crate::{Error, IdResolver, OneOrMany, Source, Sources, Xyz};

#[derive(thiserror::Error, Debug)]
pub enum FileError {
Expand Down Expand Up @@ -40,6 +40,22 @@ pub enum FileConfigEnum {
Config(FileConfig),
}

impl FileConfigEnum {
pub fn extract_file_config(&mut self) -> FileConfig {
match self {
FileConfigEnum::Path(path) => FileConfig {
paths: Some(One(mem::take(path))),
..FileConfig::default()
},
FileConfigEnum::Paths(paths) => FileConfig {
paths: Some(Many(mem::take(paths))),
..Default::default()
},
FileConfigEnum::Config(cfg) => mem::take(cfg),
}
}
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FileConfig {
/// A list of file paths
Expand Down Expand Up @@ -69,12 +85,12 @@ pub enum FileConfigSrc {
}

impl FileConfigSrc {
#[must_use]
pub fn path(&self) -> &PathBuf {
match self {
pub fn abs_path(&self) -> Result<PathBuf, FileError> {
let path = match self {
Self::Path(p) => p,
Self::Obj(o) => &o.path,
}
};
path.canonicalize().map_err(|e| IoError(e, path.clone()))
}
}

Expand Down Expand Up @@ -125,27 +141,16 @@ async fn resolve_int<Fut>(
where
Fut: Future<Output = Result<Box<dyn Source>, FileError>>,
{
let cfg = match config {
FileConfigEnum::Path(path) => FileConfig {
paths: Some(One(mem::take(path))),
..FileConfig::default()
},
FileConfigEnum::Paths(paths) => FileConfig {
paths: Some(Many(mem::take(paths))),
..Default::default()
},
FileConfigEnum::Config(cfg) => mem::take(cfg),
};
let cfg = config.extract_file_config();

let mut results = Sources::new();
let mut results = Sources::default();
let mut configs = HashMap::new();
let mut files = HashSet::new();
let mut directories = Vec::new();

if let Some(sources) = cfg.sources {
for (id, source) in sources {
let path = source.path();
let can = path.canonicalize().map_err(|e| IoError(e, path.clone()))?;
let can = source.abs_path()?;
if !can.is_file() {
// todo: maybe warn instead?
return Err(InvalidSourceFilePath(id.to_string(), can));
Expand Down Expand Up @@ -173,7 +178,7 @@ where
directories.push(path.clone());
path.read_dir()
.map_err(|e| IoError(e, path.clone()))?
.filter_map(std::result::Result::ok)
.filter_map(Result::ok)
.filter(|f| {
f.path().extension().filter(|e| *e == extension).is_some()
&& f.path().is_file()
Expand Down
6 changes: 4 additions & 2 deletions martin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ mod test_utils;
#[cfg(test)]
pub use crate::args::Env;
pub use crate::config::{read_config, Config};
pub use crate::source::{IdResolver, Source, Sources, Xyz};
pub use crate::utils::{decode_brotli, decode_gzip, BoolOrObject, Error, OneOrMany, Result};
pub use crate::source::{Source, Sources, Xyz};
pub use crate::utils::{
decode_brotli, decode_gzip, BoolOrObject, Error, IdResolver, OneOrMany, Result,
};

// Ensure README.md contains valid code
#[cfg(doctest)]
Expand Down
4 changes: 2 additions & 2 deletions martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::pg::config_function::FuncInfoSources;
use crate::pg::config_table::TableInfoSources;
use crate::pg::configurator::PgBuilder;
use crate::pg::utils::Result;
use crate::source::{IdResolver, Sources};
use crate::utils::{sorted_opt_map, BoolOrObject, OneOrMany};
use crate::source::Sources;
use crate::utils::{sorted_opt_map, BoolOrObject, IdResolver, OneOrMany};

pub trait PgInfo {
fn format_id(&self) -> String;
Expand Down
10 changes: 5 additions & 5 deletions martin/src/pg/configurator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;

use futures::future::join_all;
use itertools::Itertools;
Expand All @@ -14,8 +14,8 @@ use crate::pg::pool::PgPool;
use crate::pg::table_source::{calc_srid, get_table_sources, merge_table_info, table_to_query};
use crate::pg::utils::PgError::InvalidTableExtent;
use crate::pg::utils::Result;
use crate::source::{IdResolver, Sources};
use crate::utils::{find_info, normalize_key, BoolOrObject, InfoMap, OneOrMany};
use crate::source::Sources;
use crate::utils::{find_info, normalize_key, BoolOrObject, IdResolver, InfoMap, OneOrMany};

pub type SqlFuncInfoMapMap = InfoMap<InfoMap<(PgSqlInfo, FunctionInfo)>>;
pub type SqlTableInfoMapMapMap = InfoMap<InfoMap<InfoMap<TableInfo>>>;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl PgBuilder {
}
}

let mut res: Sources = HashMap::new();
let mut res = Sources::default();
let mut info_map = TableInfoSources::new();
let pending = join_all(pending).await;
for src in pending {
Expand All @@ -161,7 +161,7 @@ impl PgBuilder {

pub async fn instantiate_functions(&self) -> Result<(Sources, FuncInfoSources)> {
let mut all_funcs = get_function_sources(&self.pool).await?;
let mut res: Sources = HashMap::new();
let mut res = Sources::default();
let mut info_map = FuncInfoSources::new();
let mut used = HashSet::<(&str, &str)>::new();

Expand Down
Loading

0 comments on commit 7e20602

Please sign in to comment.