summaryrefslogtreecommitdiff
path: root/src/data/ipaddress_history.rs
blob: 2787c3691ed00ad5e83dce35d1376990f11f4363 (plain)
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
use just_enough_vcs::vcs::current::current_cfg_dir;

const IP_HISTORY_NAME: &str = "ip_history.txt";

pub struct IpAddressHistory {
    pub recent_ip_address: Vec<String>,
}

pub async fn get_recent_ip_address() -> Vec<String> {
    if let Some(local) = current_cfg_dir() {
        let path = local.join(IP_HISTORY_NAME);
        match tokio::fs::read_to_string(path).await {
            Ok(content) => content.lines().map(String::from).collect(),
            Err(_) => Vec::new(),
        }
    } else {
        Vec::new()
    }
}

pub async fn insert_recent_ip_address(ip: impl Into<String>) {
    let ip = ip.into();
    if let Some(local) = current_cfg_dir() {
        let path = local.join(IP_HISTORY_NAME);
        let mut recent_ips = get_recent_ip_address().await;
        recent_ips.retain(|existing_ip| existing_ip != &ip);
        recent_ips.insert(0, ip);
        if recent_ips.len() > 8 {
            recent_ips.truncate(8);
        }
        let content = recent_ips.join("\n");
        let _ = tokio::fs::write(path, content).await;
    }
}