diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-30 14:14:59 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-30 15:24:29 +0100 |
| commit | 0848d57c7654d09c3041022c231dfbbbfac17600 (patch) | |
| tree | 7e1ee05dbc74ec5bbb2ee7a633e211c4fea9f8e8 /src/repo_state.rs | |
| parent | 6f64c35e57789d5731a51b46eb98a46803f072f4 (diff) | |
refactor: add struct `RepoState`
to more easily access state details
Diffstat (limited to 'src/repo_state.rs')
| -rw-r--r-- | src/repo_state.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/repo_state.rs b/src/repo_state.rs new file mode 100644 index 0000000..33bc90f --- /dev/null +++ b/src/repo_state.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use git2::Oid; | ||
| 3 | |||
| 4 | pub struct RepoState { | ||
| 5 | pub state: Vec<(String, String)>, | ||
| 6 | pub event: nostr::Event, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl RepoState { | ||
| 10 | pub fn try_from(mut state_events: Vec<nostr::Event>) -> Result<Self> { | ||
| 11 | state_events.sort_by_key(|e| e.created_at); | ||
| 12 | let event = state_events.first().context("no state events")?; | ||
| 13 | let mut state = vec![]; | ||
| 14 | for tag in &event.tags { | ||
| 15 | if let Some(name) = tag.as_vec().first() { | ||
| 16 | if ["refs/heads/", "refs/tags", "HEAD"] | ||
| 17 | .iter() | ||
| 18 | .any(|s| name.starts_with(*s)) | ||
| 19 | { | ||
| 20 | if let Some(value) = tag.as_vec().get(1) { | ||
| 21 | if Oid::from_str(value).is_ok() { | ||
| 22 | state.push((name.to_owned(), value.to_owned())); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | Ok(RepoState { | ||
| 29 | state, | ||
| 30 | event: event.clone(), | ||
| 31 | }) | ||
| 32 | } | ||
| 33 | } | ||