use serde::{Deserialize, Serialize};
use crate::entry::{Did, EditOp};
use crate::interaction::{InteractionAnchor, SemanticInteraction};
use crate::key::CREATE_ACTION;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum StepKind {
Create,
Write,
Edit,
Delete,
Move,
Read,
Run,
#[default]
Work,
}
impl StepKind {
#[must_use]
pub const fn word(self) -> &'static str {
match self {
Self::Create => "create",
Self::Write => "write",
Self::Edit => "edit",
Self::Delete => "delete",
Self::Move => "move",
Self::Read => "read",
Self::Run => "run",
Self::Work => "work",
}
}
#[must_use]
pub const fn did_message(self) -> &'static str {
match self {
Self::Create => DID_CREATED,
Self::Write => DID_WROTE,
Self::Edit => DID_EDITED,
Self::Delete => DID_DELETED,
Self::Move => DID_MOVED,
Self::Read => DID_READ,
Self::Run => DID_RAN,
Self::Work => DID_WORKED,
}
}
#[must_use]
pub fn of_word(word: &str) -> Option<Self> {
[
Self::Create,
Self::Write,
Self::Edit,
Self::Delete,
Self::Move,
Self::Read,
Self::Run,
Self::Work,
]
.into_iter()
.find(|kind| kind.word() == word)
}
#[must_use]
pub const fn of_edit(op: EditOp) -> Self {
match op {
EditOp::Splice | EditOp::Replace | EditOp::Mark => Self::Write,
EditOp::Add
| EditOp::Insert
| EditOp::InsertRow
| EditOp::InsertColumn
| EditOp::React => Self::Create,
EditOp::Remove
| EditOp::RemoveRow
| EditOp::RemoveColumn
| EditOp::Unreact
| EditOp::Forget => Self::Delete,
EditOp::MoveTo => Self::Move,
EditOp::Set | EditOp::SetCell => Self::Edit,
EditOp::Settle => Self::Work,
}
}
#[must_use]
pub const fn of_anchor(anchor: &InteractionAnchor) -> Option<Self> {
match anchor {
InteractionAnchor::Text { .. } | InteractionAnchor::Grid { .. } => Some(Self::Write),
InteractionAnchor::ListItems { .. }
| InteractionAnchor::Objects { .. }
| InteractionAnchor::SemanticControl { .. } => Some(Self::Edit),
InteractionAnchor::Timeline { .. } | InteractionAnchor::Resource => None,
}
}
#[must_use]
pub fn of_did(what: &Did) -> Option<Self> {
match what {
Did::Edit(edit) => Some(Self::of_edit(edit.op)),
Did::Verb(verb) => {
if verb.action.action == CREATE_ACTION {
return Some(Self::Create);
}
verb.args
.keys()
.any(|name| COMMAND_PARAMS.contains(&name.as_str()))
.then_some(Self::Run)
}
Did::Group(group) => group.shared().and_then(Self::of_did),
Did::History(_) => None,
}
}
}
#[must_use]
pub fn kind(declared: Option<StepKind>, anchor: &InteractionAnchor) -> StepKind {
declared
.or_else(|| StepKind::of_anchor(anchor))
.unwrap_or(StepKind::Work)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Step<'a> {
pub kind: StepKind,
pub record: Option<&'a str>,
pub app: Option<&'a str>,
pub command: Option<&'a str>,
pub makes: Option<(&'a str, &'a str)>,
}
impl<'a> Step<'a> {
#[must_use]
pub fn new(kind: StepKind, record: Option<&'a str>) -> Self {
Self {
kind,
record: nonempty(record),
app: None,
command: None,
makes: None,
}
}
#[must_use]
pub fn in_app(mut self, app: Option<&'a str>) -> Self {
self.app = nonempty(app);
self
}
#[must_use]
pub fn running(mut self, command: Option<&'a str>) -> Self {
self.command = nonempty(command);
self
}
#[must_use]
pub fn making(mut self, kind: &'a str, field: &'a str) -> Self {
self.makes = Some((kind, field));
self
}
#[must_use]
pub fn of_interaction(interaction: &'a SemanticInteraction) -> Step<'a> {
Self {
kind: kind(interaction.action.step, &interaction.anchor),
record: nonempty(Some(interaction.resource.label.as_str())),
app: nonempty(
interaction
.action
.app
.as_ref()
.map(|app| app.label.as_str()),
),
command: None,
makes: interaction
.action
.creates
.as_deref()
.and_then(|field| nonempty(Some(field)))
.map(|field| (interaction.resource.kind.as_str(), field)),
}
}
#[must_use]
pub fn message(&self) -> (&'static str, Option<(&'static str, &'a str)>) {
fn named<'n>(
key: &'static str,
name: &'n str,
) -> (&'static str, Option<(&'static str, &'n str)>) {
(key, Some(("record", name)))
}
match self.kind {
StepKind::Create => match (self.record, self.app) {
(Some(record), _) => named(CREATING, record),
(None, Some(app)) => named(CREATING, app),
(None, None) => (WORKING_ALONE, None),
},
StepKind::Write => self
.record
.map_or((WORKING_ALONE, None), |record| named(WRITING, record)),
StepKind::Edit => self
.record
.map_or((WORKING_ALONE, None), |record| named(EDITING, record)),
StepKind::Delete => self
.record
.map_or((WORKING_ALONE, None), |record| named(DELETING, record)),
StepKind::Move => self
.record
.map_or((WORKING_ALONE, None), |record| named(MOVING, record)),
StepKind::Read => self
.record
.map_or((WORKING_ALONE, None), |record| named(READING, record)),
StepKind::Run => match (self.command, self.record) {
(Some(command), _) => (RUNNING, Some(("command", command))),
(None, Some(record)) => named(RUNNING_IN, record),
(None, None) => (WORKING_ALONE, None),
},
StepKind::Work => self
.record
.map_or((WORKING_ALONE, None), |record| named(WORKING, record)),
}
}
}
pub const COMMAND_PARAMS: &[&str] = &["command", "command_line", "cmd"];
pub const CREATING: &str = "step-creating";
pub const CREATING_ONE: &str = "step-creating-one";
pub const WRITING: &str = "step-writing";
pub const EDITING: &str = "step-editing";
pub const DELETING: &str = "step-deleting";
pub const MOVING: &str = "step-moving";
pub const READING: &str = "step-reading";
pub const RUNNING: &str = "step-running";
pub const RUNNING_IN: &str = "step-running-in";
pub const WORKING: &str = "step-working";
pub const WORKING_ALONE: &str = "step-working-alone";
pub const DID_CREATED: &str = "step-did-created";
pub const DID_WROTE: &str = "step-did-wrote";
pub const DID_EDITED: &str = "step-did-edited";
pub const DID_DELETED: &str = "step-did-deleted";
pub const DID_MOVED: &str = "step-did-moved";
pub const DID_READ: &str = "step-did-read";
pub const DID_RAN: &str = "step-did-ran";
pub const DID_WORKED: &str = "step-did-worked";
fn nonempty(name: Option<&str>) -> Option<&str> {
name.map(str::trim).filter(|name| !name.is_empty())
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "in a test the unwrap is the assertion"
)]
use super::*;
use crate::interaction::{InteractionAction, InteractionTextRange};
use crate::{ActionApp, ActionResource, InteractionPhase, InvocationId};
fn interaction(
step: Option<StepKind>,
anchor: InteractionAnchor,
record: &str,
) -> SemanticInteraction {
SemanticInteraction {
version: crate::INTERACTION_VERSION,
invocation: InvocationId(uuid::Uuid::from_u128(1)),
sequence: 1,
actor: crate::Acting::agent("quill", "ada"),
phase: InteractionPhase::Applying,
action: InteractionAction {
id: "create".into(),
label: "Create an issue".into(),
app: Some(ActionApp {
id: "so.tilo.tasks".into(),
label: "Tasks".into(),
}),
step,
creates: None,
did: None,
},
resource: ActionResource {
id: "tasks.tracker".into(),
kind: "tasks.tracker".into(),
label: record.into(),
},
anchor,
group: None,
}
}
tilo_test_env::scenario! {
fn a_step_names_its_kind_and_record;
phase a_steps_kind_is_declared_else_read_from_the_part_it_acts_on;
{
let text = InteractionAnchor::Text {
region: "text".into(),
ranges: vec![InteractionTextRange {
block: "note".into(),
start: 0,
end: 0,
}],
};
let created = interaction(Some(StepKind::Create), InteractionAnchor::Resource, "Tasks");
assert_eq!(Step::of_interaction(&created).kind, StepKind::Create);
let written = interaction(None, text, "Trip plan");
let writing = Step::of_interaction(&written);
assert_eq!(writing.kind, StepKind::Write);
assert_eq!(writing.message(), (WRITING, Some(("record", "Trip plan"))));
let items = InteractionAnchor::ListItems {
ids: vec!["issue-1".into()],
};
assert_eq!(
Step::of_interaction(&interaction(None, items, "Book the ferry")).kind,
StepKind::Edit,
"an item's own step changes that item"
);
assert_eq!(
Step::of_interaction(&interaction(None, InteractionAnchor::Resource, "Issues")).kind,
StepKind::Work
);
}
phase a_step_names_its_record_or_says_only_what_it_is;
{
let made = interaction(
Some(StepKind::Create),
InteractionAnchor::Resource,
"Issues",
);
let created = Step::of_interaction(&made).in_app(Some(" Tasks "));
assert_eq!(created.message(), (CREATING, Some(("record", "Issues"))));
let mut declaring = made.clone();
declaring.action.creates = Some(" issues ".into());
assert_eq!(
Step::of_interaction(&declaring).makes,
Some(("tasks.tracker", "issues")),
"what it makes, named by the record's kind and its collection"
);
assert_eq!(created.makes, None, "a create that names none makes none");
assert_eq!(
Step::new(StepKind::Create, None)
.in_app(Some("Tasks"))
.message(),
(CREATING, Some(("record", "Tasks")))
);
assert_eq!(
Step::new(StepKind::Delete, None).message(),
(WORKING_ALONE, None)
);
assert_eq!(
Step::new(StepKind::Run, Some("Terminal")).running(Some("cargo build")),
Step {
kind: StepKind::Run,
record: Some("Terminal"),
app: None,
command: Some("cargo build"),
makes: None,
}
);
assert_eq!(
Step::new(StepKind::Run, Some("Terminal")).message(),
(RUNNING_IN, Some(("record", "Terminal")))
);
}
phase a_committed_change_names_its_kind_as_it_declared_it;
{
for (op, kind) in [
(EditOp::Splice, StepKind::Write),
(EditOp::Replace, StepKind::Write),
(EditOp::Insert, StepKind::Create),
(EditOp::Remove, StepKind::Delete),
(EditOp::MoveTo, StepKind::Move),
(EditOp::Set, StepKind::Edit),
] {
assert_eq!(StepKind::of_edit(op), kind, "{op:?}");
}
let made = Did::verb(
crate::ActionKey::new("so.tilo.notes", "notes.note", CREATE_ACTION),
std::iter::empty::<(String, crate::Scalar)>(),
);
assert_eq!(StepKind::of_did(&made), Some(StepKind::Create));
let command = Did::verb(
crate::ActionKey::new("so.tilo.terminal", "tl.so.tilo.terminal.Session", "run"),
[(
"command".to_owned(),
crate::Scalar::from_json(&serde_json::json!("cargo build")).unwrap(),
)],
);
assert_eq!(StepKind::of_did(&command), Some(StepKind::Run));
let renamed = Did::verb(
crate::ActionKey::new("so.tilo.notes", "notes.note", "rename"),
std::iter::empty::<(String, crate::Scalar)>(),
);
assert_eq!(StepKind::of_did(&renamed), None);
}
phase a_kind_reads_back_from_its_word;
{
for kind in [
StepKind::Create,
StepKind::Write,
StepKind::Edit,
StepKind::Delete,
StepKind::Move,
StepKind::Read,
StepKind::Run,
StepKind::Work,
] {
assert_eq!(StepKind::of_word(kind.word()), Some(kind));
}
assert_eq!(StepKind::of_word("writing"), None);
}
}
}