use crate::api::ActionAddress;
use super::{
APPLICATION_OPEN_ACTION, APPLICATION_RESOURCE_PREFIX, RemoteActionTarget, workspace_provider,
};
#[must_use]
pub fn valid_file_name(name: &str) -> bool {
!name.is_empty()
&& name.len() <= 255
&& name != "."
&& name != ".."
&& !name
.chars()
.any(|c| c.is_control() || c == '/' || c == '\\')
}
#[must_use]
pub fn application_open_target(workspace: &str, app_id: &str) -> RemoteActionTarget {
RemoteActionTarget {
provider: workspace_provider(workspace),
address: ActionAddress::new(
format!("{APPLICATION_RESOURCE_PREFIX}{app_id}"),
APPLICATION_OPEN_ACTION,
),
}
}
pub const APPLICATION_PROVIDER: &str = "application";
#[must_use]
pub fn application_provider_token(workspace: &str, app_id: &str) -> String {
format!("{APPLICATION_PROVIDER}:{workspace}:{app_id}")
}
#[must_use]
pub fn application_action_target(
workspace: &str,
app_id: &str,
resource: impl Into<String>,
action: impl Into<String>,
) -> RemoteActionTarget {
RemoteActionTarget {
provider: application_provider_token(workspace, app_id),
address: ActionAddress::new(resource, action),
}
}
#[must_use]
pub fn application_resource_target(
workspace: &str,
app_id: &str,
record: &str,
resource: impl Into<String>,
action: impl Into<String>,
) -> RemoteActionTarget {
let mut target = application_action_target(workspace, app_id, resource, action);
target.provider = format!("{}:{record}", target.provider);
target
}
#[must_use]
pub fn application_provider(provider: &str) -> Option<(&str, &str)> {
application_parts(provider).map(|(workspace, app_id, _)| (workspace, app_id))
}
#[must_use]
pub fn application_provider_resource(provider: &str) -> Option<&str> {
application_parts(provider).and_then(|(_, _, record)| record)
}
fn application_parts(provider: &str) -> Option<(&str, &str, Option<&str>)> {
let (workspace, rest) = provider
.strip_prefix(APPLICATION_PROVIDER)?
.strip_prefix(':')?
.split_once(':')?;
let (app_id, record) = match rest.split_once(':') {
Some((app_id, record)) => (app_id, Some(record)),
None => (rest, None),
};
(!workspace.is_empty() && !app_id.is_empty() && record.is_none_or(|record| !record.is_empty()))
.then_some((workspace, app_id, record))
}