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
use crate::get_api_key;
use crate::omdb::test_api_key;
use crate::Result;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
pub struct OnDiskConfig {
pub api_key: String,
}
impl OnDiskConfig {
pub fn new_from_prompt(client: &Client) -> Result<Self> {
let api_key = get_api_key(client)?;
Ok(OnDiskConfig { api_key })
}
pub fn save(&self) -> std::io::Result<()> {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(OnDiskConfig::config_path())?;
let bytes = serde_json::to_vec(&self)?;
file.write_all(&bytes)
}
pub fn load() -> std::io::Result<Self> {
let bytes = fs::read(OnDiskConfig::config_path())?;
let config = serde_json::from_slice(&bytes)?;
Ok(config)
}
pub fn check(&self, client: &Client) -> std::result::Result<(), String> {
test_api_key(&self.api_key, client)
}
pub fn validate(&mut self, client: &Client) -> Result<()> {
if let Err(why) = self.check(client) {
eprintln!("{}", why);
self.api_key = get_api_key(client)?;
}
Ok(())
}
fn config_path() -> PathBuf {
let mut config_path = dirs::config_dir().expect("Platform unsupported by dirs");
config_path.push("imdb-id.json");
config_path
}
}