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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::{
    env,
    fs::File,
    io::{Read, Write},
};

use anyhow::{anyhow, Context, Result};

pub mod api_usage;
pub mod cli;
pub mod user_setup;

#[cfg(test)]
mod testing;

mod program_info;
mod types;

pub mod constants {
    /// JSON file name for an API key.
    pub const API_JSON_NAME: &str = "api";

    /// JSON file name for user setting.
    pub const USER_SETTING_JSON_NAME: &str = "setting";

    /// ## Current weather data
    ///
    /// Access current weather data for any location on Earth!
    /// We collect and process weather data from different sources such as
    /// global and local weather models, satellites, radars and a vast network
    /// of weather stations. Data is available in JSON, XML, or HTML format.
    /// API Documentation: [https://openweathermap.org/api/current](https://openweathermap.org/api/current)
    ///
    /// - `{lat_value}`: Latitude value of the location.
    /// - `{lon_value}`: Longitude value of the location.
    /// - `{api_key}`: OpenWeatherMap API key.
    /// - `{unit}`: The desired measurement unit.
    ///   - ex. `standard`, `metric`, `imperial`
    ///   - MORE INFO: [https://openweathermap.org/weather-data](https://openweathermap.org/weather-data)
    ///
    /// ### Example Usage
    /// ```
    /// # use weather_cli::constants::WEATHER_API_URL;
    /// let url = WEATHER_API_URL
    ///     .replace("{LAT_VALUE}", "37.3361663")
    ///     .replace("{LON_VALUE}", "-121.890591")
    ///     .replace("{API_KEY}", "EXAMPLE_KEY")
    ///     .replace("{UNIT}", "imperial");
    ///
    /// assert_eq!(url, "https://api.openweathermap.org/data/2.5/weather?lat=37.3361663&lon=-121.890591&appid=EXAMPLE_KEY&units=imperial");
    /// ```
    pub const WEATHER_API_URL: &str = "https://api.openweathermap.org/data/2.5/weather?lat={LAT_VALUE}&lon={LON_VALUE}&appid={API_KEY}&units={UNIT}";

    /// ## Geocoding API
    ///
    /// Geocoding API is a simple tool that we have developed to ease
    /// the search for locations while working with geographic names and coordinates.
    /// API Documentation: [https://openweathermap.org/api/geocoding-api](https://openweathermap.org/api/geocoding-api)
    ///
    /// - `{lat_value}`: Latitude value of the location.
    /// - `{lon_value}`: Longitude value of the location.
    /// - `{api_key}`: OpenWeatherMap API key.
    /// - `{unit}`: The desired measurement unit.
    ///   - ex. `standard`, `metric`, `imperial`
    ///   - MORE INFO: [https://openweathermap.org/weather-data](https://openweathermap.org/weather-data)
    ///
    /// ### Example Usage
    /// ```
    /// # use weather_cli::constants::WEATHER_API_URL;
    /// let url = WEATHER_API_URL
    ///     .replace("{LAT_VALUE}", "37.3361663")
    ///     .replace("{LON_VALUE}", "-121.890591")
    ///     .replace("{API_KEY}", "EXAMPLE_KEY")
    ///     .replace("{UNIT}", "imperial");
    ///
    /// assert_eq!(url, "https://api.openweathermap.org/data/2.5/weather?lat=37.3361663&lon=-121.890591&appid=EXAMPLE_KEY&units=imperial");
    /// ```
    pub const GEOLOCATION_API_URL: &str =
        "http://api.openweathermap.org/geo/1.0/direct?q={QUERY}&limit=10&appid={API_KEY}";
}

/// Returns executable directory.
pub fn get_executable_directory() -> Result<String> {
    let executable_path =
        env::current_exe().context("Failed to get the executable file directory!")?;
    let executable_directory = executable_path
        .parent()
        .context("Failed to get the executable directory!")?;

    if let Some(dir_str) = executable_directory.to_str() {
        return Ok(dir_str.to_string());
    }

    Err(anyhow!("Unable to get the executable directory."))
}

/// Returns `std::fs::File` type value of a JSON file.
pub fn get_json_file(json_suffix: &str) -> Result<File> {
    let executable_dir = get_executable_directory()?;

    let file = match File::open(format!(
        "{}/{}",
        executable_dir,
        make_json_file_name(json_suffix)
    )) {
        Ok(f) => f,
        Err(_) => {
            let mut new_file = File::create(format!(
                "{}/{}",
                executable_dir,
                make_json_file_name(json_suffix)
            ))
            .context("Failed to create a json file.")?;
            new_file
                .write_all("{}".as_bytes())
                .context("Failed to create a json file.")?;

            File::open(format!(
                "{}/{}",
                executable_dir,
                make_json_file_name(json_suffix)
            ))
            .context("Failed to get the json file.")?
        }
    };

    Ok(file)
}

/// Complete a JSON file name.
/// ## Example
/// ```
/// # use weather_cli::make_json_file_name;
/// assert_eq!(make_json_file_name("api"), "weather-cli-api.json");
/// ```
pub fn make_json_file_name(suffix: &str) -> String {
    format!("weather-cli-{}.json", suffix)
}

pub enum ErrorMessageType {
    SettingRead,
    ApiResponseRead,
    InvalidApiKey,
}

fn get_file_read_error_message(error_type: ErrorMessageType, context: Option<&str>) -> String {
    match (error_type, context) {
        (ErrorMessageType::SettingRead, Some(context)) => {
            if context == "api" {
                format!(
                    "Failed to read {}. Please make sure to setup your API key.",
                    make_json_file_name(context)
                )
            } else {
                format!("Failed to read the following file: {}", context)
            }
        }
        (ErrorMessageType::ApiResponseRead, Some(context)) => {
            format!("The given '{}' JSON input may be invalid.", context)
        }
        (ErrorMessageType::InvalidApiKey, None) => {
            "API Key is invalid. Please try again.".to_string()
        }
        _ => unreachable!(),
    }
}

/// Read a JSON file and return the string.
pub fn read_json_file<T: serde::de::DeserializeOwned>(json_name: &str) -> Result<T> {
    let mut file = get_json_file(json_name)?;
    let mut json_string = String::new();
    file.read_to_string(&mut json_string)?;

    let api_key_data: T = serde_json::from_str(&json_string).context(
        get_file_read_error_message(ErrorMessageType::SettingRead, Some(json_name)),
    )?; // ERROR

    Ok(api_key_data)
}

/// Reads a JSON file and returns serialized data.
pub fn read_json_response<T: serde::de::DeserializeOwned>(
    response: &str,
    error_message_type: ErrorMessageType,
    error_context: &str,
) -> Result<T> {
    use serde_json::Value;

    let api_response: Value = serde_json::from_str(response).context(
        get_file_read_error_message(ErrorMessageType::ApiResponseRead, Some(error_context)),
    )?;

    // Invalid API key error.
    if let Some(401) = api_response["cod"].as_i64() {
        return Err(anyhow!(get_file_read_error_message(
            ErrorMessageType::InvalidApiKey,
            None
        )));
    }

    let response_data: T = serde_json::from_str(response).context(get_file_read_error_message(
        error_message_type,
        Some(error_context),
    ))?;

    Ok(response_data)
}

/// URL placeholder information.
///
/// ## Example Usage
/// ```no_run
/// # use weather_cli::URLPlaceholder;
/// URLPlaceholder {
///     placeholder: "{LAT_VALUE}".to_string(),
///     value: "37.3361663".to_string(),
/// };
/// ```
pub struct URLPlaceholder {
    pub placeholder: String,
    pub value: String,
}

/// Replaces URL placeholders with given values.
pub fn replace_url_placeholders(url: &str, url_placeholders: &[URLPlaceholder]) -> String {
    let mut replaced_url = String::from(url);
    for url_placeholder in url_placeholders {
        replaced_url = replaced_url.replace(&url_placeholder.placeholder, &url_placeholder.value);
    }

    replaced_url
}