1use serde::{Deserialize, Serialize};
20
21use crate::entry::{Did, EditOp};
22use crate::interaction::{InteractionAnchor, SemanticInteraction};
23use crate::key::CREATE_ACTION;
24
25#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
27#[serde(rename_all = "kebab-case")]
28pub enum StepKind {
29 Create,
31 Write,
33 Edit,
35 Delete,
37 Move,
39 Read,
41 Run,
43 #[default]
45 Work,
46}
47
48impl StepKind {
49 #[must_use]
51 pub const fn word(self) -> &'static str {
52 match self {
53 Self::Create => "create",
54 Self::Write => "write",
55 Self::Edit => "edit",
56 Self::Delete => "delete",
57 Self::Move => "move",
58 Self::Read => "read",
59 Self::Run => "run",
60 Self::Work => "work",
61 }
62 }
63
64 #[must_use]
69 pub const fn did_message(self) -> &'static str {
70 match self {
71 Self::Create => DID_CREATED,
72 Self::Write => DID_WROTE,
73 Self::Edit => DID_EDITED,
74 Self::Delete => DID_DELETED,
75 Self::Move => DID_MOVED,
76 Self::Read => DID_READ,
77 Self::Run => DID_RAN,
78 Self::Work => DID_WORKED,
79 }
80 }
81
82 #[must_use]
84 pub fn of_word(word: &str) -> Option<Self> {
85 [
86 Self::Create,
87 Self::Write,
88 Self::Edit,
89 Self::Delete,
90 Self::Move,
91 Self::Read,
92 Self::Run,
93 Self::Work,
94 ]
95 .into_iter()
96 .find(|kind| kind.word() == word)
97 }
98
99 #[must_use]
102 pub const fn of_edit(op: EditOp) -> Self {
103 match op {
104 EditOp::Splice | EditOp::Replace | EditOp::Mark => Self::Write,
105 EditOp::Add
106 | EditOp::Insert
107 | EditOp::InsertRow
108 | EditOp::InsertColumn
109 | EditOp::React => Self::Create,
110 EditOp::Remove
111 | EditOp::RemoveRow
112 | EditOp::RemoveColumn
113 | EditOp::Unreact
114 | EditOp::Forget => Self::Delete,
115 EditOp::MoveTo => Self::Move,
116 EditOp::Set | EditOp::SetCell => Self::Edit,
117 EditOp::Settle => Self::Work,
118 }
119 }
120
121 #[must_use]
126 pub const fn of_anchor(anchor: &InteractionAnchor) -> Option<Self> {
127 match anchor {
128 InteractionAnchor::Text { .. } | InteractionAnchor::Grid { .. } => Some(Self::Write),
129 InteractionAnchor::ListItems { .. }
130 | InteractionAnchor::Objects { .. }
131 | InteractionAnchor::SemanticControl { .. } => Some(Self::Edit),
132 InteractionAnchor::Timeline { .. } | InteractionAnchor::Resource => None,
136 }
137 }
138
139 #[must_use]
145 pub fn of_did(what: &Did) -> Option<Self> {
146 match what {
147 Did::Edit(edit) => Some(Self::of_edit(edit.op)),
148 Did::Verb(verb) => {
149 if verb.action.action == CREATE_ACTION {
150 return Some(Self::Create);
151 }
152 verb.args
155 .keys()
156 .any(|name| COMMAND_PARAMS.contains(&name.as_str()))
157 .then_some(Self::Run)
158 }
159 Did::Group(group) => group.shared().and_then(Self::of_did),
160 Did::History(_) => None,
161 }
162 }
163}
164
165#[must_use]
169pub fn kind(declared: Option<StepKind>, anchor: &InteractionAnchor) -> StepKind {
170 declared
171 .or_else(|| StepKind::of_anchor(anchor))
172 .unwrap_or(StepKind::Work)
173}
174
175#[derive(Clone, Debug, PartialEq, Eq)]
178pub struct Step<'a> {
179 pub kind: StepKind,
180 pub record: Option<&'a str>,
183 pub app: Option<&'a str>,
186 pub command: Option<&'a str>,
188 pub makes: Option<(&'a str, &'a str)>,
194}
195
196impl<'a> Step<'a> {
197 #[must_use]
199 pub fn new(kind: StepKind, record: Option<&'a str>) -> Self {
200 Self {
201 kind,
202 record: nonempty(record),
203 app: None,
204 command: None,
205 makes: None,
206 }
207 }
208
209 #[must_use]
211 pub fn in_app(mut self, app: Option<&'a str>) -> Self {
212 self.app = nonempty(app);
213 self
214 }
215
216 #[must_use]
218 pub fn running(mut self, command: Option<&'a str>) -> Self {
219 self.command = nonempty(command);
220 self
221 }
222
223 #[must_use]
226 pub fn making(mut self, kind: &'a str, field: &'a str) -> Self {
227 self.makes = Some((kind, field));
228 self
229 }
230
231 #[must_use]
235 pub fn of_interaction(interaction: &'a SemanticInteraction) -> Step<'a> {
236 Self {
237 kind: kind(interaction.action.step, &interaction.anchor),
238 record: nonempty(Some(interaction.resource.label.as_str())),
239 app: nonempty(
240 interaction
241 .action
242 .app
243 .as_ref()
244 .map(|app| app.label.as_str()),
245 ),
246 command: None,
247 makes: interaction
248 .action
249 .creates
250 .as_deref()
251 .and_then(|field| nonempty(Some(field)))
252 .map(|field| (interaction.resource.kind.as_str(), field)),
253 }
254 }
255
256 #[must_use]
261 pub fn message(&self) -> (&'static str, Option<(&'static str, &'a str)>) {
262 fn named<'n>(
263 key: &'static str,
264 name: &'n str,
265 ) -> (&'static str, Option<(&'static str, &'n str)>) {
266 (key, Some(("record", name)))
267 }
268 match self.kind {
269 StepKind::Create => match (self.record, self.app) {
270 (Some(record), _) => named(CREATING, record),
271 (None, Some(app)) => named(CREATING, app),
272 (None, None) => (WORKING_ALONE, None),
273 },
274 StepKind::Write => self
275 .record
276 .map_or((WORKING_ALONE, None), |record| named(WRITING, record)),
277 StepKind::Edit => self
278 .record
279 .map_or((WORKING_ALONE, None), |record| named(EDITING, record)),
280 StepKind::Delete => self
281 .record
282 .map_or((WORKING_ALONE, None), |record| named(DELETING, record)),
283 StepKind::Move => self
284 .record
285 .map_or((WORKING_ALONE, None), |record| named(MOVING, record)),
286 StepKind::Read => self
287 .record
288 .map_or((WORKING_ALONE, None), |record| named(READING, record)),
289 StepKind::Run => match (self.command, self.record) {
290 (Some(command), _) => (RUNNING, Some(("command", command))),
291 (None, Some(record)) => named(RUNNING_IN, record),
292 (None, None) => (WORKING_ALONE, None),
293 },
294 StepKind::Work => self
295 .record
296 .map_or((WORKING_ALONE, None), |record| named(WORKING, record)),
297 }
298 }
299}
300
301pub const COMMAND_PARAMS: &[&str] = &["command", "command_line", "cmd"];
305
306pub const CREATING: &str = "step-creating";
308pub const CREATING_ONE: &str = "step-creating-one";
310pub const WRITING: &str = "step-writing";
312pub const EDITING: &str = "step-editing";
314pub const DELETING: &str = "step-deleting";
316pub const MOVING: &str = "step-moving";
318pub const READING: &str = "step-reading";
320pub const RUNNING: &str = "step-running";
322pub const RUNNING_IN: &str = "step-running-in";
324pub const WORKING: &str = "step-working";
326pub const WORKING_ALONE: &str = "step-working-alone";
328
329pub const DID_CREATED: &str = "step-did-created";
331pub const DID_WROTE: &str = "step-did-wrote";
332pub const DID_EDITED: &str = "step-did-edited";
333pub const DID_DELETED: &str = "step-did-deleted";
334pub const DID_MOVED: &str = "step-did-moved";
335pub const DID_READ: &str = "step-did-read";
336pub const DID_RAN: &str = "step-did-ran";
337pub const DID_WORKED: &str = "step-did-worked";
338
339fn nonempty(name: Option<&str>) -> Option<&str> {
341 name.map(str::trim).filter(|name| !name.is_empty())
342}
343
344#[cfg(test)]
345mod tests {
346 #![allow(
347 clippy::unwrap_used,
348 clippy::expect_used,
349 reason = "in a test the unwrap is the assertion"
350 )]
351
352 use super::*;
353 use crate::interaction::{InteractionAction, InteractionTextRange};
354 use crate::{ActionApp, ActionResource, InteractionPhase, InvocationId};
355
356 fn interaction(
357 step: Option<StepKind>,
358 anchor: InteractionAnchor,
359 record: &str,
360 ) -> SemanticInteraction {
361 SemanticInteraction {
362 version: crate::INTERACTION_VERSION,
363 invocation: InvocationId(uuid::Uuid::from_u128(1)),
364 sequence: 1,
365 actor: crate::Acting::agent("quill", "ada"),
366 phase: InteractionPhase::Applying,
367 action: InteractionAction {
368 id: "create".into(),
369 label: "Create an issue".into(),
370 app: Some(ActionApp {
371 id: "so.tilo.tasks".into(),
372 label: "Tasks".into(),
373 }),
374 step,
375 creates: None,
376 did: None,
377 },
378 resource: ActionResource {
379 id: "tasks.tracker".into(),
380 kind: "tasks.tracker".into(),
381 label: record.into(),
382 },
383 anchor,
384 group: None,
385 }
386 }
387
388 tilo_test_env::scenario! {
389 fn a_step_names_its_kind_and_record;
390
391 phase a_steps_kind_is_declared_else_read_from_the_part_it_acts_on;
392 {
395 let text = InteractionAnchor::Text {
396 region: "text".into(),
397 ranges: vec![InteractionTextRange {
398 block: "note".into(),
399 start: 0,
400 end: 0,
401 }],
402 };
403 let created = interaction(Some(StepKind::Create), InteractionAnchor::Resource, "Tasks");
404 assert_eq!(Step::of_interaction(&created).kind, StepKind::Create);
405 let written = interaction(None, text, "Trip plan");
406 let writing = Step::of_interaction(&written);
407 assert_eq!(writing.kind, StepKind::Write);
408 assert_eq!(writing.message(), (WRITING, Some(("record", "Trip plan"))));
409 let items = InteractionAnchor::ListItems {
410 ids: vec!["issue-1".into()],
411 };
412 assert_eq!(
413 Step::of_interaction(&interaction(None, items, "Book the ferry")).kind,
414 StepKind::Edit,
415 "an item's own step changes that item"
416 );
417 assert_eq!(
418 Step::of_interaction(&interaction(None, InteractionAnchor::Resource, "Issues")).kind,
419 StepKind::Work
420 );
421 }
422
423 phase a_step_names_its_record_or_says_only_what_it_is;
424 {
427 let made = interaction(
428 Some(StepKind::Create),
429 InteractionAnchor::Resource,
430 "Issues",
431 );
432 let created = Step::of_interaction(&made).in_app(Some(" Tasks "));
433 assert_eq!(created.message(), (CREATING, Some(("record", "Issues"))));
434 let mut declaring = made.clone();
437 declaring.action.creates = Some(" issues ".into());
438 assert_eq!(
439 Step::of_interaction(&declaring).makes,
440 Some(("tasks.tracker", "issues")),
441 "what it makes, named by the record's kind and its collection"
442 );
443 assert_eq!(created.makes, None, "a create that names none makes none");
444 assert_eq!(
445 Step::new(StepKind::Create, None)
446 .in_app(Some("Tasks"))
447 .message(),
448 (CREATING, Some(("record", "Tasks")))
449 );
450 assert_eq!(
451 Step::new(StepKind::Delete, None).message(),
452 (WORKING_ALONE, None)
453 );
454 assert_eq!(
455 Step::new(StepKind::Run, Some("Terminal")).running(Some("cargo build")),
456 Step {
457 kind: StepKind::Run,
458 record: Some("Terminal"),
459 app: None,
460 command: Some("cargo build"),
461 makes: None,
462 }
463 );
464 assert_eq!(
465 Step::new(StepKind::Run, Some("Terminal")).message(),
466 (RUNNING_IN, Some(("record", "Terminal")))
467 );
468 }
469
470 phase a_committed_change_names_its_kind_as_it_declared_it;
471 {
474 for (op, kind) in [
475 (EditOp::Splice, StepKind::Write),
476 (EditOp::Replace, StepKind::Write),
477 (EditOp::Insert, StepKind::Create),
478 (EditOp::Remove, StepKind::Delete),
479 (EditOp::MoveTo, StepKind::Move),
480 (EditOp::Set, StepKind::Edit),
481 ] {
482 assert_eq!(StepKind::of_edit(op), kind, "{op:?}");
483 }
484 let made = Did::verb(
485 crate::ActionKey::new("so.tilo.notes", "notes.note", CREATE_ACTION),
486 std::iter::empty::<(String, crate::Scalar)>(),
487 );
488 assert_eq!(StepKind::of_did(&made), Some(StepKind::Create));
489 let command = Did::verb(
490 crate::ActionKey::new("so.tilo.terminal", "tl.so.tilo.terminal.Session", "run"),
491 [(
492 "command".to_owned(),
493 crate::Scalar::from_json(&serde_json::json!("cargo build")).unwrap(),
494 )],
495 );
496 assert_eq!(StepKind::of_did(&command), Some(StepKind::Run));
497 let renamed = Did::verb(
498 crate::ActionKey::new("so.tilo.notes", "notes.note", "rename"),
499 std::iter::empty::<(String, crate::Scalar)>(),
500 );
501 assert_eq!(StepKind::of_did(&renamed), None);
502 }
503
504 phase a_kind_reads_back_from_its_word;
505 {
506 for kind in [
507 StepKind::Create,
508 StepKind::Write,
509 StepKind::Edit,
510 StepKind::Delete,
511 StepKind::Move,
512 StepKind::Read,
513 StepKind::Run,
514 StepKind::Work,
515 ] {
516 assert_eq!(StepKind::of_word(kind.word()), Some(kind));
517 }
518 assert_eq!(StepKind::of_word("writing"), None);
519 }
520 }
521}