Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force-simple parameter when copying #723

Merged
merged 6 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 147 additions & 34 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::path::{Path, PathBuf};

use anyhow::Result;
use clap::{Parser, Subcommand};
use martin_mbtiles::{Mbtiles, TileCopier, TileCopierOptions};
use martin_mbtiles::{copy_mbtiles_file, Mbtiles, TileCopierOptions};
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Connection, SqliteConnection};

#[derive(Parser, Debug)]
#[derive(Parser, PartialEq, Eq, Debug)]
#[command(
version,
name = "mbtiles",
Expand All @@ -20,7 +20,7 @@ pub struct Args {
command: Commands,
}

#[derive(Subcommand, Debug)]
#[derive(Subcommand, PartialEq, Eq, Debug)]
enum Commands {
// /// Prints all values in the metadata table.
// #[command(name = "meta-all")]
Expand All @@ -44,21 +44,7 @@ enum Commands {
// },
/// Copy tiles from one mbtiles file to another.
#[command(name = "copy")]
Copy {
/// MBTiles file to read from
src_file: PathBuf,
/// MBTiles file to write to
dst_file: PathBuf,
/// Minimum zoom level to copy
#[arg(long)]
min_zoom: Option<u8>,
/// Maximum zoom level to copy
#[arg(long)]
max_zoom: Option<u8>,
/// List of zoom levels to copy; if provided, min-zoom and max-zoom will be ignored
#[arg(long, value_delimiter(','))]
zoom_levels: Vec<u8>,
},
Copy(TileCopierOptions),
}

#[tokio::main]
Expand All @@ -69,22 +55,8 @@ async fn main() -> Result<()> {
Commands::MetaGetValue { file, key } => {
meta_get_value(file.as_path(), &key).await?;
}
Commands::Copy {
src_file,
dst_file,
min_zoom,
max_zoom,
zoom_levels,
} => {
let copy_opts = TileCopierOptions::new()
.verbose(args.verbose)
.min_zoom(min_zoom)
.max_zoom(max_zoom)
.zooms(zoom_levels);

let tile_copier = TileCopier::new(src_file, dst_file, copy_opts)?;

tile_copier.run().await?;
Commands::Copy(opts) => {
copy_mbtiles_file(opts).await?;
}
}

Expand All @@ -100,3 +72,144 @@ async fn meta_get_value(file: &Path, key: &str) -> Result<()> {
}
Ok(())
}

#[cfg(test)]
mod tests {
use crate::Args;
use crate::Commands::{Copy, MetaGetValue};
use clap::error::ErrorKind;
use clap::Parser;
use martin_mbtiles::TileCopierOptions;
use std::path::PathBuf;

#[test]
fn test_copy_no_arguments() {
assert_eq!(
Args::try_parse_from(["mbtiles", "copy"])
.unwrap_err()
.kind(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find!

ErrorKind::MissingRequiredArgument
);
}

#[test]
fn test_copy_minimal_arguments() {
assert_eq!(
Args::parse_from(["mbtiles", "copy", "src_file", "dst_file"]),
Args {
verbose: false,
command: Copy(TileCopierOptions::new(
PathBuf::from("src_file"),
PathBuf::from("dst_file")
))
}
);
}

#[test]
fn test_copy_min_max_zoom_arguments() {
assert_eq!(
Args::parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"100",
"--min-zoom",
"1"
]),
Args {
verbose: false,
command: Copy(
TileCopierOptions::new(PathBuf::from("src_file"), PathBuf::from("dst_file"))
.min_zoom(Some(1))
.max_zoom(Some(100))
)
}
);
}

#[test]
fn test_copy_min_max_zoom_no_arguments() {
assert_eq!(
Args::try_parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"--min-zoom",
])
.unwrap_err()
.kind(),
ErrorKind::InvalidValue
);
}

#[test]
fn test_copy_min_max_zoom_with_zoom_levels_arguments() {
assert_eq!(
Args::try_parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"100",
"--min-zoom",
"1",
"--zoom-levels",
"3,7,1"
])
.unwrap_err()
.kind(),
ErrorKind::ArgumentConflict
);
}

#[test]
fn test_copy_zoom_levels_arguments() {
assert_eq!(
Args::parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--zoom-levels",
"3,7,1"
]),
Args {
verbose: false,
command: Copy(
TileCopierOptions::new(PathBuf::from("src_file"), PathBuf::from("dst_file"))
.zoom_levels(vec![1, 3, 7])
)
}
);
}

#[test]
fn test_meta_get_no_arguments() {
assert_eq!(
Args::try_parse_from(["mbtiles", "meta-get"])
.unwrap_err()
.kind(),
ErrorKind::MissingRequiredArgument
);
}

#[test]
fn test_meta_get_with_arguments() {
assert_eq!(
Args::parse_from(["mbtiles", "meta-get", "src_file", "key"]),
Args {
verbose: false,
command: MetaGetValue {
file: PathBuf::from("src_file"),
key: "key".to_string(),
}
}
);
}
}
2 changes: 1 addition & 1 deletion martin-mbtiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mod tile_copier;
pub use errors::MbtError;
pub use mbtiles::{Mbtiles, Metadata};
pub use mbtiles_pool::MbtilesPool;
pub use tile_copier::{TileCopier, TileCopierOptions};
pub use tile_copier::{copy_mbtiles_file, TileCopierOptions};
Loading