forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

marks.rs53 lines, 1.2 KiBHistoryRawDownload
1use std::collections::HashSet;
2
3use common::model::Id;
4
5use crate::{read_config, write_config};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Mark {
9 Mute,
10 Pin,
11}
12
13impl Mark {
14 fn file(self) -> &'static str {
15 match self {
16 Mark::Mute => "mutes",
17 Mark::Pin => "pins",
18 }
19 }
20}
21
22fn prefix(account_user_id: Id) -> String {
23 format!("{account_user_id}\t")
24}
25
26pub fn load(mark: Mark, account_user_id: Id) -> HashSet<Id> {
27 let Some(content) = read_config(mark.file()) else {
28 return HashSet::new();
29 };
30 let prefix = prefix(account_user_id);
31
32 content
33 .lines()
34 .filter_map(|line| line.strip_prefix(&prefix))
35 .filter_map(|community_id| community_id.parse().ok())
36 .collect()
37}
38
39pub fn save(mark: Mark, account_user_id: Id, ids: &HashSet<Id>) {
40 let prefix = prefix(account_user_id);
41 let mut content: String = read_config(mark.file())
42 .unwrap_or_default()
43 .lines()
44 .filter(|line| !line.starts_with(&prefix))
45 .map(|line| format!("{line}\n"))
46 .collect();
47 content.extend(
48 ids.iter()
49 .map(|community_id| format!("{prefix}{community_id}\n")),
50 );
51
52 write_config(mark.file(), &content);
53}