use aion_core::{Payload, WorkflowId};
use aion_proto::{
WireError,
convert::{ProtoPayload, decode_core_value, encode_event},
};
pub(super) fn required_workflow_id(
id: Option<aion_proto::ProtoWorkflowId>,
) -> Result<WorkflowId, WireError> {
id.ok_or_else(|| WireError::invalid_input("workflow_id is required and was not supplied"))?
.try_into()
}
pub(super) fn required_payload(payload: Option<ProtoPayload>) -> Result<Payload, WireError> {
payload
.ok_or_else(|| {
WireError::invalid_input(
"payload is required and was not supplied (a JSON null is read as absent; send \
{} or the declared shape)",
)
})?
.try_into()
}
pub(super) fn optional_payload(payload: Option<ProtoPayload>) -> Result<Payload, WireError> {
payload.map_or_else(|| Ok(Payload::json_null()), TryInto::try_into)
}
pub(super) fn decode_list_request(
request: Option<&aion_proto::WireEnvelope>,
) -> Result<aion_core::WorkflowListRequest, WireError> {
request.map_or_else(
|| {
Err(WireError::invalid_input(
"list request is missing: a list names its filter, sort, cursor, and limit",
))
},
decode_core_value,
)
}
pub(super) fn encode_history(
include_history: bool,
namespace: &str,
history: &[aion_core::Event],
) -> Result<Vec<aion_proto::WireEnvelope>, WireError> {
if include_history {
history
.iter()
.map(|event| encode_event(namespace.to_owned(), None, event))
.collect()
} else {
Ok(Vec::new())
}
}