use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct DeviceData {
pub serial: String,
pub name: String,
pub is_connected: bool,
pub manufacturer: Option<String>,
pub product: Option<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct LogRequest {
pub count: u32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct Log {
pub uuidv7: Uuid,
pub msg: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct LogRangeRequest {
pub uuid: Option<Uuid>,
pub unix_ms_ts: Option<u64>,
pub direction: Direction,
pub count: u32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub enum Direction {
Before,
After,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct TopicRequest {
pub path: String,
pub key: foreign::Key,
pub count: u32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct TopicMsg {
pub uuidv7: Uuid,
pub msg: serde_json::Value,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct TopicStreamRequest {
pub path: String,
pub key: foreign::Key,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub struct TopicStreamMsg {
pub stream_id: Uuid,
pub msg: serde_json::Value,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Hash, JsonSchema)]
pub enum TopicStreamResult {
Started(Uuid),
NoDeviceKnown,
DeviceDisconnected,
NoSuchTopic,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct ProxyRequest {
pub path: String,
pub req_key: foreign::Key,
pub resp_key: foreign::Key,
pub seq_no: u32,
pub body: serde_json::Value,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct ProxyResponseOk {
pub resp_key: foreign::Key,
pub seq_no: u32,
pub body: serde_json::Value,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub enum ProxyResponseError {
WireErr {
resp_key: foreign::Key,
seq_no: u32,
body: foreign::WireError,
},
OtherErr(String),
}
#[derive(Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct PublishRequest {
pub path: String,
pub topic_key: foreign::Key,
pub seq_no: u32,
pub body: serde_json::Value,
}
pub mod foreign {
use std::collections::HashSet;
use schema::OwnedNamedType;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
impl From<postcard_rpc::Key> for Key {
fn from(value: postcard_rpc::Key) -> Self {
Self(format!("{:016X}", u64::from_le_bytes(value.to_bytes())))
}
}
impl TryFrom<Key> for postcard_rpc::Key {
type Error = String;
fn try_from(value: Key) -> Result<Self, Self::Error> {
let Ok(val) = u64::from_str_radix(&value.0, 16) else {
return Err(value.0);
};
unsafe { Ok(postcard_rpc::Key::from_bytes(val.to_le_bytes())) }
}
}
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Hash, JsonSchema,
)]
pub struct Key(String);
#[derive(Serialize, Deserialize, Debug, PartialEq, JsonSchema)]
pub struct FrameTooLong {
pub len: u32,
pub max: u32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, JsonSchema)]
pub struct FrameTooShort {
pub len: u32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, JsonSchema)]
pub enum WireError {
FrameTooLong(FrameTooLong),
FrameTooShort(FrameTooShort),
DeserFailed,
SerFailed,
UnknownKey,
FailedToSpawn,
KeyTooSmall,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
pub struct SchemaReport {
pub types: HashSet<OwnedNamedType>,
pub topics_in: Vec<TopicReport>,
pub topics_out: Vec<TopicReport>,
pub endpoints: Vec<EndpointReport>,
}
impl From<postcard_rpc::host_client::SchemaReport> for SchemaReport {
fn from(value: postcard_rpc::host_client::SchemaReport) -> Self {
Self {
types: value.types.iter().map(Into::into).collect(),
topics_in: value.topics_in.into_iter().map(Into::into).collect(),
topics_out: value.topics_out.into_iter().map(Into::into).collect(),
endpoints: value.endpoints.into_iter().map(Into::into).collect(),
}
}
}
impl From<postcard_rpc::host_client::TopicReport> for TopicReport {
fn from(value: postcard_rpc::host_client::TopicReport) -> Self {
Self {
path: value.path,
key: value.key.into(),
ty: (&value.ty).into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
pub struct TopicReport {
pub path: String,
pub key: Key,
pub ty: OwnedNamedType,
}
impl From<postcard_rpc::host_client::EndpointReport> for EndpointReport {
fn from(value: postcard_rpc::host_client::EndpointReport) -> Self {
Self {
path: value.path,
req_key: value.req_key.into(),
req_ty: (&value.req_ty).into(),
resp_key: value.resp_key.into(),
resp_ty: (&value.resp_ty).into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
pub struct EndpointReport {
pub path: String,
pub req_key: Key,
pub req_ty: OwnedNamedType,
pub resp_key: Key,
pub resp_ty: OwnedNamedType,
}
pub mod schema {
use postcard_schema::schema::owned as real;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{boxed::Box, ops::Deref, string::String, vec::Vec};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct OwnedNamedType {
pub name: String,
pub ty: OwnedDataModelType,
}
impl From<&real::OwnedNamedType> for OwnedNamedType {
fn from(value: &real::OwnedNamedType) -> Self {
Self {
name: value.name.to_string(),
ty: (&value.ty).into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum OwnedDataModelType {
Bool,
I8,
U8,
I16,
I32,
I64,
I128,
U16,
U32,
U64,
U128,
Usize,
Isize,
F32,
F64,
Char,
String,
ByteArray,
Option(Box<OwnedNamedType>),
Unit,
UnitStruct,
NewtypeStruct(Box<OwnedNamedType>),
Seq(Box<OwnedNamedType>),
Tuple(Vec<OwnedNamedType>),
TupleStruct(Vec<OwnedNamedType>),
Map {
key: Box<OwnedNamedType>,
val: Box<OwnedNamedType>,
},
Struct(Vec<OwnedNamedValue>),
Enum(Vec<OwnedNamedVariant>),
Schema,
}
impl From<&real::OwnedDataModelType> for OwnedDataModelType {
fn from(other: &real::OwnedDataModelType) -> Self {
match other {
real::OwnedDataModelType::Bool => Self::Bool,
real::OwnedDataModelType::I8 => Self::I8,
real::OwnedDataModelType::U8 => Self::U8,
real::OwnedDataModelType::I16 => Self::I16,
real::OwnedDataModelType::I32 => Self::I32,
real::OwnedDataModelType::I64 => Self::I64,
real::OwnedDataModelType::I128 => Self::I128,
real::OwnedDataModelType::U16 => Self::U16,
real::OwnedDataModelType::U32 => Self::U32,
real::OwnedDataModelType::U64 => Self::U64,
real::OwnedDataModelType::U128 => Self::U128,
real::OwnedDataModelType::Usize => Self::Usize,
real::OwnedDataModelType::Isize => Self::Isize,
real::OwnedDataModelType::F32 => Self::F32,
real::OwnedDataModelType::F64 => Self::F64,
real::OwnedDataModelType::Char => Self::Char,
real::OwnedDataModelType::String => Self::String,
real::OwnedDataModelType::ByteArray => Self::ByteArray,
real::OwnedDataModelType::Option(o) => Self::Option(Box::new(o.deref().into())),
real::OwnedDataModelType::Unit => Self::Unit,
real::OwnedDataModelType::UnitStruct => Self::UnitStruct,
real::OwnedDataModelType::NewtypeStruct(nts) => {
Self::NewtypeStruct(Box::new(nts.deref().into()))
}
real::OwnedDataModelType::Seq(s) => Self::Seq(Box::new(s.deref().into())),
real::OwnedDataModelType::Tuple(t) => {
Self::Tuple(t.iter().map(|i| i.into()).collect())
}
real::OwnedDataModelType::TupleStruct(ts) => {
Self::TupleStruct(ts.iter().map(|i| i.into()).collect())
}
real::OwnedDataModelType::Map { key, val } => Self::Map {
key: Box::new(key.deref().into()),
val: Box::new(val.deref().into()),
},
real::OwnedDataModelType::Struct(s) => {
Self::Struct(s.iter().map(|i| i.into()).collect())
}
real::OwnedDataModelType::Enum(e) => {
Self::Enum(e.iter().map(|i| i.into()).collect())
}
real::OwnedDataModelType::Schema => Self::Schema,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum OwnedDataModelVariant {
UnitVariant,
NewtypeVariant(Box<OwnedNamedType>),
TupleVariant(Vec<OwnedNamedType>),
StructVariant(Vec<OwnedNamedValue>),
}
impl From<&real::OwnedDataModelVariant> for OwnedDataModelVariant {
fn from(value: &real::OwnedDataModelVariant) -> Self {
match value {
real::OwnedDataModelVariant::UnitVariant => Self::UnitVariant,
real::OwnedDataModelVariant::NewtypeVariant(d) => {
Self::NewtypeVariant(Box::new(d.deref().into()))
}
real::OwnedDataModelVariant::TupleVariant(d) => {
Self::TupleVariant(d.iter().map(|i| i.into()).collect())
}
real::OwnedDataModelVariant::StructVariant(d) => {
Self::StructVariant(d.iter().map(|i| i.into()).collect())
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct OwnedNamedValue {
pub name: String,
pub ty: OwnedNamedType,
}
impl From<&real::OwnedNamedValue> for OwnedNamedValue {
fn from(value: &real::OwnedNamedValue) -> Self {
Self {
name: value.name.to_string(),
ty: (&value.ty).into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct OwnedNamedVariant {
pub name: String,
pub ty: OwnedDataModelVariant,
}
impl From<&real::OwnedNamedVariant> for OwnedNamedVariant {
fn from(value: &real::OwnedNamedVariant) -> Self {
Self {
name: value.name.to_string(),
ty: (&value.ty).into(),
}
}
}
}
}