onetaskgraph_plugin_api/metadata.rs
1//! The one key a narrow metadata write names, and the refusal a source that cannot make one
2//! answers with.
3//!
4//! A narrow metadata write sets one key of one record's metadata and changes nothing else
5//! about the record — see [`TaskSource::set_task_metadata`](crate::TaskSource::set_task_metadata).
6//! What a caller may name there is narrower than what a record may hold: a record read from a
7//! source carries this product's own `onetaskgraph.` keys, and a caller writing one of them
8//! through this seam would be editing the store's bookkeeping by hand.
9
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12
13use crate::SourceError;
14
15/// One caller-owned metadata key: `<namespace>.<name>`, at least two non-empty segments
16/// separated by dots, whose first segment is not [`MetadataKey::RESERVED_NAMESPACE`].
17///
18/// Validated wherever one is built, deserialized included, so a plugin handed one never has
19/// to ask whether it names a key this product owns.
20#[derive(
21 Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
22)]
23#[serde(try_from = "String", into = "String")]
24pub struct MetadataKey(String);
25
26impl MetadataKey {
27 /// The namespace this product owns, and a narrow write therefore never names.
28 ///
29 /// Every key the contract reserves lives under it — `onetaskgraph.origin`,
30 /// `onetaskgraph.repositories`, `onetaskgraph.depends_on`, `onetaskgraph.delivers`,
31 /// `onetaskgraph.delivered_by`, `onetaskgraph.item_kind`, `onetaskgraph.template`
32 /// ([`Self::TEMPLATE_KEY`]) and `onetaskgraph.copies` ([`Self::COPIES_KEY`]) — and so
33 /// does any key it
34 /// reserves later, which is why the whole namespace is refused rather than a list.
35 pub const RESERVED_NAMESPACE: &'static str = "onetaskgraph";
36
37 /// The reserved key a task or a document rendered from a template records where it came
38 /// from under: an object holding the template's reference, the chain digest it was
39 /// rendered with, and the SHA-256 of its content and of its resolved answers.
40 ///
41 /// A key of this product's own, so no [`MetadataKey`] can name it: only a rendering write
42 /// — [`TaskSource::write_task_rendered`](crate::TaskSource::write_task_rendered) and
43 /// [`TaskSource::set_task_rendering`](crate::TaskSource::set_task_rendering) and their
44 /// document siblings — ever sets it, and a copy carries it like any other entry. The
45 /// engine builds and reads the value; a plugin only puts it where its metadata lives.
46 pub const TEMPLATE_KEY: &'static str = "onetaskgraph.template";
47
48 /// The reserved key a copied item records where it landed under: a JSON object mapping a
49 /// destination source name to the qualified id of that item's counterpart there, at
50 /// most one entry per destination.
51 ///
52 /// A key of this product's own, so [`Self::new`] refuses it like every other key in the
53 /// namespace: only the engine's copy writes it, through the narrow metadata write, and
54 /// [`Self::copies`] is how it names the key there. A plugin only puts it where its
55 /// metadata lives.
56 pub const COPIES_KEY: &'static str = "onetaskgraph.copies";
57
58 /// [`Self::COPIES_KEY`], as the one reserved key a narrow metadata write may carry.
59 ///
60 /// Not a way round [`Self::new`] for a caller: nothing a person types reaches this, and
61 /// the one write that sends it is the copy recording where an item landed.
62 #[must_use]
63 // llmlint: ignore[invalid_states_unrepresentable] A key type of its own would change the
64 // signature of the three narrow metadata writes on `TaskSource`, which every plugin
65 // implements and this crate keeps still (AGENTS.md); the reserved key reaches that seam
66 // only through this one named constructor, while `new`, deserialization, the command line
67 // and both SDKs refuse it, and the stdio boundary admits it only with a value that is links.
68 pub fn copies() -> Self {
69 Self(Self::COPIES_KEY.to_owned())
70 }
71
72 /// Whether this is [`Self::COPIES_KEY`].
73 #[must_use]
74 pub fn is_copies(&self) -> bool {
75 self.0 == Self::COPIES_KEY
76 }
77
78 /// One key, once it is established it is a caller's own dotted key.
79 ///
80 /// # Errors
81 ///
82 /// Returns a message saying why, and what to write instead, when the key has no dot, has
83 /// an empty segment, or is in [`Self::RESERVED_NAMESPACE`].
84 pub fn new(key: impl Into<String>) -> Result<Self, String> {
85 let key = key.into();
86 let segments: Vec<&str> = key.split('.').collect();
87 if segments.len() < 2 {
88 return Err(format!(
89 "the metadata key {key:?} has no namespace: a key is `<namespace>.<name>`, two \
90 or more non-empty segments separated by dots; next: name it under a namespace \
91 of your own, such as `myapp.{key}`"
92 ));
93 }
94 if segments.iter().any(|segment| segment.is_empty()) {
95 return Err(format!(
96 "the metadata key {key:?} has an empty segment: a key is `<namespace>.<name>`, \
97 two or more non-empty segments separated by dots; next: remove the extra dot"
98 ));
99 }
100 if segments[0] == Self::RESERVED_NAMESPACE {
101 return Err(format!(
102 "the metadata key {key:?} is in the `{}.` namespace, which this product owns \
103 and keeps in step itself; next: name the key under a namespace of your own",
104 Self::RESERVED_NAMESPACE
105 ));
106 }
107 Ok(Self(key))
108 }
109
110 /// The key as a record's metadata map spells it.
111 #[must_use]
112 pub fn as_str(&self) -> &str {
113 &self.0
114 }
115}
116
117impl TryFrom<String> for MetadataKey {
118 type Error = String;
119
120 fn try_from(key: String) -> Result<Self, Self::Error> {
121 Self::new(key)
122 }
123}
124
125impl From<MetadataKey> for String {
126 fn from(key: MetadataKey) -> Self {
127 key.0
128 }
129}
130
131impl std::fmt::Display for MetadataKey {
132 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 self.0.fmt(formatter)
134 }
135}
136
137/// Which kind of record a narrow metadata write names.
138///
139/// The three a record can be, and no fourth: a refusal, a not-found error or a protocol guard
140/// that names the record takes one of these rather than a noun, so it cannot name something
141/// that is not a record.
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub enum MetadataRecord {
144 /// A task, written through [`TaskSource::set_task_metadata`](crate::TaskSource::set_task_metadata).
145 Task,
146 /// A project, written through
147 /// [`TaskSource::set_project_metadata`](crate::TaskSource::set_project_metadata).
148 Project,
149 /// A document, written through
150 /// [`TaskSource::set_document_metadata`](crate::TaskSource::set_document_metadata).
151 Document,
152}
153
154impl MetadataRecord {
155 /// The record's noun as a message spells it: `task`, `project` or `document`.
156 #[must_use]
157 pub const fn noun(self) -> &'static str {
158 match self {
159 Self::Task => "task",
160 Self::Project => "project",
161 Self::Document => "document",
162 }
163 }
164}
165
166impl std::fmt::Display for MetadataRecord {
167 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168 formatter.write_str(self.noun())
169 }
170}
171
172/// The refusal a source answers a narrow metadata write with when it cannot make one on its
173/// own.
174///
175/// It reads `the linear plugin cannot write a document's metadata on its own`, naming the
176/// record. Spelled once beside [`unwritable_field`](crate::unwritable_field) for that
177/// function's reason: the trait's defaults and the engine's refusal of a plugin whose
178/// handshake does not declare the write say the same thing in the same words.
179#[must_use]
180pub fn unwritable_metadata(kind: &str, record: MetadataRecord) -> SourceError {
181 SourceError::Refused {
182 message: format!("the {kind} plugin cannot write a {record}'s metadata on its own"),
183 }
184}