From 173d337bc467124fe4dbc4029adabc9b61c39ea0 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 20 Nov 2023 19:11:19 -0500 Subject: [PATCH] Rename Result to MartinResult, fix CI --- martin/benches/bench.rs | 4 ++-- martin/src/args/connections.rs | 4 ++-- martin/src/args/root.rs | 10 ++++++--- martin/src/bin/martin-cp.rs | 7 ++++--- martin/src/bin/martin.rs | 4 ++-- martin/src/config.rs | 17 ++++++++-------- martin/src/file_config.rs | 7 ++++--- martin/src/lib.rs | 4 ++-- martin/src/mbtiles/mod.rs | 8 ++------ martin/src/pg/config.rs | 3 ++- martin/src/pg/pg_source.rs | 4 ++-- martin/src/pmtiles/mod.rs | 8 ++------ martin/src/source.rs | 5 +++-- martin/src/srv/server.rs | 37 +++++++++------------------------- martin/src/utils/error.rs | 3 ++- 15 files changed, 54 insertions(+), 71 deletions(-) diff --git a/martin/benches/bench.rs b/martin/benches/bench.rs index 7f3773b1a..e4d7bac29 100644 --- a/martin/benches/bench.rs +++ b/martin/benches/bench.rs @@ -6,7 +6,7 @@ use async_trait::async_trait; use criterion::async_executor::FuturesExecutor; use criterion::{criterion_group, criterion_main, Criterion}; use martin::srv::{get_tile_impl, TileRequest}; -use martin::{CatalogSourceEntry, MartinError, Source, Tile, TileSources, UrlQuery, Xyz}; +use martin::{CatalogSourceEntry, MartinResult, Source, Tile, TileSources, UrlQuery, Xyz}; use martin_tile_utils::{Encoding, Format, TileInfo}; use tilejson::{tilejson, TileJSON}; @@ -45,7 +45,7 @@ impl Source for NullSource { false } - async fn get_tile(&self, _xyz: &Xyz, _query: &Option) -> Result { + async fn get_tile(&self, _xyz: &Xyz, _query: &Option) -> MartinResult { Ok(Vec::new()) } diff --git a/martin/src/args/connections.rs b/martin/src/args/connections.rs index 7fc9430d3..17cbbac43 100644 --- a/martin/src/args/connections.rs +++ b/martin/src/args/connections.rs @@ -1,4 +1,4 @@ -use crate::MartinError; +use crate::{MartinError, MartinResult}; #[derive(Debug, Clone)] pub enum State { @@ -58,7 +58,7 @@ impl Arguments { } /// Check that all params have been claimed - pub fn check(self) -> Result<(), MartinError> { + pub fn check(self) -> MartinResult<()> { let mut unrecognized = Vec::new(); for (i, value) in self.values.into_iter().enumerate() { if let State::Ignore = self.state[i] { diff --git a/martin/src/args/root.rs b/martin/src/args/root.rs index 8506ab4b2..a0974cff7 100644 --- a/martin/src/args/root.rs +++ b/martin/src/args/root.rs @@ -11,7 +11,7 @@ use crate::args::State::{Ignore, Share, Take}; use crate::config::Config; use crate::file_config::FileConfigEnum; use crate::MartinError::ConfigAndConnectionsError; -use crate::{OptOneMany, Result}; +use crate::{MartinResult, OptOneMany}; #[derive(Parser, Debug, PartialEq, Default)] #[command(about, version)] @@ -58,7 +58,11 @@ pub struct ExtraArgs { } impl Args { - pub fn merge_into_config<'a>(self, config: &mut Config, env: &impl Env<'a>) -> Result<()> { + pub fn merge_into_config<'a>( + self, + config: &mut Config, + env: &impl Env<'a>, + ) -> MartinResult<()> { if self.meta.watch { warn!("The --watch flag is no longer supported, and will be ignored"); } @@ -125,7 +129,7 @@ mod tests { use crate::utils::OptOneMany; use crate::MartinError::UnrecognizableConnections; - fn parse(args: &[&str]) -> Result<(Config, MetaArgs)> { + fn parse(args: &[&str]) -> MartinResult<(Config, MetaArgs)> { let args = Args::parse_from(args); let meta = args.meta.clone(); let mut config = Config::default(); diff --git a/martin/src/bin/martin-cp.rs b/martin/src/bin/martin-cp.rs index 9001ca882..0602f88e4 100644 --- a/martin/src/bin/martin-cp.rs +++ b/martin/src/bin/martin-cp.rs @@ -11,7 +11,8 @@ use log::{debug, error, info, log_enabled}; use martin::args::{Args, ExtraArgs, MetaArgs, OsEnv, PgArgs, SrvArgs}; use martin::srv::{get_composite_tile, merge_tilejson, RESERVED_KEYWORDS}; use martin::{ - append_rect, read_config, Config, IdResolver, MartinError, Result, ServerState, TileRect, Xyz, + append_rect, read_config, Config, IdResolver, MartinError, MartinResult, ServerState, TileRect, + Xyz, }; use mbtiles::{ init_mbtiles_schema, is_empty_database, CopyDuplicateMode, MbtType, MbtTypeCli, Mbtiles, @@ -81,7 +82,7 @@ pub struct CopyArgs { pub zoom_levels: Vec, } -async fn start(copy_args: CopierArgs) -> Result<()> { +async fn start(copy_args: CopierArgs) -> MartinResult<()> { info!("Starting Martin v{VERSION}"); let env = OsEnv::default(); @@ -223,7 +224,7 @@ fn iterate_tiles(tiles: Vec) -> impl Iterator { }) } -async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> { +async fn run_tile_copy(args: CopyArgs, state: ServerState) -> MartinResult<()> { let output_file = &args.output_file; let concurrency = args.concurrency.unwrap_or(1); let (sources, _use_url_query, info) = state.tiles.get_sources(args.source.as_str(), None)?; diff --git a/martin/src/bin/martin.rs b/martin/src/bin/martin.rs index b6e5e7a25..5765e66e8 100644 --- a/martin/src/bin/martin.rs +++ b/martin/src/bin/martin.rs @@ -5,11 +5,11 @@ use clap::Parser; use log::{error, info, log_enabled}; use martin::args::{Args, OsEnv}; use martin::srv::{new_server, RESERVED_KEYWORDS}; -use martin::{read_config, Config, IdResolver, Result}; +use martin::{read_config, Config, IdResolver, MartinResult}; const VERSION: &str = env!("CARGO_PKG_VERSION"); -async fn start(args: Args) -> Result { +async fn start(args: Args) -> MartinResult { info!("Starting Martin v{VERSION}"); let env = OsEnv::default(); diff --git a/martin/src/config.rs b/martin/src/config.rs index 4efcba357..d06b1621e 100644 --- a/martin/src/config.rs +++ b/martin/src/config.rs @@ -20,7 +20,7 @@ use crate::source::{TileInfoSources, TileSources}; use crate::sprites::SpriteSources; use crate::srv::SrvConfig; use crate::MartinError::{ConfigLoadError, ConfigParseError, ConfigWriteError, NoSources}; -use crate::{IdResolver, OptOneMany, Result}; +use crate::{IdResolver, MartinResult, OptOneMany}; pub type UnrecognizedValues = HashMap; @@ -56,7 +56,7 @@ pub struct Config { impl Config { /// Apply defaults to the config, and validate if there is a connection string - pub fn finalize(&mut self) -> Result { + pub fn finalize(&mut self) -> MartinResult { let mut res = UnrecognizedValues::new(); copy_unrecognized_config(&mut res, "", &self.unrecognized); @@ -83,7 +83,7 @@ impl Config { } } - pub async fn resolve(&mut self, idr: IdResolver) -> Result { + pub async fn resolve(&mut self, idr: IdResolver) -> MartinResult { Ok(ServerState { tiles: self.resolve_tile_sources(idr).await?, sprites: SpriteSources::resolve(&mut self.sprites)?, @@ -91,10 +91,11 @@ impl Config { }) } - async fn resolve_tile_sources(&mut self, idr: IdResolver) -> Result { + async fn resolve_tile_sources(&mut self, idr: IdResolver) -> MartinResult { let new_pmt_src = &mut PmtSource::new_box; let new_mbt_src = &mut MbtSource::new_box; - let mut sources: Vec>>>> = Vec::new(); + let mut sources: Vec>>>> = + Vec::new(); for s in self.postgres.iter_mut() { sources.push(Box::pin(s.resolve(idr.clone()))); @@ -113,7 +114,7 @@ impl Config { Ok(TileSources::new(try_join_all(sources).await?)) } - pub fn save_to_file(&self, file_name: PathBuf) -> Result<()> { + pub fn save_to_file(&self, file_name: PathBuf) -> MartinResult<()> { let yaml = serde_yaml::to_string(&self).expect("Unable to serialize config"); if file_name.as_os_str() == OsStr::new("-") { info!("Current system configuration:"); @@ -147,7 +148,7 @@ pub fn copy_unrecognized_config( } /// Read config from a file -pub fn read_config<'a, M>(file_name: &Path, env: &'a M) -> Result +pub fn read_config<'a, M>(file_name: &Path, env: &'a M) -> MartinResult where M: VariableMap<'a>, M::Value: AsRef, @@ -159,7 +160,7 @@ where parse_config(&contents, env, file_name) } -pub fn parse_config<'a, M>(contents: &str, env: &'a M, file_name: &Path) -> Result +pub fn parse_config<'a, M>(contents: &str, env: &'a M, file_name: &Path) -> MartinResult where M: VariableMap<'a>, M::Value: AsRef, diff --git a/martin/src/file_config.rs b/martin/src/file_config.rs index 5f8b1160c..0336c3f9e 100644 --- a/martin/src/file_config.rs +++ b/martin/src/file_config.rs @@ -10,7 +10,8 @@ use serde::{Deserialize, Serialize}; use crate::config::{copy_unrecognized_config, UnrecognizedValues}; use crate::file_config::FileError::{InvalidFilePath, InvalidSourceFilePath, IoError}; use crate::source::{Source, TileInfoSources}; -use crate::utils::{IdResolver, MartinError, OptOneMany}; +use crate::utils::{IdResolver, OptOneMany}; +use crate::MartinResult; use crate::OptOneMany::{Many, One}; #[derive(thiserror::Error, Debug)] @@ -102,7 +103,7 @@ impl FileConfigEnum { } } - pub fn finalize(&self, prefix: &str) -> Result { + pub fn finalize(&self, prefix: &str) -> MartinResult { let mut res = UnrecognizedValues::new(); if let Self::Config(cfg) = self { copy_unrecognized_config(&mut res, prefix, &cfg.unrecognized); @@ -171,7 +172,7 @@ pub async fn resolve_files( idr: IdResolver, extension: &str, new_source: &mut impl FnMut(String, PathBuf) -> Fut, -) -> Result +) -> MartinResult where Fut: Future, FileError>>, { diff --git a/martin/src/lib.rs b/martin/src/lib.rs index 01b9d18a7..439fb6817 100644 --- a/martin/src/lib.rs +++ b/martin/src/lib.rs @@ -11,8 +11,8 @@ pub use source::{ mod utils; pub use utils::{ - append_rect, decode_brotli, decode_gzip, IdResolver, MartinError, OptBoolObj, OptOneMany, - Result, TileRect, Xyz, + append_rect, decode_brotli, decode_gzip, IdResolver, MartinError, MartinResult, OptBoolObj, + OptOneMany, TileRect, Xyz, }; pub mod args; diff --git a/martin/src/mbtiles/mod.rs b/martin/src/mbtiles/mod.rs index 003c19920..a2d90a91f 100644 --- a/martin/src/mbtiles/mod.rs +++ b/martin/src/mbtiles/mod.rs @@ -12,7 +12,7 @@ use tilejson::TileJSON; use crate::file_config::FileError; use crate::file_config::FileError::{AquireConnError, InvalidMetadata, IoError}; use crate::source::{Tile, UrlQuery}; -use crate::{MartinError, Source, Xyz}; +use crate::{MartinResult, Source, Xyz}; #[derive(Clone)] pub struct MbtSource { @@ -81,11 +81,7 @@ impl Source for MbtSource { Box::new(self.clone()) } - async fn get_tile( - &self, - xyz: &Xyz, - _url_query: &Option, - ) -> Result { + async fn get_tile(&self, xyz: &Xyz, _url_query: &Option) -> MartinResult { if let Some(tile) = self .mbtiles .get_tile(xyz.z, xyz.x, xyz.y) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index 39cac5b39..3acd0eb43 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -14,6 +14,7 @@ use crate::pg::configurator::PgBuilder; use crate::pg::Result; use crate::source::TileInfoSources; use crate::utils::{on_slow, IdResolver, OptBoolObj, OptOneMany}; +use crate::MartinResult; pub trait PgInfo { fn format_id(&self) -> String; @@ -111,7 +112,7 @@ impl PgConfig { Ok(res) } - pub async fn resolve(&mut self, id_resolver: IdResolver) -> crate::Result { + pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult { let pg = PgBuilder::new(self, id_resolver).await?; let inst_tables = on_slow( pg.instantiate_tables(), diff --git a/martin/src/pg/pg_source.rs b/martin/src/pg/pg_source.rs index 735d747af..a98c52672 100644 --- a/martin/src/pg/pg_source.rs +++ b/martin/src/pg/pg_source.rs @@ -12,7 +12,7 @@ use crate::pg::pool::PgPool; use crate::pg::utils::query_to_json; use crate::pg::PgError::{GetTileError, GetTileWithQueryError, PrepareQueryError}; use crate::source::{Source, Tile, UrlQuery}; -use crate::{Result, Xyz}; +use crate::{MartinResult, Xyz}; #[derive(Clone, Debug)] pub struct PgSource { @@ -56,7 +56,7 @@ impl Source for PgSource { self.info.use_url_query } - async fn get_tile(&self, xyz: &Xyz, url_query: &Option) -> Result { + async fn get_tile(&self, xyz: &Xyz, url_query: &Option) -> MartinResult { let empty_query = HashMap::new(); let url_query = url_query.as_ref().unwrap_or(&empty_query); let conn = self.pool.get().await?; diff --git a/martin/src/pmtiles/mod.rs b/martin/src/pmtiles/mod.rs index 2dfde8caf..b9987aea3 100644 --- a/martin/src/pmtiles/mod.rs +++ b/martin/src/pmtiles/mod.rs @@ -14,7 +14,7 @@ use tilejson::TileJSON; use crate::file_config::FileError; use crate::file_config::FileError::{InvalidMetadata, IoError}; use crate::source::{Source, Tile, UrlQuery}; -use crate::{MartinError, Xyz}; +use crate::{MartinResult, Xyz}; #[derive(Clone)] pub struct PmtSource { @@ -129,11 +129,7 @@ impl Source for PmtSource { Box::new(self.clone()) } - async fn get_tile( - &self, - xyz: &Xyz, - _url_query: &Option, - ) -> Result { + async fn get_tile(&self, xyz: &Xyz, _url_query: &Option) -> MartinResult { // TODO: optimize to return Bytes if let Some(t) = self .pmtiles diff --git a/martin/src/source.rs b/martin/src/source.rs index db1e4e04d..c27b0b26b 100644 --- a/martin/src/source.rs +++ b/martin/src/source.rs @@ -8,7 +8,7 @@ use martin_tile_utils::TileInfo; use serde::{Deserialize, Serialize}; use tilejson::TileJSON; -use crate::{Result, Xyz}; +use crate::{MartinResult, Xyz}; pub type Tile = Vec; pub type UrlQuery = HashMap; @@ -33,6 +33,7 @@ impl TileSources { ) } + #[must_use] pub fn get_catalog(&self) -> TileCatalog { self.0 .iter() @@ -107,7 +108,7 @@ pub trait Source: Send + Debug { false } - async fn get_tile(&self, xyz: &Xyz, query: &Option) -> Result; + async fn get_tile(&self, xyz: &Xyz, query: &Option) -> MartinResult; fn is_valid_zoom(&self, zoom: u8) -> bool { let tj = self.get_tilejson(); diff --git a/martin/src/srv/server.rs b/martin/src/srv/server.rs index 5403a83b5..0ef4759da 100755 --- a/martin/src/srv/server.rs +++ b/martin/src/srv/server.rs @@ -30,7 +30,7 @@ use crate::sprites::{SpriteCatalog, SpriteError, SpriteSources}; use crate::srv::config::{SrvConfig, KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT}; use crate::utils::{decode_brotli, decode_gzip, encode_brotli, encode_gzip}; use crate::MartinError::BindingError; -use crate::{MartinError, Xyz}; +use crate::{MartinResult, Xyz}; /// List of keywords that cannot be used as source IDs. Some of these are reserved for future use. /// Reserved keywords must never end in a "dot number" (e.g. ".1"). @@ -54,7 +54,7 @@ pub struct Catalog { } impl Catalog { - pub fn new(state: &ServerState) -> Result { + pub fn new(state: &ServerState) -> MartinResult { Ok(Self { tiles: state.tiles.get_catalog(), sprites: state.sprites.get_catalog()?, @@ -341,35 +341,20 @@ pub async fn get_tile_impl( y: path.y, }; - let id = &path.source_ids; - let zoom = xyz.z; - let src = sources.get_source(id)?; - if !TileSources::check_zoom(src, id, zoom) { - return Err(ErrorNotFound(format!( - "Zoom {zoom} is not valid for source {id}", - ))); - } - - let query = if src.support_url_query() { - Some(Query::::from_query(req.query_string())?.into_inner()) + let (sources, use_url_query, info) = sources.get_sources(&path.source_ids, Some(path.z))?; + let query = if use_url_query { + Some(req.query_string()) } else { None }; - let tile = src - .get_tile(&xyz, &query) - .await - .map_err(map_internal_error)?; + let tile = get_composite_tile(sources.as_slice(), info, &xyz, query).await?; Ok(if tile.is_empty() { HttpResponse::NoContent().finish() } else { // decide if (re-)encoding of the tile data is needed, and recompress if so - let (tile, info) = recompress( - tile, - src.get_tile_info(), - req.get_header::(), - )?; + let (tile, info) = recompress(tile, info, req.get_header::())?; let mut response = HttpResponse::Ok(); response.content_type(info.format.content_type()); if let Some(val) = info.encoding.content_encoding() { @@ -497,7 +482,7 @@ pub fn router(cfg: &mut web::ServiceConfig) { } /// Create a new initialized Actix `App` instance together with the listening address. -pub fn new_server(config: SrvConfig, state: ServerState) -> crate::Result<(Server, String)> { +pub fn new_server(config: SrvConfig, state: ServerState) -> MartinResult<(Server, String)> { let catalog = Catalog::new(&state)?; let keep_alive = Duration::from_secs(config.keep_alive.unwrap_or(KEEP_ALIVE_DEFAULT)); let worker_processes = config.worker_processes.unwrap_or_else(num_cpus::get); @@ -571,11 +556,7 @@ mod tests { unimplemented!() } - async fn get_tile( - &self, - _xyz: &Xyz, - _url_query: &Option, - ) -> Result { + async fn get_tile(&self, _xyz: &Xyz, _url_query: &Option) -> MartinResult { unimplemented!() } } diff --git a/martin/src/utils/error.rs b/martin/src/utils/error.rs index f8fe52228..d274cdfff 100644 --- a/martin/src/utils/error.rs +++ b/martin/src/utils/error.rs @@ -10,7 +10,8 @@ use crate::fonts::FontError; use crate::pg::PgError; use crate::sprites::SpriteError; -pub type Result = std::result::Result; +/// A convenience [`Result`](Result) +pub type MartinResult = Result; fn elide_vec(vec: &[String], max_items: usize, max_len: usize) -> String { let mut s = String::new();