1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::path::PathBuf;

use martin_tile_utils::TileInfo;
use sqlite_hashes::rusqlite;

use crate::MbtType;

#[derive(thiserror::Error, Debug)]
pub enum MbtError {
    #[error("The source and destination MBTiles files are the same: {}", .0.display())]
    SameSourceAndDestination(PathBuf),

    #[error("The diff file and source or destination MBTiles files are the same: {}", .0.display())]
    SameDiffAndSourceOrDestination(PathBuf),

    #[error("SQL Error {0}")]
    SqlxError(#[from] sqlx::Error),

    #[error("SQL Error {0}")]
    RusqliteError(#[from] rusqlite::Error),

    #[error("MBTile filepath contains unsupported characters: {}", .0.display())]
    UnsupportedCharsInFilepath(PathBuf),

    #[error("Inconsistent tile formats detected: {0} vs {1}")]
    InconsistentMetadata(TileInfo, TileInfo),

    #[error("Invalid data format for MBTile file {0}")]
    InvalidDataFormat(String),

    #[error("Integrity check failed for MBTile file {0} for the following reasons: \n {1:?}")]
    FailedIntegrityCheck(String, Vec<String>),

    #[error("At least one tile has mismatching hash: stored value is `{1}` != computed value `{2}` in MBTile file {0}")]
    IncorrectTileHash(String, String, String),

    #[error("Computed aggregate tiles hash {0} does not match tile data in metadata {1} for MBTile file {2}")]
    AggHashMismatch(String, String, String),

    #[error("Metadata value `agg_tiles_hash` is not set in MBTiles file {0}")]
    AggHashValueNotFound(String),

    #[error(r#"Filename "{0}" passed to SQLite must be valid UTF-8"#)]
    InvalidFilenameType(PathBuf),

    #[error("No tiles found")]
    NoTilesFound,

    #[error("The destination file {0} is not empty. Some operations like creating a diff file require the destination file to be non-existent or empty.")]
    NonEmptyTargetFile(PathBuf),

    #[error("The file {0} does not have the required uniqueness constraint")]
    NoUniquenessConstraint(String),

    #[error("Could not copy MBTiles file: {reason}")]
    UnsupportedCopyOperation { reason: String },

    #[error("Unexpected duplicate tiles found when copying")]
    DuplicateValues,

    #[error("Applying a patch while diffing is not supported")]
    CannotApplyPatchAndDiff,

    #[error("The MBTiles file {0} has data of type {1}, but the desired type was set to {2}")]
    MismatchedTargetType(PathBuf, MbtType, MbtType),
}

pub type MbtResult<T> = Result<T, MbtError>;