Skip to main content

tilo_actions/remote/
providers.rs

1//! Tokens naming who publishes an action, and the resources a workspace's
2//! own actions on an application live under.
3
4/// Provider token naming the calling application's own connection, for an
5/// app that routes one of its own actions through the provider.
6pub const OWN_PROVIDER: &str = "self";
7
8/// Prefix of the provider token for actions a workspace itself offers
9/// rather than an application; see [`workspace_provider`].
10pub const WORKSPACE_PROVIDER: &str = "workspace";
11
12/// Resource prefix of a workspace's "open this application" actions.
13pub const APPLICATION_RESOURCE_PREFIX: &str = "app.";
14
15/// Resource kind of an installed Tilo application a workspace lists, which
16/// a catalogue query's `resource_name` answers whole.
17pub const APPLICATION_RESOURCE_KIND: &str = "applications.app";
18
19/// Action id that opens an application into a workspace.
20pub const APPLICATION_OPEN_ACTION: &str = "open";
21
22/// The provider a device answers the workspace's own folder under: the files it holds, and
23/// Files' verbs on them by paths in the workspace (`tilo_action_services::device_files`).
24pub const DEVICE_FILES_PROVIDER: &str = "tilo.device-files";
25
26/// The resource of the workspace's own folder a device's actions are on. It is the folder the
27/// Files app behind [`APPLICATION_RESOURCE_PREFIX`] is about, answered where its tree is kept.
28pub const DEVICE_FILES_FOLDER: &str = "files.folder";
29
30/// Action id that starts an application in a workspace without showing it:
31/// an instance with no window where the provider runs one, ready for the
32/// actions of the app that show nothing. It answers with that instance.
33pub const APPLICATION_START_ACTION: &str = "start";
34
35/// Action id of a resource's link: a URL that opens it for the workspace's
36/// members, carrying no secret. A record's root always offers it.
37pub const SHARE_LINK_ACTION: &str = "share-link";
38
39/// Prefix of the provider token of one record of a workspace, served by a
40/// device that holds it whoever has it open: `record:<app id>:<record id>`
41/// (a provider serving its workspaces' records answers under these). Such a
42/// provider is the app's record, live, as a window of the app is.
43pub const RESOURCE_PROVIDER: &str = "record";
44
45/// Whether `provider` names one record of a workspace ([`RESOURCE_PROVIDER`]).
46#[must_use]
47pub fn is_resource_provider(provider: &str) -> bool {
48    provider
49        .strip_prefix(RESOURCE_PROVIDER)
50        .is_some_and(|rest| rest.starts_with(':'))
51}
52
53/// The app and record ids `provider` names, when it names one record of a
54/// workspace ([`RESOURCE_PROVIDER`]): `record:<app id>:<record id>`. App ids
55/// hold no colon; a record id may (`uuid:…`), and an empty one is the app's
56/// own first record.
57#[must_use]
58pub fn resource_parts(provider: &str) -> Option<(&str, &str)> {
59    provider
60        .strip_prefix(RESOURCE_PROVIDER)?
61        .strip_prefix(':')?
62        .split_once(':')
63        .filter(|(app, _)| !app.is_empty())
64}
65
66#[cfg(test)]
67mod tests {
68    #![allow(
69        clippy::unwrap_used,
70        clippy::panic,
71        reason = "in a test these are the assertion, not a hazard"
72    )]
73
74    use super::*;
75
76    /// A record provider names its app and record, whichever shape the
77    /// record id has; nothing else names either.
78    #[test]
79    fn a_record_provider_names_its_app_and_record() {
80        assert_eq!(
81            resource_parts("record:so.tilo.notes:uuid:7"),
82            Some(("so.tilo.notes", "uuid:7"))
83        );
84        assert_eq!(
85            resource_parts("record:so.tilo.chat:"),
86            Some(("so.tilo.chat", ""))
87        );
88        assert_eq!(resource_parts("record:so.tilo.chat"), None);
89        assert_eq!(resource_parts("record::main"), None);
90        assert_eq!(resource_parts("workspace:6b3f9a10"), None);
91        assert_eq!(resource_parts("4711"), None);
92        assert!(!is_resource_provider("4711"));
93        assert!(is_resource_provider("record:so.tilo.chat:"));
94    }
95}