use khive_types::Namespace;
use serde::{Deserialize, Serialize};
use crate::{ActorRef, GateContext, GateValidationError};
#[derive(Clone, Debug, Serialize)]
pub struct GateRequest {
pub actor: ActorRef,
pub namespace: Namespace,
pub verb: String,
pub args: serde_json::Value,
#[serde(default)]
pub context: GateContext,
}
#[derive(Deserialize)]
struct RawGateRequest {
actor: ActorRef,
namespace: Namespace,
verb: String,
args: serde_json::Value,
#[serde(default)]
context: GateContext,
}
impl TryFrom<RawGateRequest> for GateRequest {
type Error = GateValidationError;
fn try_from(raw: RawGateRequest) -> Result<Self, Self::Error> {
if raw.verb.is_empty() {
return Err(GateValidationError::EmptyVerb);
}
Ok(Self {
actor: raw.actor,
namespace: raw.namespace,
verb: raw.verb,
args: raw.args,
context: raw.context,
})
}
}
impl<'de> Deserialize<'de> for GateRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = RawGateRequest::deserialize(deserializer)?;
GateRequest::try_from(raw).map_err(serde::de::Error::custom)
}
}
impl GateRequest {
pub fn try_new(
actor: ActorRef,
namespace: Namespace,
verb: impl Into<String>,
args: serde_json::Value,
) -> Result<Self, GateValidationError> {
let verb = verb.into();
if verb.is_empty() {
return Err(GateValidationError::EmptyVerb);
}
Ok(Self {
actor,
namespace,
verb,
args,
context: GateContext::default(),
})
}
pub fn new(
actor: ActorRef,
namespace: Namespace,
verb: impl Into<String>,
args: serde_json::Value,
) -> Self {
Self::try_new(actor, namespace, verb, args)
.expect("GateRequest::new: verb must not be empty")
}
pub fn with_context(mut self, context: GateContext) -> Self {
self.context = context;
self
}
}