From 949c6459aa7683453a7160423b689ceadb08954b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 4 Sep 2024 08:04:48 +0100 Subject: refactor: organise into lib and bin structure the make the code more readable this commit just moves the files, the next commit should fix the imports --- src/lib/repo_state.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/lib/repo_state.rs (limited to 'src/lib/repo_state.rs') diff --git a/src/lib/repo_state.rs b/src/lib/repo_state.rs new file mode 100644 index 0000000..a5cebab --- /dev/null +++ b/src/lib/repo_state.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; + +use anyhow::{Context, Result}; +use git2::Oid; + +pub struct RepoState { + pub identifier: String, + pub state: HashMap, + pub event: nostr::Event, +} + +impl RepoState { + pub fn try_from(mut state_events: Vec) -> Result { + state_events.sort_by_key(|e| e.created_at); + let event = state_events.first().context("no state events")?; + let mut state = HashMap::new(); + for tag in &event.tags { + if let Some(name) = tag.as_vec().first() { + if ["refs/heads/", "refs/tags", "HEAD"] + .iter() + .any(|s| name.starts_with(*s)) + { + if let Some(value) = tag.as_vec().get(1) { + if Oid::from_str(value).is_ok() || value.contains("ref: refs/") { + state.insert(name.to_owned(), value.to_owned()); + } + } + } + } + } + Ok(RepoState { + identifier: event + .identifier() + .context("existing event must have an identifier")? + .to_string(), + state, + event: event.clone(), + }) + } +} -- cgit v1.2.3