use lgwks_std::json::serde::{Deserialize, Serialize};
use super::super::ensure;
use super::super::time::parse_millis;
use super::{JsonValue, SignalEnvelope, SourceKind};
use crate::canvas_protocol::SessionInvariant;
#[derive(Debug, Clone, PartialEq, Eq, lgwks_macros::Error)]
#[non_exhaustive]
pub enum EnvelopeError {
#[error("signal_id is not a UUID")]
InvalidSignalId,
#[error("kind is invalid")]
InvalidKind,
#[error("emitted_at is not a timestamp")]
InvalidTimestamp,
#[error("session invariant field is invalid: {0}")]
InvalidInvariant(&'static str),
}
pub(crate) fn valid_signal_kind(kind: &str) -> bool {
if kind.is_empty() || kind.chars().count() > 128 {
return false;
}
if kind.starts_with('.') || kind.ends_with('.') || kind.contains("..") {
return false;
}
!kind.chars().any(char::is_control)
}
fn is_valid_uuid(text: &str) -> bool {
lgwks_std::id::Uuid::parse(text).is_ok()
}
pub fn validate_envelope(envelope: &SignalEnvelope) -> Result<(), EnvelopeError> {
ensure(is_valid_uuid(&envelope.signal_id), || {
EnvelopeError::InvalidSignalId
})?;
ensure(valid_signal_kind(&envelope.kind), || {
EnvelopeError::InvalidKind
})?;
ensure(envelope.emitted_at.ends_with('Z'), || {
EnvelopeError::InvalidTimestamp
})?;
ensure(parse_millis(&envelope.emitted_at).is_ok(), || {
EnvelopeError::InvalidTimestamp
})?;
validate_session_invariant(&envelope.session_invariant)
}
pub fn validate_session_invariant(session: &SessionInvariant) -> Result<(), EnvelopeError> {
let fields = [
("work_object_id", is_valid_uuid(session.work_object_id())),
("session_id", is_valid_uuid(session.session_id())),
(
"policy_bundle_id",
is_valid_uuid(session.policy_bundle_id()),
),
("user_sub", is_valid_uuid(session.user_sub())),
("tenant_id", !session.tenant_id().is_empty()),
];
fields
.into_iter()
.try_for_each(|(name, valid)| ensure(valid, || EnvelopeError::InvalidInvariant(name)))
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
pub struct PartialIntent {
kind: String,
payload: JsonValue,
text: String,
}
impl PartialIntent {
#[must_use]
pub fn new(kind: String, payload: JsonValue, text: String) -> Self {
Self {
kind,
payload,
text,
}
}
#[must_use]
pub fn kind(&self) -> &str {
&self.kind
}
#[must_use]
pub const fn payload(&self) -> &JsonValue {
&self.payload
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
pub struct IntentEnvelope {
signal_id: String,
kind: String,
#[serde(deserialize_with = "check_user_source")]
source_kind: SourceKind,
source_id: String,
payload: JsonValue,
session_invariant: SessionInvariant,
emitted_at: String,
raw_text: Option<String>,
resolved_kind: Option<String>,
}
fn check_user_source<'de, D>(deserializer: D) -> Result<SourceKind, D::Error>
where
D: lgwks_std::json::serde::Deserializer<'de>,
{
let kind = SourceKind::deserialize(deserializer)?;
ensure(kind == SourceKind::User, || {
<D::Error as lgwks_std::json::serde::de::Error>::custom(
"IntentEnvelope sourceKind must be user",
)
})?;
Ok(kind)
}
impl IntentEnvelope {
#[must_use]
pub fn new(
kind: String,
signal_id: String,
source_kind: SourceKind,
source_id: String,
session_invariant: SessionInvariant,
emitted_at: String,
payload: JsonValue,
) -> Self {
Self {
kind,
signal_id,
source_kind,
source_id,
session_invariant,
emitted_at,
payload,
raw_text: None,
resolved_kind: None,
}
}
pub fn set_raw_text(&mut self, raw_text: Option<String>) {
self.raw_text = raw_text;
}
pub fn set_resolved_kind(&mut self, resolved_kind: Option<String>) {
self.resolved_kind = resolved_kind;
}
#[must_use]
pub fn signal_id(&self) -> &str {
&self.signal_id
}
#[must_use]
pub fn kind(&self) -> &str {
&self.kind
}
#[must_use]
pub const fn payload(&self) -> &JsonValue {
&self.payload
}
#[must_use]
pub fn emitted_at(&self) -> &str {
&self.emitted_at
}
#[must_use]
pub const fn session_invariant(&self) -> &SessionInvariant {
&self.session_invariant
}
#[must_use]
pub fn source_id(&self) -> &str {
&self.source_id
}
#[must_use]
pub const fn source_kind(&self) -> SourceKind {
self.source_kind
}
#[must_use]
pub const fn raw_text(&self) -> Option<&String> {
self.raw_text.as_ref()
}
#[must_use]
pub const fn resolved_kind(&self) -> Option<&String> {
self.resolved_kind.as_ref()
}
}