From 119cd0fe4674b45bc2c7789f09207e8448b693f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 23 Oct 2021 12:57:00 +0200 Subject: [PATCH] Improve common AccessList code --- aquatic_common/src/access_list.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/aquatic_common/src/access_list.rs b/aquatic_common/src/access_list.rs index 28e24e3..06b5dd4 100644 --- a/aquatic_common/src/access_list.rs +++ b/aquatic_common/src/access_list.rs @@ -51,6 +51,20 @@ impl AccessList { Ok(()) } + + pub fn create_from_path(path: &PathBuf) -> anyhow::Result { + let file = File::open(path)?; + let reader = BufReader::new(file); + + let mut new_list = Self::default(); + + for line in reader.lines() { + new_list.insert_from_line(&line?)?; + } + + Ok(new_list) + } + pub fn allows(&self, mode: AccessListMode, info_hash: &[u8; 20]) -> bool { match mode { AccessListMode::White => self.0.contains(info_hash), @@ -69,16 +83,7 @@ pub type AccessListArcSwap = ArcSwap; impl AccessListQuery for AccessListArcSwap { fn update_from_path(&self, path: &PathBuf) -> anyhow::Result<()> { - let file = File::open(path)?; - let reader = BufReader::new(file); - - let mut new_list = HashSet::new(); - - for line in reader.lines() { - new_list.insert(parse_info_hash(&line?)?); - } - - self.store(Arc::new(AccessList(new_list))); + self.store(Arc::new(AccessList::create_from_path(path)?)); Ok(()) }