use std::collections::BTreeMap;
use lgwks_std::json::serde::{Deserialize, Serialize};
use crate::canvas_protocol::SessionInvariant;
mod descriptor;
mod entries;
mod envelopes;
mod patterns;
mod prediction;
pub use descriptor::{WidgetDescriptor, WidgetSurface};
pub use entries::{Attribution, AttributionKind, GovernanceEntry};
pub(crate) use envelopes::valid_signal_kind;
pub use envelopes::{
EnvelopeError, IntentEnvelope, PartialIntent, validate_envelope, validate_session_invariant,
};
pub use patterns::{PayloadPredicate, SignalPattern};
pub use prediction::PredictionEnvelope;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
pub enum SourceKind {
Widget,
Ml,
System,
User,
}
impl SourceKind {
#[must_use]
pub const fn all() -> [Self; 4] {
[Self::Widget, Self::Ml, Self::System, Self::User]
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
#[serde(untagged)]
pub enum JsonValue {
String(String),
Number(f64),
Bool(bool),
Null,
Array(Vec<JsonValue>),
Object(BTreeMap<String, JsonValue>),
}
impl JsonValue {
#[must_use]
pub const fn string_value(&self) -> Option<&String> {
match *self {
Self::String(ref text) => Some(text),
_ => None,
}
}
#[must_use]
pub const fn array_value(&self) -> Option<&Vec<JsonValue>> {
match *self {
Self::Array(ref items) => Some(items),
_ => None,
}
}
#[must_use]
pub const fn object_value(&self) -> Option<&BTreeMap<String, JsonValue>> {
match *self {
Self::Object(ref map) => Some(map),
_ => None,
}
}
#[must_use]
pub fn from_json(value: &lgwks_std::json::Value) -> Self {
match *value {
lgwks_std::json::Value::Null => Self::Null,
lgwks_std::json::Value::Bool(flag) => Self::Bool(flag),
lgwks_std::json::Value::Number(ref number) => {
let Some(value) = number.as_f64() else {
return Self::Null;
};
Self::Number(value)
}
lgwks_std::json::Value::String(ref text) => Self::String(String::from(text.as_str())),
lgwks_std::json::Value::Array(ref items) => {
Self::Array(items.iter().map(Self::from_json).collect())
}
lgwks_std::json::Value::Object(ref map) => Self::Object(
map.iter()
.map(|(key, item)| (String::from(key), Self::from_json(item)))
.collect(),
),
_ => Self::Null,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
pub struct SignalEnvelope {
signal_id: String,
kind: String,
source_kind: SourceKind,
source_id: String,
session_invariant: SessionInvariant,
emitted_at: String,
payload: JsonValue,
}
impl SignalEnvelope {
#[must_use]
pub fn new(
signal_id: String,
kind: String,
source_kind: SourceKind,
source_id: String,
session_invariant: SessionInvariant,
payload: JsonValue,
emitted_at: String,
) -> Self {
Self {
signal_id,
kind,
source_kind,
source_id,
session_invariant,
payload,
emitted_at,
}
}
#[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 source_kind(&self) -> SourceKind {
self.source_kind
}
#[must_use]
pub fn source_id(&self) -> &str {
&self.source_id
}
#[must_use]
pub const fn session_invariant(&self) -> &SessionInvariant {
&self.session_invariant
}
#[must_use]
pub fn emitted_at(&self) -> &str {
&self.emitted_at
}
#[must_use]
pub const fn payload(&self) -> &JsonValue {
&self.payload
}
}