diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:19:28 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:19:28 +0000 |
| commit | e98276e79cea0c3e474ca0251c276c474c35ed70 (patch) | |
| tree | ef5149bba1c4933b86f92c7ef79368faa873de3f /src/groups/groups.rs | |
| parent | fda0fdd81caab1ca92eb7ed601058e6c2fdc59f5 (diff) | |
groups
Diffstat (limited to 'src/groups/groups.rs')
| -rw-r--r-- | src/groups/groups.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/groups/groups.rs b/src/groups/groups.rs new file mode 100644 index 0000000..3b837a7 --- /dev/null +++ b/src/groups/groups.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | use std::fs; | ||
| 2 | |||
| 3 | use nostr::EventId; | ||
| 4 | |||
| 5 | use crate::{utils::load_file}; | ||
| 6 | |||
| 7 | use super::group::Group; | ||
| 8 | |||
| 9 | pub struct Groups { | ||
| 10 | groups:Vec<Group> | ||
| 11 | } | ||
| 12 | impl Groups { | ||
| 13 | pub fn new() -> Self { | ||
| 14 | |||
| 15 | let cur_dir = std::env::current_dir().unwrap(); | ||
| 16 | |||
| 17 | // check for potential problems | ||
| 18 | let ngit_path = cur_dir.clone().join(".ngit"); | ||
| 19 | if !ngit_path.is_dir() { | ||
| 20 | panic!("ngit not initialised. Run 'ngit init' first..."); | ||
| 21 | } | ||
| 22 | |||
| 23 | let mut groups = vec![]; | ||
| 24 | |||
| 25 | for dir_entry in fs::read_dir(ngit_path.join("groups")) | ||
| 26 | .expect("groups folder to exist and read_dir to read it") | ||
| 27 | { | ||
| 28 | groups.push( | ||
| 29 | Group::new_from_json_event( | ||
| 30 | load_file( | ||
| 31 | dir_entry | ||
| 32 | .expect("DirEntry in read_dir should exist").path() | ||
| 33 | ) | ||
| 34 | .expect("group json to load from file") | ||
| 35 | ).expect("group json to produce Group") | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | Self { | ||
| 40 | groups, | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | pub fn by_event_id(&self, id:&EventId) -> Option<&Group> { | ||
| 45 | self.groups.iter().find(|g| g.id == *id) | ||
| 46 | } | ||
| 47 | } \ No newline at end of file | ||