Skip to content

Commit

Permalink
Rename Result to MartinResult, fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 21, 2023
1 parent 12c662f commit 173d337
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 71 deletions.
4 changes: 2 additions & 2 deletions martin/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -45,7 +45,7 @@ impl Source for NullSource {
false
}

async fn get_tile(&self, _xyz: &Xyz, _query: &Option<UrlQuery>) -> Result<Tile, MartinError> {
async fn get_tile(&self, _xyz: &Xyz, _query: &Option<UrlQuery>) -> MartinResult<Tile> {
Ok(Vec::new())
}

Expand Down
4 changes: 2 additions & 2 deletions martin/src/args/connections.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::MartinError;
use crate::{MartinError, MartinResult};

#[derive(Debug, Clone)]
pub enum State<T: Clone> {
Expand Down Expand Up @@ -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] {
Expand Down
10 changes: 7 additions & 3 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions martin/src/bin/martin-cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -81,7 +82,7 @@ pub struct CopyArgs {
pub zoom_levels: Vec<u8>,
}

async fn start(copy_args: CopierArgs) -> Result<()> {
async fn start(copy_args: CopierArgs) -> MartinResult<()> {
info!("Starting Martin v{VERSION}");

let env = OsEnv::default();
Expand Down Expand Up @@ -223,7 +224,7 @@ fn iterate_tiles(tiles: Vec<TileRect>) -> impl Iterator<Item = Xyz> {
})
}

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)?;
Expand Down
4 changes: 2 additions & 2 deletions martin/src/bin/martin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server> {
async fn start(args: Args) -> MartinResult<Server> {
info!("Starting Martin v{VERSION}");

let env = OsEnv::default();
Expand Down
17 changes: 9 additions & 8 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, serde_yaml::Value>;

Expand Down Expand Up @@ -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<UnrecognizedValues> {
pub fn finalize(&mut self) -> MartinResult<UnrecognizedValues> {
let mut res = UnrecognizedValues::new();
copy_unrecognized_config(&mut res, "", &self.unrecognized);

Expand All @@ -83,18 +83,19 @@ impl Config {
}
}

pub async fn resolve(&mut self, idr: IdResolver) -> Result<ServerState> {
pub async fn resolve(&mut self, idr: IdResolver) -> MartinResult<ServerState> {
Ok(ServerState {
tiles: self.resolve_tile_sources(idr).await?,
sprites: SpriteSources::resolve(&mut self.sprites)?,
fonts: FontSources::resolve(&mut self.fonts)?,
})
}

async fn resolve_tile_sources(&mut self, idr: IdResolver) -> Result<TileSources> {
async fn resolve_tile_sources(&mut self, idr: IdResolver) -> MartinResult<TileSources> {
let new_pmt_src = &mut PmtSource::new_box;
let new_mbt_src = &mut MbtSource::new_box;
let mut sources: Vec<Pin<Box<dyn Future<Output = Result<TileInfoSources>>>>> = Vec::new();
let mut sources: Vec<Pin<Box<dyn Future<Output = MartinResult<TileInfoSources>>>>> =
Vec::new();

for s in self.postgres.iter_mut() {
sources.push(Box::pin(s.resolve(idr.clone())));
Expand All @@ -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:");
Expand Down Expand Up @@ -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<Config>
pub fn read_config<'a, M>(file_name: &Path, env: &'a M) -> MartinResult<Config>
where
M: VariableMap<'a>,
M::Value: AsRef<str>,
Expand All @@ -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<Config>
pub fn parse_config<'a, M>(contents: &str, env: &'a M, file_name: &Path) -> MartinResult<Config>
where
M: VariableMap<'a>,
M::Value: AsRef<str>,
Expand Down
7 changes: 4 additions & 3 deletions martin/src/file_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -102,7 +103,7 @@ impl FileConfigEnum {
}
}

pub fn finalize(&self, prefix: &str) -> Result<UnrecognizedValues, MartinError> {
pub fn finalize(&self, prefix: &str) -> MartinResult<UnrecognizedValues> {
let mut res = UnrecognizedValues::new();
if let Self::Config(cfg) = self {
copy_unrecognized_config(&mut res, prefix, &cfg.unrecognized);
Expand Down Expand Up @@ -171,7 +172,7 @@ pub async fn resolve_files<Fut>(
idr: IdResolver,
extension: &str,
new_source: &mut impl FnMut(String, PathBuf) -> Fut,
) -> Result<TileInfoSources, MartinError>
) -> MartinResult<TileInfoSources>
where
Fut: Future<Output = Result<Box<dyn Source>, FileError>>,
{
Expand Down
4 changes: 2 additions & 2 deletions martin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions martin/src/mbtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -81,11 +81,7 @@ impl Source for MbtSource {
Box::new(self.clone())
}

async fn get_tile(
&self,
xyz: &Xyz,
_url_query: &Option<UrlQuery>,
) -> Result<Tile, MartinError> {
async fn get_tile(&self, xyz: &Xyz, _url_query: &Option<UrlQuery>) -> MartinResult<Tile> {
if let Some(tile) = self
.mbtiles
.get_tile(xyz.z, xyz.x, xyz.y)
Expand Down
3 changes: 2 additions & 1 deletion martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +112,7 @@ impl PgConfig {
Ok(res)
}

pub async fn resolve(&mut self, id_resolver: IdResolver) -> crate::Result<TileInfoSources> {
pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult<TileInfoSources> {
let pg = PgBuilder::new(self, id_resolver).await?;
let inst_tables = on_slow(
pg.instantiate_tables(),
Expand Down
4 changes: 2 additions & 2 deletions martin/src/pg/pg_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Source for PgSource {
self.info.use_url_query
}

async fn get_tile(&self, xyz: &Xyz, url_query: &Option<UrlQuery>) -> Result<Tile> {
async fn get_tile(&self, xyz: &Xyz, url_query: &Option<UrlQuery>) -> MartinResult<Tile> {
let empty_query = HashMap::new();
let url_query = url_query.as_ref().unwrap_or(&empty_query);
let conn = self.pool.get().await?;
Expand Down
8 changes: 2 additions & 6 deletions martin/src/pmtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -129,11 +129,7 @@ impl Source for PmtSource {
Box::new(self.clone())
}

async fn get_tile(
&self,
xyz: &Xyz,
_url_query: &Option<UrlQuery>,
) -> Result<Tile, MartinError> {
async fn get_tile(&self, xyz: &Xyz, _url_query: &Option<UrlQuery>) -> MartinResult<Tile> {
// TODO: optimize to return Bytes
if let Some(t) = self
.pmtiles
Expand Down
5 changes: 3 additions & 2 deletions martin/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>;
pub type UrlQuery = HashMap<String, String>;
Expand All @@ -33,6 +33,7 @@ impl TileSources {
)
}

#[must_use]
pub fn get_catalog(&self) -> TileCatalog {
self.0
.iter()
Expand Down Expand Up @@ -107,7 +108,7 @@ pub trait Source: Send + Debug {
false
}

async fn get_tile(&self, xyz: &Xyz, query: &Option<UrlQuery>) -> Result<Tile>;
async fn get_tile(&self, xyz: &Xyz, query: &Option<UrlQuery>) -> MartinResult<Tile>;

fn is_valid_zoom(&self, zoom: u8) -> bool {
let tj = self.get_tilejson();
Expand Down
37 changes: 9 additions & 28 deletions martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -54,7 +54,7 @@ pub struct Catalog {
}

impl Catalog {
pub fn new(state: &ServerState) -> Result<Self, MartinError> {
pub fn new(state: &ServerState) -> MartinResult<Self> {
Ok(Self {
tiles: state.tiles.get_catalog(),
sprites: state.sprites.get_catalog()?,
Expand Down Expand Up @@ -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::<UrlQuery>::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::<AcceptEncoding>(),
)?;
let (tile, info) = recompress(tile, info, req.get_header::<AcceptEncoding>())?;
let mut response = HttpResponse::Ok();
response.content_type(info.format.content_type());
if let Some(val) = info.encoding.content_encoding() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -571,11 +556,7 @@ mod tests {
unimplemented!()
}

async fn get_tile(
&self,
_xyz: &Xyz,
_url_query: &Option<UrlQuery>,
) -> Result<Tile, MartinError> {
async fn get_tile(&self, _xyz: &Xyz, _url_query: &Option<UrlQuery>) -> MartinResult<Tile> {
unimplemented!()
}
}
Expand Down
Loading

0 comments on commit 173d337

Please sign in to comment.