use crate::geom::{DisplayInfo, LogicalPoint};
use crate::ids::{AnnotationId, SessionId};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DaemonMessage {
Ack(Ack),
Invalidated(Invalidated),
Error(ProtocolError),
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Ack {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<SessionId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub annotation_id: Option<AnnotationId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resolved_coords: Option<LogicalPoint>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub confidence: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub display: Option<DisplayInfo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub activated: Option<Activated>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub appeared: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Activated {
pub app: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bundle_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
}
impl Ack {
pub fn session(session_id: SessionId) -> Self {
Self {
session_id: Some(session_id),
..Self::default()
}
}
pub fn annotation(annotation_id: AnnotationId) -> Self {
Self {
annotation_id: Some(annotation_id),
..Self::default()
}
}
#[must_use]
pub fn with_display(mut self, display: DisplayInfo) -> Self {
self.display = Some(display);
self
}
#[must_use]
pub fn with_resolution(mut self, coords: LogicalPoint, confidence: f64) -> Self {
self.resolved_coords = Some(coords);
self.confidence = Some(confidence);
self
}
pub fn appeared(appeared: bool) -> Self {
Self {
appeared: Some(appeared),
..Self::default()
}
}
pub fn activated(app: Activated) -> Self {
Self {
activated: Some(app),
..Self::default()
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Invalidated {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub annotation_id: Option<AnnotationId>,
pub reason: InvalidationReason,
}
impl Invalidated {
pub fn one(annotation_id: AnnotationId, reason: InvalidationReason) -> Self {
Self {
annotation_id: Some(annotation_id),
reason,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvalidationReason {
Scroll,
DisplayChange,
SessionEnd,
Ttl,
Cleared,
#[serde(untagged)]
Unknown(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolError {
pub code: ErrorCode,
pub msg: String,
}
impl ProtocolError {
pub fn new(code: ErrorCode, msg: impl Into<String>) -> Self {
Self {
code,
msg: msg.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorCode {
BadSchema,
UnknownType,
PayloadTooLarge,
NoResolver,
NotPermitted,
ResolveFailed,
UnknownDisplay,
NotOwner,
VersionUnsupported,
#[serde(untagged)]
Unknown(String),
}
impl ErrorCode {
pub fn as_str(&self) -> &str {
match self {
Self::BadSchema => "bad_schema",
Self::UnknownType => "unknown_type",
Self::PayloadTooLarge => "payload_too_large",
Self::NoResolver => "no_resolver",
Self::NotPermitted => "not_permitted",
Self::ResolveFailed => "resolve_failed",
Self::UnknownDisplay => "unknown_display",
Self::NotOwner => "not_owner",
Self::VersionUnsupported => "version_unsupported",
Self::Unknown(s) => s,
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown_codes_survive_a_round_trip() {
let json = r#""teleported_into_the_sun""#;
let code: ErrorCode = serde_json::from_str(json).unwrap();
assert_eq!(code, ErrorCode::Unknown("teleported_into_the_sun".into()));
assert_eq!(serde_json::to_string(&code).unwrap(), json);
}
#[test]
fn known_codes_take_priority_over_the_fallback() {
assert_eq!(
serde_json::from_str::<ErrorCode>(r#""not_owner""#).unwrap(),
ErrorCode::NotOwner
);
assert_eq!(
serde_json::from_str::<InvalidationReason>(r#""display_change""#).unwrap(),
InvalidationReason::DisplayChange
);
}
}