1pub mod recognition;
18
19use crate::{Contract, Event, Request};
20
21pub struct Issues;
22
23impl Contract for Issues {
24 const NAME: &'static str = "issues";
25 const VERSION: u32 = 1;
26}
27
28pub const MAX_RESOLVE: usize = 50;
30
31#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
33pub struct Changed;
34
35impl Event for Changed {
36 type Contract = Issues;
37 const NAME: &'static str = "changed";
38}
39
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
44pub struct CurrentScope;
45
46impl Request for CurrentScope {
47 type Contract = Issues;
48 const NAME: &'static str = "current-scope";
49 type Response = Option<IssueScope>;
50}
51
52#[derive(Clone, PartialEq, Eq)]
55#[non_exhaustive]
56pub struct IssueScope {
57 pub profile_id: String,
58 pub profile_name: String,
59 pub app_origin: String,
60 pub organisation_id: String,
61 pub team_keys: Vec<String>,
64 pub linear_workspace: Option<String>,
67}
68
69impl IssueScope {
70 pub fn new(
71 profile_id: &str,
72 profile_name: &str,
73 app_origin: &str,
74 organisation_id: &str,
75 ) -> Self {
76 Self {
77 profile_id: profile_id.into(),
78 profile_name: profile_name.into(),
79 app_origin: app_origin.into(),
80 organisation_id: organisation_id.into(),
81 team_keys: Vec::new(),
82 linear_workspace: None,
83 }
84 }
85
86 pub fn with_linear_workspace(mut self, workspace: &str) -> Self {
88 self.linear_workspace = (!workspace.is_empty()).then(|| workspace.to_owned());
89 self
90 }
91
92 pub fn with_team_keys(mut self, keys: impl IntoIterator<Item = impl Into<String>>) -> Self {
93 self.team_keys = keys.into_iter().map(Into::into).collect();
94 self
95 }
96
97 pub fn navigation_url(&self, url: &str) -> Option<String> {
100 let mut url = url::Url::parse(url).ok()?;
101 if url.origin().ascii_serialization() != self.app_origin {
102 return None;
103 }
104 url.query_pairs_mut()
105 .clear()
106 .append_pair("orgId", &self.organisation_id)
107 .append_pair("profile", &self.profile_id);
108 Some(url.into())
109 }
110}
111
112#[derive(Clone, PartialEq, Eq)]
117pub struct Resolve {
118 profile_id: String,
119 identifiers: Vec<String>,
120}
121
122impl Resolve {
123 pub fn new(profile_id: &str, identifiers: impl IntoIterator<Item = impl Into<String>>) -> Self {
125 let mut distinct: Vec<String> = Vec::new();
126 for identifier in identifiers.into_iter().map(Into::into) {
127 if distinct.len() == MAX_RESOLVE {
128 break;
129 }
130 if !distinct.contains(&identifier) {
131 distinct.push(identifier);
132 }
133 }
134 Self {
135 profile_id: profile_id.into(),
136 identifiers: distinct,
137 }
138 }
139
140 pub fn profile_id(&self) -> &str {
142 &self.profile_id
143 }
144
145 pub fn identifiers(&self) -> &[String] {
146 &self.identifiers
147 }
148}
149
150impl Request for Resolve {
151 type Contract = Issues;
152 const NAME: &'static str = "resolve";
153 type Response = Vec<Resolution>;
154}
155
156#[derive(Clone, PartialEq, Eq)]
158#[non_exhaustive]
159pub struct Resolution {
160 pub identifier: String,
161 pub state: ResolutionState,
162}
163
164impl Resolution {
165 pub fn new(identifier: &str, state: ResolutionState) -> Self {
166 Self {
167 identifier: identifier.into(),
168 state,
169 }
170 }
171}
172
173#[derive(Clone, PartialEq, Eq)]
174#[non_exhaustive]
175pub enum ResolutionState {
176 Available(IssueSummary),
177 Unavailable,
180 Pending,
182 SignedOut,
183 Failed,
185}
186
187#[derive(Clone, Copy, Debug, PartialEq, Eq)]
189#[non_exhaustive]
190pub enum StateType {
191 Triage,
192 Backlog,
193 Unstarted,
194 Started,
195 Completed,
196 Canceled,
197 Duplicate,
198}
199
200#[derive(Clone, PartialEq, Eq)]
203#[non_exhaustive]
204pub struct IssueState {
205 pub name: String,
206 pub kind: StateType,
207}
208
209impl IssueState {
210 pub fn new(name: &str, kind: StateType) -> Self {
211 Self {
212 name: name.into(),
213 kind,
214 }
215 }
216}
217
218#[derive(Clone, PartialEq, Eq)]
220#[non_exhaustive]
221pub struct IssueSummary {
222 pub identifier: String,
223 pub title: String,
224 pub url: String,
226 pub state: IssueState,
227 pub tracker: bool,
228 pub archived: bool,
229 pub assignee: Option<String>,
231}
232
233impl IssueSummary {
234 pub fn new(identifier: &str, title: &str, url: &str, state: IssueState) -> Self {
235 Self {
236 identifier: identifier.into(),
237 title: title.into(),
238 url: url.into(),
239 state,
240 tracker: false,
241 archived: false,
242 assignee: None,
243 }
244 }
245
246 pub fn with_markers(mut self, tracker: bool, archived: bool) -> Self {
248 self.tracker = tracker;
249 self.archived = archived;
250 self
251 }
252
253 pub fn with_assignee(mut self, assignee: Option<&str>) -> Self {
254 self.assignee = assignee.map(str::to_owned);
255 self
256 }
257}