pub const APPLICATION_ACTIVATE_ACTION: &str = "activate-window";
pub const APPLICATION_ACTIVATE_TYPE: &str = "tilo.app.activate-window";
pub const APPLICATION_OPEN_TYPE: &str = "tilo.app.open";
pub const APPLICATION_OPEN_LINK_ACTION: &str = "open-link";
pub const APPLICATION_OPEN_LINK_TYPE: &str = "tilo.app.open-link";
pub const OPEN_LINK_PARAM: &str = "link";
#[must_use]
pub fn open_link_arguments(link: &str) -> serde_json::Map<String, serde_json::Value> {
let mut arguments = serde_json::Map::new();
arguments.insert(
OPEN_LINK_PARAM.to_owned(),
serde_json::Value::String(link.to_owned()),
);
arguments
}
#[must_use]
pub fn link_scheme(link: &str) -> Option<String> {
let (scheme, rest) = link.split_once(':')?;
let mut chars = scheme.chars();
let valid = scheme.len() > 1
&& chars.next().is_some_and(|c| c.is_ascii_alphabetic())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
&& !rest.is_empty();
valid.then(|| scheme.to_ascii_lowercase())
}
#[must_use]
pub fn activate_spec(label: String, description: String) -> crate::ActionSpec {
let mut spec = crate::ActionSpec::typed_operation(
APPLICATION_ACTIVATE_ACTION,
label.clone(),
label,
crate::OperationKind::View,
APPLICATION_ACTIVATE_TYPE,
Vec::new(),
|_| crate::EncodedAction {
type_id: APPLICATION_ACTIVATE_TYPE.into(),
payload: b"{}".to_vec(),
},
)
.of_kind(crate::ActionKind::Read)
.with_description(description)
.for_audience(crate::Audience::Agents);
spec.type_mut().contract.result = crate::ResultSpec::Json {
schema: serde_json::json!({"type": "object"}),
};
spec
}
#[must_use]
pub fn activation_requested() -> crate::ActionOutput {
crate::ActionOutput {
value: Some(serde_json::json!({"activation_requested": true})),
resources: Vec::new(),
}
}
pub const APPLICATION_OUTLINE_ACTION: &str = "outline";
pub const APPLICATION_OUTLINE_TYPE: &str = "tilo.app.outline";
pub const OUTLINE_ANCHOR_PARAM: &str = "anchor";
pub const OUTLINE_PICTURE_UNAVAILABLE: &str = "picture_unavailable";
#[must_use]
pub fn outline_spec() -> crate::ActionSpec {
let mut spec = crate::ActionSpec::typed_operation(
APPLICATION_OUTLINE_ACTION,
"Read the view",
"Read the view",
crate::OperationKind::View,
APPLICATION_OUTLINE_TYPE,
vec![
crate::ActionParam::optional(
OUTLINE_ANCHOR_PARAM,
crate::ParamType::Text,
"An item of the app's to show first: the anchor of a search hit, or an item \
resource's id",
)
.nullable(),
crate::ActionParam::optional(
"picture",
crate::ParamType::Boolean,
"Also answer the view as a picture of it, where its window is drawn (default: \
text only)",
)
.nullable(),
crate::ActionParam::optional(
"scale",
crate::ParamType::Number,
"Pixels per point of the picture, 0.5 to 2 (default 1)",
)
.nullable(),
],
|args| {
crate::EncodedAction::encode_operation(
APPLICATION_OUTLINE_TYPE,
&serde_json::json!({
"picture": args.boolean("picture"),
"scale": args.number("scale"),
OUTLINE_ANCHOR_PARAM: args.text(OUTLINE_ANCHOR_PARAM),
}),
)
.unwrap_or_else(|_| crate::EncodedAction {
type_id: APPLICATION_OUTLINE_TYPE.into(),
payload: Vec::new(),
})
},
)
.of_kind(crate::ActionKind::Read)
.with_description(
"Read what the app's window shows as lines: every label, value and control in reading \
order, and the action each control runs, which is called by the address the line \
names. With picture, also the view as a picture. With an anchor, shows that item first.",
)
.for_audience(crate::Audience::Agents);
spec.type_mut().contract.result = crate::ResultSpec::Json {
schema: serde_json::json!({"type": "object"}),
};
spec
}
#[must_use]
pub fn outline_anchor(payload: &[u8]) -> Option<String> {
let args: serde_json::Value = serde_json::from_slice(payload).ok()?;
args.get(OUTLINE_ANCHOR_PARAM)
.and_then(serde_json::Value::as_str)
.filter(|anchor| !anchor.is_empty())
.map(str::to_owned)
}
pub fn outline_asked(payload: &[u8]) -> Result<(bool, f64), String> {
let args: serde_json::Value = if payload.is_empty() {
serde_json::Value::Null
} else {
serde_json::from_slice(payload).map_err(|error| error.to_string())?
};
let scale = args
.get("scale")
.and_then(serde_json::Value::as_f64)
.unwrap_or(1.0);
if !(0.5..=2.0).contains(&scale) {
return Err("scale must be within 0.5..=2".into());
}
let picture = args
.get("picture")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);
Ok((picture, scale))
}
#[cfg(test)]
mod tests {
use super::link_scheme;
#[test]
fn a_link_names_its_scheme_and_nothing_else_does() {
assert_eq!(link_scheme("geo:52.5,13.4").as_deref(), Some("geo"));
assert_eq!(
link_scheme("Settings://keyboard").as_deref(),
Some("settings")
);
assert_eq!(
link_scheme("web+search:cats").as_deref(),
Some("web+search")
);
for none in [
"/home/a/b.txt",
"cafe",
"geo:",
"C:\\notes",
"1x:y",
":a",
"ht tp://x",
] {
assert_eq!(link_scheme(none), None, "{none}");
}
}
}