Skip to main content

japan_reverse_geocoder/
error.rs

1use std::fmt;
2
3/// このライブラリのエラー型
4#[derive(Debug)]
5pub enum Error {
6    /// 座標が日本のメッシュ範囲(北緯20〜46度、東経122〜154度)の外
7    OutOfJapanRange {
8        /// 指定された緯度
9        lat: f64,
10        /// 指定された経度
11        lon: f64,
12    },
13    /// データファイルの形式が不正
14    InvalidData(String),
15    /// データファイルの読み込みエラー
16    Io(std::io::Error),
17}
18
19impl fmt::Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Error::OutOfJapanRange { lat, lon } => {
23                write!(
24                    f,
25                    "coordinate ({lat}, {lon}) is outside of Japan's range (lat 20-46, lon 122-154)"
26                )
27            }
28            Error::InvalidData(msg) => write!(f, "invalid data: {msg}"),
29            Error::Io(e) => write!(f, "I/O error: {e}"),
30        }
31    }
32}
33
34impl std::error::Error for Error {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        match self {
37            Error::Io(e) => Some(e),
38            _ => None,
39        }
40    }
41}
42
43impl From<std::io::Error> for Error {
44    fn from(e: std::io::Error) -> Self {
45        Error::Io(e)
46    }
47}