Files
polyscribe/crates/polyscribe-protocol/src/lib.rs

61 lines
1.3 KiB
Rust

use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
pub id: String,
pub method: String,
pub params: Option<Value>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Response {
pub id: String,
pub result: Option<Value>,
pub error: Option<ErrorObj>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorObj {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "event", content = "data")]
pub enum ProgressEvent {
Started,
Message(String),
Percent(f32),
Finished,
}
impl Response {
pub fn ok(id: impl Into<String>, result: Value) -> Self {
Self {
id: id.into(),
result: Some(result),
error: None,
}
}
pub fn err(
id: impl Into<String>,
code: i32,
message: impl Into<String>,
data: Option<Value>,
) -> Self {
Self {
id: id.into(),
result: None,
error: Some(ErrorObj {
code,
message: message.into(),
data,
}),
}
}
}