Skip to main content

onetaskgraph_plugin_api/
work.rs

1//! The work items every source is normalised into.
2
3use chrono::{DateTime, Utc};
4use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::collections::BTreeMap;
8
9use crate::{NativeId, SourceName};
10
11/// One unit of work as a source reports it.
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
13pub struct Task {
14    /// The source's own opaque identifier.
15    pub id: NativeId,
16    /// The short handle the backend shows people, beside [`id`](Self::id) and never
17    /// instead of it — a Linear issue's `ENG-123`, a GitHub issue's `1043`.
18    ///
19    /// **It is human-facing and it may change.** A Linear issue moved between teams gets a
20    /// new identifier and a GitHub issue transferred between repositories gets a new
21    /// number, so nothing stores this in place of [`id`](Self::id) and nothing matches on
22    /// it: `id` is what everything stores and matches on, and this is what a person says
23    /// out loud.
24    ///
25    /// Absent by default, so a source that predates this field — and every source with no
26    /// separate handle of its own — reads as `None`, which means *this backend has no
27    /// short handle for this task* rather than *the handle is the id*. A source never
28    /// copies [`id`](Self::id) here.
29    ///
30    /// **Read-only.** A source derives it on a read and never stores one it is handed: a
31    /// task arriving on an [`ItemWrite`](crate::ItemWrite) may still hold its source's key,
32    /// and a destination ignores it.
33    // llmlint: ignore[invalid_states_unrepresentable] The answer `Task::url` and `Project::url` below already record: this crate's field types are the contract itself, and the contract approving this field states its shape as `Option<String>`. A backend's handle is also the one value this product never parses, matches on or resolves by — `id` does all three — so the confusion a newtype prevents is confusion no code here can act on.
34    #[serde(default)]
35    pub key: Option<String>,
36    /// The one-line summary a user recognises the task by.
37    pub title: String,
38    /// The long-form body, when the source has one.
39    pub content: Option<String>,
40    /// The source's status, normalised and preserved.
41    pub status: Status,
42    /// The task's priority; `none` means none is set, and a source that cannot hold one
43    /// reports `none`.
44    ///
45    /// Defaulted when a document omits it, so a task written before this field existed —
46    /// and every task of a plugin that predates it — reads as [`Priority::None`]. Always
47    /// written, so a reader never has to tell an absent member from a `none` one. Whether a
48    /// source can hold one at all is [`Capabilities::priority`](crate::Capabilities::priority),
49    /// and the engine never hands a source declaring it cannot a priority other than `none`.
50    #[serde(default)]
51    pub priority: Priority,
52    /// Inline rather than by id: a source returning a task already knows them.
53    pub labels: Vec<Label>,
54    /// `None` is a first-class case — an orphan task — not an edge case.
55    pub project: Option<NativeId>,
56    /// Where a human can open this task.
57    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs), and in AGENTS.md's "The plugin contract": this crate's field types ARE the approved contract, six undispatched nodes compile against `Option<String>` here, and only the contract's owner may narrow one. No code change is available that clears this without editing that frozen surface.
58    // llmlint: ignore[boundary_inputs_validated] parsing this into a URL type would narrow the same frozen surface, and would add a URL dependency to the crate AGENTS.md says to keep still ("Keep the api crate still" — every change here re-tests every plugin). A plugin that returns a string this interface cannot represent is what `SourceError::Malformed` is for. Contract owner's call; recorded in AGENTS.md, "The plugin contract".
59    pub url: Option<String>,
60    /// Where this task is, when the source says (see [`Location`]).
61    ///
62    /// Absent by default, so a source that predates this field — and every source that
63    /// simply does not say — reads as `None`, which means *the source did not say where
64    /// this is* rather than *this is nowhere*. It neither replaces nor derives from
65    /// [`url`](Self::url), which goes on meaning exactly what it always did.
66    #[serde(default)]
67    pub location: Option<Location>,
68    /// When the source says the task was created.
69    pub created_at: Option<DateTime<Utc>>,
70    /// When the source says the task last changed.
71    pub updated_at: Option<DateTime<Utc>>,
72    /// Caller-defined attributes, preserving their JSON types.
73    ///
74    /// Keys are free-form, with two reserved prefixes: `onetaskgraph.` belongs to this
75    /// product — [`Repository::METADATA_KEY`] and [`DependencyEdge::RECORDED_KEY`] are
76    /// the two every source honours, and [`ItemKind::METADATA_KEY`] is one plugin's —
77    /// and `onepipeline.` belongs to that consumer. Every other key is the caller's, and
78    /// a source returns it exactly as it holds it.
79    #[serde(default)]
80    pub metadata: BTreeMap<String, Value>,
81    /// Normalized repository origins this task concerns, in source order and without
82    /// repeats.
83    #[serde(default, deserialize_with = "unique_repositories")]
84    pub repositories: Vec<Repository>,
85    /// The tasks this one delivers: finishing this task finishes them.
86    ///
87    /// Each entry is a [`TaskRef`] — `<source>:<native>` names a task of any source, and a
88    /// bare native id names a task of the source holding this one — with no repeats and
89    /// never this task itself. Empty by default, and left out of the wire when empty, so a
90    /// reader written before the field existed reads exactly what it read before.
91    #[serde(default, skip_serializing_if = "Vec::is_empty")]
92    #[schemars(!skip_serializing_if)]
93    pub delivers: Vec<TaskRef>,
94    /// Every task that delivers this one, by qualified id: the reverse of [`Self::delivers`].
95    ///
96    /// **Owned by the store, not by a source record and not by a copy.** The engine keeps it
97    /// in step whenever it writes a task's `delivers`, through
98    /// [`TaskSource::set_delivered_by`](crate::TaskSource::set_delivered_by); a source holds
99    /// and reports it, and a copy keeps the destination's own rather than taking the
100    /// source's. Empty by default and left out of the wire when empty, as `delivers` is.
101    #[serde(default, skip_serializing_if = "Vec::is_empty")]
102    #[schemars(!skip_serializing_if)]
103    pub delivered_by: Vec<TaskRef>,
104    /// The task's change token: an opaque value its source gives so a reader can tell whether
105    /// the task changed without reading it whole.
106    ///
107    /// **Two equal versions of one task from one source mean nothing in it has changed** — its
108    /// title, body, status, priority, labels, project fields, metadata, edges and comments,
109    /// comments added, edited and deleted included. A source that cannot promise all of that
110    /// for a task leaves this absent, which means *always read it*. What each plugin keys its
111    /// token on, and which change kinds it covers, is stated per plugin in
112    /// `docs/plugin-protocol.md` §4.13d.
113    ///
114    /// Opaque to every caller: nothing compares one but for equality, and the one reader that
115    /// looks inside is the source that gave it, through
116    /// [`TaskSource::order_versions`](crate::TaskSource::order_versions). Absent by default and left out of the wire when absent, so a source written
117    /// before it existed is read as giving none.
118    ///
119    /// **Read-only.** A source derives it on a read and never stores one it is handed: a task
120    /// arriving on an [`ItemWrite`](crate::ItemWrite) may still hold its source's token, and a
121    /// destination ignores it.
122    // llmlint: ignore[invalid_states_unrepresentable] The contract this lands states the token as an opaque string so the next node's cache and both SDKs carry it unread; a typed token would make every plugin's format part of this crate, which AGENTS.md asks to keep still. The one reader that parses it, its own source's `TaskSource::order_versions`, answers `None` for any value it cannot parse.
123    #[serde(default, skip_serializing_if = "Option::is_none")]
124    pub version: Option<String>,
125    /// Who may read this task, as it was declared (see [`Classification`]).
126    ///
127    /// Defaulted and left out of the wire while `public`, so a task written before this
128    /// field existed reads as public and a reader written before it reads what it always
129    /// read. It is the *declared* classification: the engine tightens it by the task's own
130    /// repositories — any private or unknown one makes the task private — before it decides
131    /// where the task may be written, and nothing written here can loosen that. The project
132    /// a task is filed under never tightens it: a task's visibility is its own.
133    #[serde(default, skip_serializing_if = "Classification::is_public")]
134    #[schemars(!skip_serializing_if)]
135    pub classification: Classification,
136    /// Every person the task is assigned to, as its source names a person — a GitHub login, a
137    /// Linear display name, the string as given in a folder or in memory — in the order the
138    /// source holds them; empty means unassigned.
139    ///
140    /// **Present only for a source that opted in** with `config.assignees: true`
141    /// ([`Capabilities::assignees`](crate::Capabilities::assignees) `native`): every other
142    /// source leaves it absent, and it is left out of the wire when absent, so a task of a
143    /// source that has not opted in reads, and is written, exactly as it was before the field
144    /// existed. On a write, absent leaves the item's assignees as they are, and present
145    /// replaces them with exactly the people it names — the engine never hands a source more
146    /// than one.
147    // llmlint: ignore[invalid_states_unrepresentable] A read holds every person the destination holds — a GitHub issue may carry several — so the read field is a list; the one-person write is the engine's rule, refused before any source is asked (`EngineError::SeveralAssignees`), and keeping one field for both is what lets a source compare a write's answer with what it asked for without a second shape. Absent is a distinct state from empty: a source that never opted in, versus an unassigned item of one that did.
148    #[serde(default, skip_serializing_if = "Option::is_none")]
149    pub assignees: Option<Vec<String>>,
150}
151
152/// Whether two lists of assignees name the same people, whatever their order.
153#[must_use]
154pub(crate) fn same_assignees(left: &[String], right: &[String]) -> bool {
155    let left: std::collections::BTreeSet<&str> = left.iter().map(String::as_str).collect();
156    let right: std::collections::BTreeSet<&str> = right.iter().map(String::as_str).collect();
157    left == right
158}
159
160/// A grouping of tasks, shaped like a [`Task`] without a parent of its own.
161#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
162pub struct Project {
163    /// The source's own opaque identifier.
164    pub id: NativeId,
165    /// The one-line summary a user recognises the project by.
166    pub title: String,
167    /// The long-form body, when the source has one.
168    pub content: Option<String>,
169    /// The source's status, normalised and preserved.
170    pub status: Status,
171    /// Inline rather than by id, for the same reason as on [`Task`].
172    pub labels: Vec<Label>,
173    /// Where a human can open this project.
174    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs), and in AGENTS.md's "The plugin contract": this crate's field types ARE the approved contract, six undispatched nodes compile against `Option<String>` here, and only the contract's owner may narrow one. No code change is available that clears this without editing that frozen surface.
175    // llmlint: ignore[boundary_inputs_validated] parsing this into a URL type would narrow the same frozen surface, and would add a URL dependency to the crate AGENTS.md says to keep still ("Keep the api crate still" — every change here re-tests every plugin). A plugin that returns a string this interface cannot represent is what `SourceError::Malformed` is for. Contract owner's call; recorded in AGENTS.md, "The plugin contract".
176    pub url: Option<String>,
177    /// Where this project is, on exactly the terms of [`Task::location`].
178    #[serde(default)]
179    pub location: Option<Location>,
180    /// When the source says the project was created.
181    pub created_at: Option<DateTime<Utc>>,
182    /// When the source says the project last changed.
183    pub updated_at: Option<DateTime<Utc>>,
184    /// Caller-defined attributes, preserving their JSON types, on the same terms as
185    /// [`Task::metadata`].
186    #[serde(default)]
187    pub metadata: BTreeMap<String, Value>,
188    /// Normalized repository origins this project concerns, in source order and without
189    /// repeats.
190    #[serde(default, deserialize_with = "unique_repositories")]
191    pub repositories: Vec<Repository>,
192    /// The project's change token, on exactly the terms of [`Task::version`]: two equal
193    /// versions of one project from one source mean nothing its source's
194    /// [`ProjectVersionCoverage`](crate::ProjectVersionCoverage) covers has changed, and a
195    /// source that cannot promise that leaves it absent, which means *always read it*.
196    /// Absent by default and left out of the wire when absent.
197    // llmlint: ignore[invalid_states_unrepresentable] As `Task::version`: the token is opaque to every caller, so the cache and both SDKs carry it unread, and the one reader that parses it — its own source's `TaskSource::order_versions` — answers `None` for any value it cannot parse.
198    #[serde(default, skip_serializing_if = "Option::is_none")]
199    pub version: Option<String>,
200    /// Who may read this project, as it was declared, on the terms of
201    /// [`Task::classification`]. A project holding a private task or document is private
202    /// however this reads, and the engine records it so when it writes the project.
203    #[serde(default, skip_serializing_if = "Classification::is_public")]
204    #[schemars(!skip_serializing_if)]
205    pub classification: Classification,
206}
207
208/// One piece of information that lives in a project and is not work.
209///
210/// A document carries **no status** and **no dependencies**, and both omissions are the
211/// contract rather than an oversight: a document is not work, so it has no place in a
212/// status filter and no place in a dependency graph. [`ItemKind`] therefore gains no
213/// document variant — that enum names what a dependency endpoint points at, and nothing
214/// may point at a document.
215///
216/// A source says whether it has documents at all through
217/// [`Capabilities::documents`](crate::Capabilities::documents), and one that says it has
218/// none is never asked for one.
219#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
220pub struct Document {
221    /// The source's own opaque identifier.
222    pub id: NativeId,
223    /// The one-line summary a person recognises it by.
224    pub title: String,
225    /// The long-form body, when the source has one.
226    pub content: Option<String>,
227    /// The project it lives in; `None` is an orphan document, exactly as it is on a
228    /// [`Task`].
229    pub project: Option<NativeId>,
230    /// Inline, on the same terms as a [`Task`]'s.
231    pub labels: Vec<Label>,
232    /// Where a person can open it, on the same terms as a [`Task`]'s.
233    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Task::url` and `Project::url` in this module, at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs), and in AGENTS.md's "The plugin contract": this crate's field types ARE the approved contract, this field is `Option<String>` because a task's and a project's are, and only the contract's owner may narrow one. Narrowing it here alone would leave the three entities describing the same thing in two different types.
234    // llmlint: ignore[boundary_inputs_validated] parsing this into a URL type would narrow the same frozen surface, and would add a URL dependency to the crate AGENTS.md says to keep still ("Keep the api crate still" — every change here re-tests every plugin). A plugin that returns a string this interface cannot represent is what `SourceError::Malformed` is for. Contract owner's call; recorded in AGENTS.md, "The plugin contract".
235    pub url: Option<String>,
236    /// Where it is, when the source says (see [`Location`]).
237    #[serde(default)]
238    pub location: Option<Location>,
239    /// When the source says it was created.
240    pub created_at: Option<DateTime<Utc>>,
241    /// When the source says it last changed.
242    pub updated_at: Option<DateTime<Utc>>,
243    /// Caller-defined attributes, preserving their JSON types, with the same reserved
244    /// prefixes [`Task::metadata`] carries.
245    #[serde(default)]
246    pub metadata: BTreeMap<String, Value>,
247    /// Normalized repository origins this document concerns, in source order and without
248    /// repeats, as a [`Task`]'s.
249    #[serde(default, deserialize_with = "unique_repositories")]
250    pub repositories: Vec<Repository>,
251    /// The document's change token, on exactly the terms of [`Task::version`] and of what its
252    /// source's [`DocumentVersionCoverage`](crate::DocumentVersionCoverage) covers. Absent by
253    /// default and left out of the wire when absent.
254    // llmlint: ignore[invalid_states_unrepresentable] As `Task::version`: the token is opaque to every caller, so the cache and both SDKs carry it unread, and the one reader that parses it — its own source's `TaskSource::order_versions` — answers `None` for any value it cannot parse.
255    #[serde(default, skip_serializing_if = "Option::is_none")]
256    pub version: Option<String>,
257    /// Who may read this document, as it was declared, on the terms of
258    /// [`Task::classification`] — except that a document filed under a private project is
259    /// private, however this reads.
260    #[serde(default, skip_serializing_if = "Classification::is_public")]
261    #[schemars(!skip_serializing_if)]
262    pub classification: Classification,
263}
264
265/// Who may read an item: anybody, or only those who may read where it is kept.
266///
267/// Two values, ordered so the stricter is the greater: [`strictest`](Self::strictest) of
268/// any two is the one an item they both describe carries. Nothing combines them any other
269/// way, which is what makes an explicit `public` unable to loosen a private repository, or
270/// a document's private project — a classification only ever tightens. A task's project
271/// never tightens the task.
272///
273/// It is recorded on the item and travels with it. A source whose backend has no notion of
274/// its own records it under [`Self::METADATA_KEY`], exactly as it records
275/// [`Repository::METADATA_KEY`], and only while it is `private`: an item without it is
276/// public, so every item written before this existed reads as it did.
277#[derive(
278    Debug,
279    Clone,
280    Copy,
281    Default,
282    PartialEq,
283    Eq,
284    PartialOrd,
285    Ord,
286    Hash,
287    Serialize,
288    Deserialize,
289    JsonSchema,
290)]
291#[serde(rename_all = "kebab-case")]
292pub enum Classification {
293    /// Anybody who may read the destination may read it.
294    #[default]
295    Public,
296    /// It may be written only to a destination verified private, and nothing of it may
297    /// reach a public one.
298    Private,
299}
300
301impl Classification {
302    /// The reserved metadata key a source records a private classification under when its
303    /// backend has no notion of its own.
304    ///
305    /// Spelled once, here, for the reason [`Repository::METADATA_KEY`] is.
306    pub const METADATA_KEY: &'static str = "onetaskgraph.classification";
307
308    /// Whether this is [`Public`](Self::Public) — the value left out of the wire.
309    #[must_use]
310    // serde's `skip_serializing_if` calls its predicate with a reference to the field, so this
311    // takes `&self` although the type is `Copy`.
312    #[allow(clippy::trivially_copy_pass_by_ref)]
313    pub fn is_public(&self) -> bool {
314        *self == Self::Public
315    }
316
317    /// The stricter of the two.
318    #[must_use]
319    pub fn strictest(self, other: Self) -> Self {
320        self.max(other)
321    }
322
323    /// The value as the wire spells it: `public` or `private`.
324    #[must_use]
325    pub const fn as_str(self) -> &'static str {
326        match self {
327            Self::Public => "public",
328            Self::Private => "private",
329        }
330    }
331
332    /// The classification a source records under [`Self::METADATA_KEY`], or `public` when
333    /// it records none.
334    ///
335    /// # Errors
336    ///
337    /// Returns a message when the key holds anything but one of the two spellings.
338    pub fn from_metadata(metadata: &BTreeMap<String, Value>) -> Result<Self, String> {
339        match metadata.get(Self::METADATA_KEY) {
340            None => Ok(Self::Public),
341            Some(value) => serde_json::from_value(value.clone()).map_err(|_| {
342                format!(
343                    "{} is {value}; it accepts only \"public\" or \"private\"",
344                    Self::METADATA_KEY
345                )
346            }),
347        }
348    }
349
350    /// Record this classification in `metadata` under [`Self::METADATA_KEY`]: present while
351    /// private, absent while public.
352    pub fn record(self, metadata: &mut BTreeMap<String, Value>) {
353        if self.is_public() {
354            metadata.remove(Self::METADATA_KEY);
355        } else {
356            metadata.insert(Self::METADATA_KEY.to_owned(), Value::from(self.as_str()));
357        }
358    }
359}
360
361impl std::fmt::Display for Classification {
362    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
363        formatter.write_str(self.as_str())
364    }
365}
366
367impl std::str::FromStr for Classification {
368    type Err = String;
369
370    fn from_str(value: &str) -> Result<Self, Self::Err> {
371        match value {
372            "public" => Ok(Self::Public),
373            "private" => Ok(Self::Private),
374            _ => Err(format!(
375                "{value:?} is not a classification; a classification is public or private"
376            )),
377        }
378    }
379}
380
381/// Where an entity is, in the one form a consumer can act on without knowing the backend.
382///
383/// Externally tagged with exactly two variants, so the JSON is `{"url": "https://…"}` or
384/// `{"path": "/home/…"}` and a consumer tells them apart by which key is present. A reader
385/// handed one of these knows what to *do* with it — open a link, or print a path and read
386/// the file out — which is what a bare string could not have said.
387///
388/// It carries no third case on purpose. `None` on the field is the third case, and it
389/// means the source did not say where the entity is, which is not the same as saying it is
390/// nowhere.
391///
392/// This does **not** redefine, replace or derive from the `url` field of [`Task`],
393/// [`Project`] or [`Document`]: a source that reports a web URL there goes on reporting
394/// it, and every existing consumer sees exactly what it saw.
395#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
396#[serde(rename_all = "kebab-case")]
397pub enum Location {
398    /// The entity lives at an external website, and this is a link a reader can open.
399    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Task::url` and `Project::url` in this module, at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs): this crate's field types ARE the approved contract, and this variant is a `String` because the `url` field it sits beside is one. Narrowing it here alone would leave two members describing a web address in two different types, which is worse than the state it would remove.
400    // llmlint: ignore[boundary_inputs_validated] parsing this into a URL type would add a URL dependency to the crate AGENTS.md says to keep still ("Keep the api crate still" — every change here rebuilds and re-tests every plugin), and would narrow a frozen surface only the contract's owner may narrow. A plugin returning a string this interface cannot represent is what `SourceError::Malformed` is for, exactly as it is for `Task::url`.
401    Url(String),
402    /// The entity is a file on the machine the source runs on, and this is that file's
403    /// absolute path, so a reader can print the path or read the contents out.
404    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — the reason the variant above carries, plus one of this variant's own: a typed path here would be `std::path::PathBuf`, whose parsing is the *reading* platform's while this string is the *source's*. A plugin on Linux reporting an absolute path to an engine on Windows must have that path survive byte for byte, so the type that would make a relative path unrepresentable is the type that would corrupt a correct one.
405    // llmlint: ignore[boundary_inputs_validated] validating absoluteness here would answer the question with the wrong machine's rules, for the reason above — this side cannot know what "absolute" means on the host the plugin runs on. The absoluteness this documents is an obligation on the source, and a source that breaks it is `SourceError::Malformed` to the reader that acts on the path.
406    Path(String),
407}
408
409/// A repository identified by its normalized origin, without a URL scheme or `.git` suffix.
410#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
411#[serde(try_from = "String", into = "String")]
412pub struct Repository(String);
413
414impl Repository {
415    /// The reserved metadata key a source reads these origins from when its backend has
416    /// no notion of its own.
417    ///
418    /// The key is spelled once, here, because every plugin has to agree on it: a source
419    /// that invented its own spelling would hold work nothing else could read.
420    pub const METADATA_KEY: &'static str = "onetaskgraph.repositories";
421
422    /// The normalized `host/owner/name` origin.
423    #[must_use]
424    pub fn as_str(&self) -> &str {
425        &self.0
426    }
427
428    /// The origins a source records under [`Self::METADATA_KEY`], or none.
429    ///
430    /// # Errors
431    ///
432    /// Returns a message when the key holds something other than a duplicate-free list
433    /// of normalized origins.
434    pub fn from_metadata(metadata: &BTreeMap<String, Value>) -> Result<Vec<Self>, String> {
435        let Some(value) = metadata.get(Self::METADATA_KEY) else {
436            return Ok(Vec::new());
437        };
438        let origins: Vec<Self> = serde_json::from_value(value.clone()).map_err(|error| {
439            format!(
440                "{} is not a list of repository origins: {error}",
441                Self::METADATA_KEY
442            )
443        })?;
444        Self::unique(origins)
445    }
446
447    /// The same origins, in the order given, once it is established none repeats.
448    ///
449    /// # Errors
450    ///
451    /// Returns a message naming the first origin that appears twice.
452    pub fn unique(origins: Vec<Self>) -> Result<Vec<Self>, String> {
453        let mut seen = std::collections::BTreeSet::new();
454        for origin in &origins {
455            if !seen.insert(origin.as_str()) {
456                return Err(format!(
457                    "{:?} is listed twice; a repository list names each origin once",
458                    origin.as_str()
459                ));
460            }
461        }
462        Ok(origins)
463    }
464}
465
466fn unique_repositories<'de, D>(deserializer: D) -> Result<Vec<Repository>, D::Error>
467where
468    D: serde::Deserializer<'de>,
469{
470    Repository::unique(Vec::<Repository>::deserialize(deserializer)?)
471        .map_err(serde::de::Error::custom)
472}
473
474impl TryFrom<String> for Repository {
475    type Error = String;
476
477    fn try_from(origin: String) -> Result<Self, Self::Error> {
478        let valid = !origin.is_empty()
479            && !origin.contains("://")
480            && !origin.ends_with(".git")
481            && !origin.chars().any(char::is_whitespace)
482            && origin.split('/').count() >= 3
483            && origin
484                .split('/')
485                .all(|part| !part.is_empty() && part != "." && part != "..");
486        valid.then_some(Self(origin.clone())).ok_or_else(|| format!(
487            "{origin:?} is not a normalized repository origin; use host/owner/name without a scheme or .git suffix"
488        ))
489    }
490}
491
492impl From<Repository> for String {
493    fn from(repository: Repository) -> Self {
494        repository.0
495    }
496}
497
498/// One task named by another task's [`Task::delivers`] or [`Task::delivered_by`].
499///
500/// A string with one of two spellings, decided the way a [`DependencyEndpoint`] decides it:
501/// one holding a colon is `<source>:<native>` and names a task of any source, and one
502/// without is a bare native id naming a task of the source that holds the list. So a
503/// native id holding a colon cannot be named bare, exactly as it cannot in
504/// `onetaskgraph.depends_on`.
505#[derive(
506    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
507)]
508#[serde(try_from = "String", into = "String")]
509pub struct TaskRef(String);
510
511impl TaskRef {
512    /// The reserved metadata key a source records [`Task::delivers`] under when its backend
513    /// has no notion of its own.
514    ///
515    /// Spelled once, here, for the reason [`Repository::METADATA_KEY`] is.
516    pub const DELIVERS_KEY: &'static str = "onetaskgraph.delivers";
517
518    /// The reserved metadata key a source records [`Task::delivered_by`] under when its
519    /// backend has no notion of its own.
520    pub const DELIVERED_BY_KEY: &'static str = "onetaskgraph.delivered_by";
521
522    /// One entry, once it is established it is a task id.
523    ///
524    /// # Errors
525    ///
526    /// Returns a message saying why when the id is empty, or when it is qualified with a
527    /// source name that breaks the pattern or with no native id after the colon.
528    pub fn new(id: impl Into<String>) -> Result<Self, String> {
529        let id = id.into();
530        if id.is_empty() {
531            return Err("an empty string names no task".to_owned());
532        }
533        if let Some((source, native)) = id.split_once(':') {
534            SourceName::new(source).map_err(|error| error.to_string())?;
535            if native.is_empty() {
536                return Err(format!("{id:?} names a source and no task in it"));
537            }
538        }
539        Ok(Self(id))
540    }
541
542    /// The qualified entry naming `native` in `source`.
543    #[must_use]
544    pub fn qualified(source: &SourceName, native: &NativeId) -> Self {
545        Self(format!("{source}:{native}"))
546    }
547
548    /// The entry as it is spelled.
549    #[must_use]
550    pub fn as_str(&self) -> &str {
551        &self.0
552    }
553
554    /// Whether the entry names its source in writing.
555    #[must_use]
556    pub fn is_qualified(&self) -> bool {
557        self.0.contains(':')
558    }
559
560    /// The source and the native id this entry names, reading a bare entry as naming a task
561    /// of `near_source`.
562    #[must_use]
563    pub fn parts<'a>(&'a self, near_source: &'a str) -> (&'a str, &'a str) {
564        self.0
565            .split_once(':')
566            .unwrap_or((near_source, self.0.as_str()))
567    }
568
569    /// This entry qualified, reading a bare one as naming a task of `near_source`.
570    #[must_use]
571    pub fn in_source(&self, near_source: &SourceName) -> Self {
572        if self.is_qualified() {
573            return self.clone();
574        }
575        Self(format!("{near_source}:{}", self.0))
576    }
577
578    /// The entries of one task's list, once it is established that none names the task
579    /// itself and none repeats.
580    ///
581    /// `field` is what the list is called where it is stored, for the message. `near` is the
582    /// task holding the list and `near_source` the configured name of the source holding it,
583    /// which is what tells `T-1` and `work:T-1` apart as the same task. A source that does
584    /// not know its own name passes `None`, and then only a bare entry can be recognised as
585    /// naming this task or one of the other entries.
586    ///
587    /// # Errors
588    ///
589    /// Returns a message naming the task and the entry.
590    pub fn listed(
591        field: &str,
592        near: &NativeId,
593        near_source: Option<&SourceName>,
594        entries: Vec<Self>,
595    ) -> Result<Vec<Self>, String> {
596        let normal = |entry: &Self| match near_source {
597            Some(source) => entry.in_source(source).0,
598            None => entry.0.clone(),
599        };
600        let this = match near_source {
601            Some(source) => Self::qualified(source, near).0,
602            None => near.0.clone(),
603        };
604        let mut seen: Vec<(String, &Self)> = Vec::with_capacity(entries.len());
605        for entry in &entries {
606            let named = normal(entry);
607            if named == this {
608                return Err(format!(
609                    "{field} on task {near} names {entry}, which is that task itself; a task \
610                     cannot be listed in its own {field}"
611                ));
612            }
613            if let Some((_, first)) = seen.iter().find(|(held, _)| *held == named) {
614                return Err(format!(
615                    "{field} on task {near} names {entry} more than once (as {first} and \
616                     {entry}); name each task once"
617                ));
618            }
619            seen.push((named, entry));
620        }
621        Ok(entries)
622    }
623
624    /// The entries one task's list holds, read out of the JSON a source stores it as.
625    ///
626    /// `value` is `None` when the source holds no list at all, which is the empty one.
627    ///
628    /// # Errors
629    ///
630    /// Returns a message naming the task and the entry when the value is not a list, when an
631    /// entry is not a task id, or when [`Self::listed`] refuses the list.
632    pub fn from_value(
633        field: &str,
634        near: &NativeId,
635        near_source: Option<&SourceName>,
636        value: Option<&Value>,
637    ) -> Result<Vec<Self>, String> {
638        let Some(value) = value else {
639            return Ok(Vec::new());
640        };
641        let Some(held) = value.as_array() else {
642            return Err(format!(
643                "{field} on task {near} is {value}, which is not a list of task ids"
644            ));
645        };
646        let entries = held
647            .iter()
648            .map(|entry| {
649                entry
650                    .as_str()
651                    .ok_or_else(|| "it is not a string".to_owned())
652                    .and_then(Self::new)
653                    .map_err(|why| {
654                        format!(
655                            "{field} on task {near} holds {entry}, which is not a task id: {why}"
656                        )
657                    })
658            })
659            .collect::<Result<Vec<_>, _>>()?;
660        Self::listed(field, near, near_source, entries)
661    }
662}
663
664impl TryFrom<String> for TaskRef {
665    type Error = String;
666
667    fn try_from(id: String) -> Result<Self, Self::Error> {
668        Self::new(id)
669    }
670}
671
672impl From<TaskRef> for String {
673    fn from(entry: TaskRef) -> Self {
674        entry.0
675    }
676}
677
678impl std::fmt::Display for TaskRef {
679    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
680        self.0.fmt(formatter)
681    }
682}
683
684/// A tag a source attaches to work.
685#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
686pub struct Label {
687    /// The source's own opaque identifier.
688    pub id: NativeId,
689    /// What a user filtering across sources actually types.
690    pub name: String,
691    /// The source's own colour for the label, when it has one.
692    pub color: Option<String>,
693}
694
695/// A source's status, kept in both normalised and original form.
696///
697/// `category` is what every filter compares against; `name` is the source's own
698/// wording, preserved so display never flattens "In Review" into "In Progress".
699#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
700pub struct Status {
701    /// The normalised value filters compare against.
702    pub category: StatusCategory,
703    /// The source's own label for this status.
704    pub name: String,
705}
706
707/// The normalised status vocabulary shared across every source.
708#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
709#[serde(rename_all = "kebab-case")]
710pub enum StatusCategory {
711    /// Written down but not yet committed to as work.
712    Draft,
713    /// Known about, not yet accepted as ready to work.
714    Backlog,
715    /// Accepted and ready to be picked up, and nothing has claimed it.
716    Todo,
717    /// Claimed by work that will do it, and not yet started.
718    Queued,
719    /// Being worked on.
720    InProgress,
721    /// Done and waiting on a reviewer before it is finished.
722    ///
723    /// No source ships a default name for it: a source writes and reads it only at the name its
724    /// `status_mapping` gives it, so a board with no review column reads exactly as before.
725    InReview,
726    /// Finished.
727    Done,
728    /// Abandoned.
729    Cancelled,
730    /// The source reported a status this vocabulary cannot place.
731    Unknown,
732}
733
734/// How much a task matters, in the one vocabulary every source is normalised into.
735///
736/// Five values, most pressing first after [`None`](Self::None): Linear's own priority has
737/// exactly these, and a source whose backend has none of its own maps its representation
738/// onto them. `none` is a value rather than an absent field, because "no priority is set" is
739/// something a person sets — writing `none` clears a priority — and a reader tells it apart
740/// from nothing by the value alone.
741#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize, JsonSchema)]
742#[serde(rename_all = "kebab-case")]
743pub enum Priority {
744    /// No priority is set.
745    #[default]
746    None,
747    /// Drop everything for it.
748    Urgent,
749    /// Next, before the rest.
750    High,
751    /// In its turn.
752    Medium,
753    /// When there is nothing more pressing.
754    Low,
755}
756
757impl Priority {
758    /// Every value, in the order the vocabulary lists them.
759    pub const ALL: [Self; 5] = [
760        Self::None,
761        Self::Urgent,
762        Self::High,
763        Self::Medium,
764        Self::Low,
765    ];
766
767    /// The value as the wire spells it: `none`, `urgent`, `high`, `medium` or `low`.
768    #[must_use]
769    pub const fn as_str(self) -> &'static str {
770        match self {
771            Self::None => "none",
772            Self::Urgent => "urgent",
773            Self::High => "high",
774            Self::Medium => "medium",
775            Self::Low => "low",
776        }
777    }
778}
779
780impl std::fmt::Display for Priority {
781    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
782        formatter.write_str(self.as_str())
783    }
784}
785
786impl std::str::FromStr for Priority {
787    type Err = String;
788
789    fn from_str(value: &str) -> Result<Self, Self::Err> {
790        Self::ALL
791            .into_iter()
792            .find(|priority| priority.as_str() == value)
793            .ok_or_else(|| {
794                format!(
795                    "{value:?} is not a priority; a priority is one of none, urgent, high, \
796                     medium or low"
797                )
798            })
799    }
800}
801
802/// A dependency between two work items.
803///
804/// An endpoint may name another source. Keeping that far id on the near item is work data
805/// owned by its plugin, not an engine-side index or mirror; the engine reports it without
806/// resolving or fetching the far item.
807///
808/// A source uses its backend's own relationship wherever that relationship can name the
809/// far end, so the backend knows the graph and its own interface draws it. Where it
810/// cannot — a far end in another source, which no backend relates — the source reads
811/// [`Self::recorded`] from the near item instead. Only the forward direction is ever
812/// recorded; the reverse of a recorded edge is derived, exactly as a
813/// [`ForwardOnly`](crate::DependencySupport::ForwardOnly) source's reverse is.
814#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
815pub struct DependencyEdge {
816    /// The item the edge starts at, and the one that **depends on** the other.
817    ///
818    /// This is the orientation every source reports in, whichever way its own backend
819    /// spells the relationship: a GitHub `blockedBy` connection read for `ENG-1` yields
820    /// `from: ENG-1`, because `ENG-1` is what depends.
821    pub from: DependencyEndpoint,
822    /// The item the edge points at, and the one that must finish first.
823    pub to: DependencyEndpoint,
824    /// What the edge means.
825    pub kind: DependencyKind,
826}
827
828impl DependencyEdge {
829    /// The reserved metadata key a near item records a far end under.
830    ///
831    /// Spelled once, here, for the reason [`Repository::METADATA_KEY`] is: a plugin that
832    /// invented its own spelling would record a plan nothing else could read.
833    pub const RECORDED_KEY: &'static str = "onetaskgraph.depends_on";
834
835    /// The forward edges `near` records under [`Self::RECORDED_KEY`], or none.
836    ///
837    /// The key holds a list of endpoints — a bare string is a native id naming a task,
838    /// `{"id": "<source>:<native>", "kind": "project"}` names any item of any source, and
839    /// `{"designator": "<plugin>/<hex>", "id": "<native>", "kind": "task"}` names an item of
840    /// whichever tracker that designator is, conveying nothing about it.
841    /// Each becomes one `blocks` edge from `near` to that endpoint.
842    ///
843    /// `natively_names` is the kind of item the near item's **own backend** can relate it
844    /// to — `Some(ItemKind::Task)` for a GitHub issue, whose `blockedBy` connection holds
845    /// issues; `None` for a GitHub draft, which has no such connection at all. An endpoint
846    /// of that kind naming an item of `near_source` is refused, because it names an item
847    /// the backend itself could hold, and the rule this key exists to serve is the
848    /// backend's own relationship first. Naming one's own source is what an unqualified id
849    /// does implicitly and what `<near_source>:<native>` does in writing, so both are
850    /// refused: which of the two spellings a plan happened to use says nothing about where
851    /// the edge belongs.
852    ///
853    /// An endpoint qualified to a *different* source is never refused. That is the whole
854    /// case this key is for: no backend relates an id in a system it knows nothing about.
855    ///
856    /// # Errors
857    ///
858    /// Returns a message when the key holds anything other than a list of endpoints, or
859    /// holds one the near item's own backend was supposed to name.
860    pub fn recorded(
861        metadata: &BTreeMap<String, Value>,
862        near: &NativeId,
863        near_kind: ItemKind,
864        near_source: &SourceName,
865        natively_names: Option<ItemKind>,
866    ) -> Result<Vec<Self>, String> {
867        let Some(value) = metadata.get(Self::RECORDED_KEY) else {
868            return Ok(Vec::new());
869        };
870        let far: Vec<DependencyEndpoint> =
871            serde_json::from_value(value.clone()).map_err(|error| {
872                format!(
873                    "{} is not a list of dependency endpoints: {error}",
874                    Self::RECORDED_KEY
875                )
876            })?;
877        far.into_iter()
878            .map(|to| {
879                // An opaque far end names a tracker rather than a source, which is never one
880                // this backend relates.
881                let names_this_source = to.designator().is_none()
882                    && to
883                        .source()
884                        .is_none_or(|source| source == near_source.as_str());
885                if names_this_source && natively_names == Some(to.kind) {
886                    return Err(format!(
887                        "{key} on {near} records {to}, which this source can relate \
888                         natively; record it as this backend's own dependency and keep \
889                         {key} for a far end no relationship here can name",
890                        key = Self::RECORDED_KEY
891                    ));
892                }
893                Ok(Self {
894                    from: DependencyEndpoint::from_native(near.clone(), near_kind),
895                    to,
896                    kind: DependencyKind::Blocks,
897                })
898            })
899            .collect()
900    }
901}
902
903/// One endpoint of a dependency edge.
904///
905/// Three forms: a native id of the source reporting it, a qualified `<source>:<native>` id,
906/// and the **opaque** form — a [`Designator`] naming the tracker that holds the item and the
907/// item's native id there, which names a private item from a public one while conveying
908/// nothing about it.
909#[derive(Debug, Clone, PartialEq, Eq, Hash)]
910pub struct DependencyEndpoint {
911    /// A qualified `<source>:<native>` id, a legacy native id which the engine qualifies to
912    /// the source reporting the edge, or a designator with a native id.
913    id: EndpointIdentity,
914    /// Whether the endpoint names a task or a project.
915    pub kind: ItemKind,
916}
917
918#[derive(Debug, Clone, PartialEq, Eq, Hash)]
919enum EndpointIdentity {
920    Native(String),
921    Qualified(String),
922    Opaque { designator: Designator, id: String },
923}
924
925impl EndpointIdentity {
926    fn as_str(&self) -> &str {
927        match self {
928            Self::Native(id) | Self::Qualified(id) | Self::Opaque { id, .. } => id,
929        }
930    }
931
932    fn into_string(self) -> String {
933        match self {
934            Self::Native(id) | Self::Qualified(id) | Self::Opaque { id, .. } => id,
935        }
936    }
937}
938
939/// The keys the opaque form of a [`DependencyEndpoint`] holds, and the only ones.
940pub(crate) const OPAQUE_ENDPOINT_KEYS: [&str; 3] = ["designator", "id", "kind"];
941
942/// Names a tracker without conveying anything about it: `<plugin>/<64 lowercase hex>`, the
943/// SHA-256 of `<plugin>\n<identifier>` over the backend's own opaque identifier of itself —
944/// a Linear workspace's organization id, a GitHub Project's node id — or, for a plugin whose
945/// backend has none, `alias/<public_alias>`, the alias every host's configuration of that
946/// tracker declares identically.
947///
948/// The one spelling, validated wherever one is built or read, so a stored opaque edge cannot
949/// carry anything a designator does not.
950#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
951#[serde(transparent)]
952pub struct Designator(String);
953
954/// The pattern every [`Designator`] matches, as the schema publishes it. What
955/// [`Designator::try_from`] accepts is reconciled with it by `tests/designator_pattern.rs` in
956/// `onetaskgraph-core`, over the schema the bundle emits.
957pub(crate) const DESIGNATOR_PATTERN: &str =
958    "^(alias/[A-Za-z0-9_-]{16,64}|[a-z0-9][a-z0-9-]*/[0-9a-f]{64})$";
959
960impl JsonSchema for Designator {
961    fn schema_name() -> std::borrow::Cow<'static, str> {
962        "Designator".into()
963    }
964
965    fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
966        json_schema!({
967            "description": "A tracker, named without conveying anything about it: `<plugin>/` and the hex SHA-256 of `<plugin>\n<backend identifier>`, or `alias/<public_alias>`.",
968            "type": "string",
969            "pattern": DESIGNATOR_PATTERN
970        })
971    }
972}
973
974impl Designator {
975    /// The prefix of a designator built from a configured `public_alias`.
976    pub const ALIAS_PREFIX: &'static str = "alias";
977
978    /// The designator of a backend `plugin` identifies by `identifier`, its own opaque
979    /// identifier of itself.
980    ///
981    /// # Errors
982    ///
983    /// A message when `plugin` is not a plugin kind's spelling — the pattern a source name
984    /// matches — or is [`Self::ALIAS_PREFIX`], which an alias designator's prefix is.
985    pub fn of_backend(plugin: &str, identifier: &str) -> Result<Self, String> {
986        use sha2::{Digest, Sha256};
987        use std::fmt::Write as _;
988        if plugin == Self::ALIAS_PREFIX || SourceName::new(plugin).is_err() {
989            return Err(format!(
990                "{plugin:?} cannot prefix a designator: a plugin kind is lower-case letters, \
991                 digits and dashes, and not {:?}",
992                Self::ALIAS_PREFIX
993            ));
994        }
995        let mut hex = String::with_capacity(64);
996        for byte in Sha256::digest(format!("{plugin}\n{identifier}").as_bytes()) {
997            write!(hex, "{byte:02x}").expect("writing to a String cannot fail");
998        }
999        Ok(Self(format!("{plugin}/{hex}")))
1000    }
1001
1002    /// The designator of a tracker whose configuration declares `alias` as its
1003    /// `public_alias`.
1004    ///
1005    /// # Errors
1006    ///
1007    /// A message naming the pattern when `alias` is not one.
1008    pub fn of_alias(alias: &str) -> Result<Self, String> {
1009        if Self::is_public_alias(alias) {
1010            Ok(Self(format!("{}/{alias}", Self::ALIAS_PREFIX)))
1011        } else {
1012            Err(format!(
1013                "a public_alias is 16 to 64 of A-Z, a-z, 0-9, `_` and `-` \
1014                 (^[A-Za-z0-9_-]{{16,64}}$), and this one is {} characters of something else",
1015                alias.chars().count()
1016            ))
1017        }
1018    }
1019
1020    /// Whether `alias` is a valid `public_alias`: `^[A-Za-z0-9_-]{16,64}$`.
1021    #[must_use]
1022    pub fn is_public_alias(alias: &str) -> bool {
1023        (16..=64).contains(&alias.len())
1024            && alias
1025                .bytes()
1026                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
1027    }
1028
1029    /// The designator as it is spelled.
1030    #[must_use]
1031    pub fn as_str(&self) -> &str {
1032        &self.0
1033    }
1034
1035    /// What comes before the `/`: the plugin whose backend it digests, or
1036    /// [`Self::ALIAS_PREFIX`].
1037    #[must_use]
1038    pub fn prefix(&self) -> &str {
1039        self.0.split_once('/').map_or("", |(prefix, _)| prefix)
1040    }
1041
1042    /// Whether it was built from a configured `public_alias`.
1043    #[must_use]
1044    pub fn is_alias(&self) -> bool {
1045        self.prefix() == Self::ALIAS_PREFIX
1046    }
1047}
1048
1049impl TryFrom<String> for Designator {
1050    type Error = String;
1051
1052    fn try_from(value: String) -> Result<Self, Self::Error> {
1053        let refused =
1054            || "a designator is `<plugin>/<64 lowercase hex>` or `alias/<public_alias>`".to_owned();
1055        let (prefix, rest) = value.split_once('/').ok_or_else(refused)?;
1056        if prefix == Self::ALIAS_PREFIX {
1057            return Self::of_alias(rest);
1058        }
1059        let plugin = SourceName::new(prefix).is_ok();
1060        let digest = rest.len() == 64
1061            && rest
1062                .bytes()
1063                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
1064        if plugin && digest {
1065            Ok(Self(value))
1066        } else {
1067            Err(refused())
1068        }
1069    }
1070}
1071
1072impl<'de> Deserialize<'de> for Designator {
1073    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1074    where
1075        D: serde::Deserializer<'de>,
1076    {
1077        Self::try_from(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
1078    }
1079}
1080
1081impl std::fmt::Display for Designator {
1082    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1083        formatter.write_str(&self.0)
1084    }
1085}
1086
1087impl Serialize for DependencyEndpoint {
1088    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1089    where
1090        S: serde::Serializer,
1091    {
1092        #[derive(Serialize)]
1093        struct Wire<'a> {
1094            #[serde(skip_serializing_if = "Option::is_none")]
1095            designator: Option<&'a Designator>,
1096            id: &'a str,
1097            kind: ItemKind,
1098        }
1099        Wire {
1100            designator: self.designator(),
1101            id: self.id(),
1102            kind: self.kind,
1103        }
1104        .serialize(serializer)
1105    }
1106}
1107
1108impl JsonSchema for DependencyEndpoint {
1109    fn schema_name() -> std::borrow::Cow<'static, str> {
1110        "DependencyEndpoint".into()
1111    }
1112
1113    fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
1114        json_schema!({
1115            "description": "A dependency endpoint. A bare string is a native id of the source reporting it, and this decoding reads one as a task; a reader that knows the level it was written at — a source's own configuration, say — may read it at that level instead. An object with a `designator` is the opaque form: the item's native id in the tracker the designator names, which a reader resolves to whichever configured source that tracker is.",
1116            "oneOf": [
1117                {"type": "string", "minLength": 1},
1118                {
1119                    "type": "object",
1120                    "additionalProperties": false,
1121                    "required": ["id", "kind"],
1122                    "properties": {
1123                        "id": {"type": "string", "minLength": 1},
1124                        "kind": {"type": "string", "enum": ["task", "project"]}
1125                    }
1126                },
1127                {
1128                    "type": "object",
1129                    "additionalProperties": false,
1130                    "required": ["designator", "id", "kind"],
1131                    "properties": {
1132                        "designator": {"type": "string", "pattern": DESIGNATOR_PATTERN},
1133                        "id": {"type": "string", "minLength": 1},
1134                        "kind": {"type": "string", "enum": ["task", "project"]}
1135                    }
1136                }
1137            ]
1138        })
1139    }
1140}
1141
1142impl DependencyEndpoint {
1143    /// Builds an endpoint from a serialized id, validating a qualified id when present.
1144    ///
1145    /// # Errors
1146    ///
1147    /// Returns an error for an empty id or a malformed `<source>:<native>` id.
1148    pub fn new(id: String, kind: ItemKind) -> Result<Self, String> {
1149        let is_qualified = id.contains(':');
1150        let id = valid_endpoint_id(id)?;
1151        Ok(Self {
1152            id: if is_qualified {
1153                EndpointIdentity::Qualified(id)
1154            } else {
1155                EndpointIdentity::Native(id)
1156            },
1157            kind,
1158        })
1159    }
1160
1161    /// The opaque endpoint naming `id`, an item's native id in the tracker `designator` names.
1162    ///
1163    /// # Errors
1164    ///
1165    /// Returns an error for an empty id.
1166    pub fn opaque(designator: Designator, id: String, kind: ItemKind) -> Result<Self, String> {
1167        if id.is_empty() {
1168            return Err("a dependency endpoint id cannot be empty".into());
1169        }
1170        Ok(Self {
1171            id: EndpointIdentity::Opaque { designator, id },
1172            kind,
1173        })
1174    }
1175
1176    /// Reads an endpoint from any of its three JSON forms, naming what about an object
1177    /// makes it none of them.
1178    ///
1179    /// Unlike deserialization, which reads what a store already holds, this is for what a
1180    /// caller asks to be written: an object carrying a key beyond `designator`, `id` and `kind` —
1181    /// a title, a team, a URL — is refused with [`EndpointRefusal::Key`] naming that key, and
1182    /// never its value.
1183    ///
1184    /// # Errors
1185    ///
1186    /// [`EndpointRefusal`].
1187    pub fn from_value(value: &Value) -> Result<Self, EndpointRefusal> {
1188        if let Value::Object(entries) = value
1189            && let Some(key) = entries
1190                .keys()
1191                .find(|key| !OPAQUE_ENDPOINT_KEYS.contains(&key.as_str()))
1192        {
1193            return Err(EndpointRefusal::Key(key.clone()));
1194        }
1195        // Serde's diagnostic can repeat what it was given — an unknown `kind`, an id — so it is
1196        // not carried.
1197        serde_json::from_value(value.clone()).map_err(|_| EndpointRefusal::Malformed)
1198    }
1199
1200    /// Builds an endpoint from a source-native id, whose contents are deliberately opaque.
1201    #[must_use]
1202    pub fn from_native(id: NativeId, kind: ItemKind) -> Self {
1203        Self {
1204            id: EndpointIdentity::Native(id.0),
1205            kind,
1206        }
1207    }
1208
1209    /// The serialized native or qualified id — for the opaque form, the native id in the
1210    /// tracker [`Self::designator`] names.
1211    #[must_use]
1212    pub fn id(&self) -> &str {
1213        self.id.as_str()
1214    }
1215
1216    /// Consumes the endpoint and returns its serialized id.
1217    #[must_use]
1218    pub fn into_id(self) -> String {
1219        self.id.into_string()
1220    }
1221
1222    /// Whether the id was explicitly supplied as a qualified endpoint.
1223    #[must_use]
1224    pub fn is_qualified(&self) -> bool {
1225        matches!(self.id, EndpointIdentity::Qualified(_))
1226    }
1227
1228    /// The source segment of a qualified id, or `None` for a native or an opaque one.
1229    ///
1230    /// A native id belongs to whichever source reports it, so `None` reads as "this
1231    /// source" rather than "no source" — ask [`Self::is_native`] before reading it that way,
1232    /// because an opaque endpoint names no source of this configuration either.
1233    #[must_use]
1234    pub fn source(&self) -> Option<&str> {
1235        match &self.id {
1236            EndpointIdentity::Qualified(id) => id.split_once(':').map(|(source, _)| source),
1237            EndpointIdentity::Native(_) | EndpointIdentity::Opaque { .. } => None,
1238        }
1239    }
1240
1241    /// Whether the id is a native id of the source reporting it: neither qualified nor
1242    /// opaque.
1243    #[must_use]
1244    pub fn is_native(&self) -> bool {
1245        matches!(self.id, EndpointIdentity::Native(_))
1246    }
1247
1248    /// The tracker an opaque endpoint names, or `None` for the other two forms.
1249    #[must_use]
1250    pub fn designator(&self) -> Option<&Designator> {
1251        match &self.id {
1252            EndpointIdentity::Opaque { designator, .. } => Some(designator),
1253            EndpointIdentity::Native(_) | EndpointIdentity::Qualified(_) => None,
1254        }
1255    }
1256}
1257
1258impl<'de> Deserialize<'de> for DependencyEndpoint {
1259    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1260    where
1261        D: serde::Deserializer<'de>,
1262    {
1263        #[derive(Deserialize)]
1264        #[serde(deny_unknown_fields)]
1265        struct Opaque {
1266            designator: Designator,
1267            id: String,
1268            kind: ItemKind,
1269        }
1270        #[derive(Deserialize)]
1271        #[serde(untagged)]
1272        enum Wire {
1273            Legacy(String),
1274            Endpoint { id: String, kind: ItemKind },
1275        }
1276        let value = Value::deserialize(deserializer)?;
1277        // An object naming a designator is the opaque form and nothing else, so a malformed
1278        // one is refused rather than read as the qualified form with a key ignored.
1279        if value.get("designator").is_some() {
1280            let Opaque {
1281                designator,
1282                id,
1283                kind,
1284            } = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
1285            return Self::opaque(designator, id, kind).map_err(serde::de::Error::custom);
1286        }
1287        match Wire::deserialize(value).map_err(serde::de::Error::custom)? {
1288            Wire::Legacy(id) => {
1289                if id.is_empty() {
1290                    return Err(serde::de::Error::custom(
1291                        "a dependency endpoint id cannot be empty",
1292                    ));
1293                }
1294                Ok(Self::from_native(NativeId(id), ItemKind::Task))
1295            }
1296            Wire::Endpoint { id, kind } => Self::new(id, kind).map_err(serde::de::Error::custom),
1297        }
1298    }
1299}
1300
1301fn valid_endpoint_id(id: String) -> Result<String, String> {
1302    if id.is_empty() {
1303        return Err("a dependency endpoint id cannot be empty".into());
1304    }
1305    if let Some((source, native)) = id.split_once(':') {
1306        crate::SourceName::new(source).map_err(|error| error.to_string())?;
1307        if native.is_empty() {
1308            return Err("a qualified dependency endpoint must name a native id".into());
1309        }
1310    }
1311    Ok(id)
1312}
1313
1314/// Why [`DependencyEndpoint::from_value`] refused what a caller asked to be written. Neither
1315/// variant carries a value the caller gave, so a refusal can be printed anywhere.
1316#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
1317pub enum EndpointRefusal {
1318    /// The object carries this key beyond `designator`, `id` and `kind`.
1319    #[error(
1320        "a dependency endpoint object carries `{0}`; it holds only `designator`, `id` and `kind`"
1321    )]
1322    Key(String),
1323    /// It is none of the three forms.
1324    #[error(
1325        "not a dependency endpoint: give a native id, a qualified `<source>:<native id>`, or an \
1326         object of `designator`, `id` and `kind`"
1327    )]
1328    Malformed,
1329}
1330
1331impl std::fmt::Display for DependencyEndpoint {
1332    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1333        match &self.id {
1334            EndpointIdentity::Opaque { designator, id } => write!(formatter, "{designator}:{id}"),
1335            EndpointIdentity::Native(id) | EndpointIdentity::Qualified(id) => id.fmt(formatter),
1336        }
1337    }
1338}
1339
1340/// An opaque endpoint names an item of another tracker, so it never equals a native id of the
1341/// source comparing it, whatever id it holds.
1342impl PartialEq<NativeId> for DependencyEndpoint {
1343    fn eq(&self, other: &NativeId) -> bool {
1344        self.designator().is_none() && self.id() == other.0
1345    }
1346}
1347
1348/// The kind of work item named by a dependency endpoint.
1349#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1350#[serde(rename_all = "kebab-case")]
1351pub enum ItemKind {
1352    /// A task.
1353    Task,
1354    /// A project.
1355    Project,
1356}
1357
1358impl ItemKind {
1359    /// The reserved metadata key an item is marked with when its backend cannot say
1360    /// which kind it is.
1361    ///
1362    /// Spelled once, here, for the reason [`Repository::METADATA_KEY`] is: a key under
1363    /// this product's prefix belongs to the product, and a plugin inventing its own
1364    /// spelling would collide with the next one to want it.
1365    ///
1366    /// Unlike the other two reserved keys, this one obliges **no** source. A backend that
1367    /// knows its own kinds — folders, native projects — never reads or writes it, and
1368    /// passes it through as ordinary caller metadata with its JSON type intact, exactly
1369    /// as it passes through every other key it does not own. `github-projects` is the one
1370    /// source that needs it, because a GitHub Projects board holds only issues and an
1371    /// empty project is indistinguishable from a task without it.
1372    pub const METADATA_KEY: &'static str = "onetaskgraph.item_kind";
1373
1374    /// The value this kind is marked with under [`Self::METADATA_KEY`].
1375    #[must_use]
1376    pub const fn marker(self) -> &'static str {
1377        match self {
1378            Self::Task => "task",
1379            Self::Project => "project",
1380        }
1381    }
1382
1383    /// The kind `metadata` marks, or `None` when it carries no marker at all.
1384    ///
1385    /// # Errors
1386    ///
1387    /// Returns a message when [`Self::METADATA_KEY`] holds anything other than the two
1388    /// markers [`Self::marker`] spells.
1389    pub fn from_metadata(metadata: &BTreeMap<String, Value>) -> Result<Option<Self>, String> {
1390        let Some(value) = metadata.get(Self::METADATA_KEY) else {
1391            return Ok(None);
1392        };
1393        match value.as_str() {
1394            Some(marker) if marker == Self::Task.marker() => Ok(Some(Self::Task)),
1395            Some(marker) if marker == Self::Project.marker() => Ok(Some(Self::Project)),
1396            _ => Err(format!(
1397                "{} is {value}; it accepts only {:?} or {:?}",
1398                Self::METADATA_KEY,
1399                Self::Project.marker(),
1400                Self::Task.marker()
1401            )),
1402        }
1403    }
1404}
1405
1406/// What a [`DependencyEdge`] means.
1407///
1408/// Both variants are read in the one direction [`DependencyEdge::from`] fixes: `from`
1409/// depends on `to`. This enum said the opposite of that until the orientation was settled,
1410/// which is why it is spelled out twice rather than once.
1411#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1412#[serde(rename_all = "kebab-case")]
1413pub enum DependencyKind {
1414    /// `from` depends on `to`, and `to` must finish before `from` can.
1415    // llmlint: ignore[names_match_behavior] `"blocks"` is the approved serialized value, spelled in docs/plugin-protocol.md §4.8 and both generated SDKs; the variant names the kind of dependency, and `from`/`to` carry the direction. Renaming it is a wire change and the contract owner's call.
1416    Blocks,
1417    /// `from` and `to` are linked without an ordering.
1418    Related,
1419}
1420
1421/// Which way a dependency query walks the graph.
1422#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1423#[serde(rename_all = "kebab-case")]
1424pub enum Direction {
1425    /// What this item depends on — the forward edges every source can report.
1426    DependsOn,
1427    /// What depends on this item — emulated by the engine for a
1428    /// [`ForwardOnly`](crate::DependencySupport::ForwardOnly) source.
1429    DependedOnBy,
1430}