use serde_json::{Value, json};
use crate::configuration::{ConfigurationError, SessionType};
use super::RelayError;
pub(super) fn map_config(error: ConfigurationError) -> RelayError {
match error {
ConfigurationError::UnknownBundle { bundle_name, path } => relay_error(
"validation_unknown_bundle",
"bundle is not configured",
Some(json!({"bundle_name": bundle_name, "path": path})),
),
ConfigurationError::InvalidConfiguration { path, message } => relay_error(
"internal_unexpected_failure",
"bundle configuration is invalid",
Some(json!({"path": path, "cause": message})),
),
ConfigurationError::InvalidGroupName { path, group_name } => relay_error(
"validation_invalid_group_name",
"bundle configuration uses invalid group name",
Some(json!({"path": path, "group_name": group_name})),
),
ConfigurationError::ReservedGroupName { path, group_name } => relay_error(
"validation_reserved_group_name",
"bundle configuration uses reserved group name",
Some(json!({"path": path, "group_name": group_name})),
),
ConfigurationError::AmbiguousSender {
working_directory,
matches,
} => relay_error(
"validation_unknown_sender",
"sender association is ambiguous",
Some(json!({"working_directory": working_directory, "matches": matches})),
),
ConfigurationError::Io { context, source } => relay_error(
"internal_unexpected_failure",
"bundle configuration could not be loaded",
Some(json!({"context": context, "cause": source.to_string()})),
),
}
}
pub(super) fn relay_error(code: &str, message: &str, details: Option<Value>) -> RelayError {
RelayError {
code: code.to_string(),
message: message.to_string(),
details,
}
}
pub(super) const VALIDATION_UNSUPPORTED_OPERATION: &str = "validation_unsupported_operation";
pub(super) fn unsupported_operation(
target_session: &str,
session_type: SessionType,
capability_flag: &str,
) -> RelayError {
let mut details = serde_json::Map::new();
details.insert("target_session".to_string(), json!(target_session));
details.insert("session_type".to_string(), json!(session_type));
details.insert(capability_flag.to_string(), json!(false));
relay_error(
VALIDATION_UNSUPPORTED_OPERATION,
"target transport does not support this operation",
Some(Value::Object(details)),
)
}
pub(super) fn session_type_not_implemented(
session_id: &str,
session_type: SessionType,
) -> RelayError {
relay_error(
"runtime_session_type_not_implemented",
"session type delivery is not yet implemented",
Some(json!({
"session_id": session_id,
"session_type": session_type,
})),
)
}
pub(super) fn map_tui_config(error: ConfigurationError) -> RelayError {
match error {
ConfigurationError::InvalidConfiguration { path, message } => relay_error(
"validation_invalid_arguments",
"tui configuration is invalid",
Some(json!({"path": path, "cause": message})),
),
ConfigurationError::Io { context, source } => relay_error(
"validation_invalid_arguments",
"failed to load tui configuration",
Some(json!({"context": context, "cause": source.to_string()})),
),
other => relay_error(
"validation_invalid_arguments",
"failed to load tui configuration",
Some(json!({"cause": other.to_string()})),
),
}
}