Skip to main content

gitpoulpi_hosting/
metadata.rs

1//! Assignees, reviewers, labels and milestones: the host-agnostic shapes
2//! the pickers (TUI), `pr edit` / `issue edit` (CLI/MCP) and the create
3//! forms speak. GitHub and GitLab fill them (`github_meta`, `gitlab_meta`);
4//! every other provider keeps the `Unsupported` defaults of the trait and
5//! nothing about it shows in a UI.
6
7use serde::{Deserialize, Serialize};
8
9/// A label of a project.
10#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
11pub struct HostedLabel {
12    pub name: String,
13    /// Hex colour without the `#` (`d73a4a`); empty when the host sent none.
14    #[serde(default)]
15    pub color: String,
16    #[serde(default)]
17    pub description: String,
18}
19
20/// A milestone of a project (open ones are offered).
21#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
22pub struct HostedMilestone {
23    /// What an edit sends: the milestone *number* on GitHub, GitLab's
24    /// project-wide milestone `id`.
25    pub id: u64,
26    pub title: String,
27    /// The due date as the host sent it (`2026-11-01`), when it has one.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub due_on: Option<String>,
30    /// `open` / `closed` / `active`, as the host spells it.
31    #[serde(default)]
32    pub state: String,
33}
34
35/// Whether a metadata call is about an issue or a pull / merge request.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum MetaKind {
39    Issue,
40    PullRequest,
41}
42
43/// What an issue or request carries right now. Logins for people, names
44/// for labels. `reviewers` stays empty for an issue.
45#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
46pub struct ItemMeta {
47    #[serde(default)]
48    pub assignees: Vec<String>,
49    #[serde(default)]
50    pub reviewers: Vec<String>,
51    #[serde(default)]
52    pub labels: Vec<String>,
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub milestone: Option<HostedMilestone>,
55}
56
57/// Replacements for an issue's metadata. `None` leaves a field alone;
58/// `Some(vec![])` clears it. `milestone: Some(None)` clears the milestone.
59#[derive(Debug, Clone, Default, PartialEq, Eq)]
60pub struct IssueEdit {
61    pub assignees: Option<Vec<String>>,
62    pub labels: Option<Vec<String>>,
63    pub milestone: Option<Option<u64>>,
64}
65
66impl IssueEdit {
67    pub fn is_empty(&self) -> bool {
68        self.assignees.is_none() && self.labels.is_none() && self.milestone.is_none()
69    }
70}
71
72/// Replacements for a pull / merge request's metadata (see [`IssueEdit`]).
73#[derive(Debug, Clone, Default, PartialEq, Eq)]
74pub struct PrEdit {
75    pub assignees: Option<Vec<String>>,
76    pub reviewers: Option<Vec<String>>,
77    pub labels: Option<Vec<String>>,
78    pub milestone: Option<Option<u64>>,
79}
80
81impl PrEdit {
82    pub fn is_empty(&self) -> bool {
83        self.assignees.is_none()
84            && self.reviewers.is_none()
85            && self.labels.is_none()
86            && self.milestone.is_none()
87    }
88}
89
90/// What to add and what to remove to turn the list `have` into `wanted`
91/// (logins compare case-insensitively).
92pub fn list_delta(have: &[String], wanted: &[String]) -> (Vec<String>, Vec<String>) {
93    let has = |list: &[String], x: &str| list.iter().any(|y| y.eq_ignore_ascii_case(x));
94    let add = wanted
95        .iter()
96        .filter(|w| !has(have, w))
97        .cloned()
98        .collect::<Vec<_>>();
99    let remove = have
100        .iter()
101        .filter(|h| !has(wanted, h))
102        .cloned()
103        .collect::<Vec<_>>();
104    (add, remove)
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn the_delta_of_two_lists_is_what_to_add_and_remove() {
113        let have = vec!["a".to_string(), "B".to_string()];
114        let wanted = vec!["b".to_string(), "c".to_string()];
115        let (add, remove) = list_delta(&have, &wanted);
116        assert_eq!(add, vec!["c"]);
117        assert_eq!(remove, vec!["a"]);
118    }
119
120    #[test]
121    fn an_empty_edit_says_so() {
122        assert!(IssueEdit::default().is_empty());
123        assert!(PrEdit::default().is_empty());
124        assert!(!PrEdit {
125            milestone: Some(None),
126            ..PrEdit::default()
127        }
128        .is_empty());
129    }
130}