upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib/repo_state.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-09-04 08:04:48 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-09-04 13:30:59 +0100
commit949c6459aa7683453a7160423b689ceadb08954b (patch)
tree230c26ecb11b99916e5570e548673eb09ecf0a36 /src/lib/repo_state.rs
parenta825311f2c55661aaab3a163bda9109295c96044 (diff)
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
Diffstat (limited to 'src/lib/repo_state.rs')
-rw-r--r--src/lib/repo_state.rs40
1 files changed, 40 insertions, 0 deletions
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 @@
1use std::collections::HashMap;
2
3use anyhow::{Context, Result};
4use git2::Oid;
5
6pub struct RepoState {
7 pub identifier: String,
8 pub state: HashMap<String, String>,
9 pub event: nostr::Event,
10}
11
12impl RepoState {
13 pub fn try_from(mut state_events: Vec<nostr::Event>) -> Result<Self> {
14 state_events.sort_by_key(|e| e.created_at);
15 let event = state_events.first().context("no state events")?;
16 let mut state = HashMap::new();
17 for tag in &event.tags {
18 if let Some(name) = tag.as_vec().first() {
19 if ["refs/heads/", "refs/tags", "HEAD"]
20 .iter()
21 .any(|s| name.starts_with(*s))
22 {
23 if let Some(value) = tag.as_vec().get(1) {
24 if Oid::from_str(value).is_ok() || value.contains("ref: refs/") {
25 state.insert(name.to_owned(), value.to_owned());
26 }
27 }
28 }
29 }
30 }
31 Ok(RepoState {
32 identifier: event
33 .identifier()
34 .context("existing event must have an identifier")?
35 .to_string(),
36 state,
37 event: event.clone(),
38 })
39 }
40}