jacquard_common/types/
xrpc.rs1use serde::{Deserialize, Serialize};
2use std::error::Error;
3use std::fmt::{self, Debug};
4
5use crate::IntoStatic;
6use crate::types::value::Data;
7
8#[derive(Debug, thiserror::Error, miette::Diagnostic)]
10pub enum EncodeError {
11 #[error("Failed to serialize query: {0}")]
13 Query(
14 #[from]
15 #[source]
16 serde_html_form::ser::Error,
17 ),
18 #[error("Failed to serialize JSON: {0}")]
20 Json(
21 #[from]
22 #[source]
23 serde_json::Error,
24 ),
25 #[error("Encoding error: {0}")]
27 Other(String),
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32pub enum XrpcMethod {
33 Query,
35 Procedure(&'static str),
37}
38
39impl XrpcMethod {
40 pub const fn as_str(&self) -> &'static str {
42 match self {
43 Self::Query => "GET",
44 Self::Procedure(_) => "POST",
45 }
46 }
47
48 pub const fn body_encoding(&self) -> Option<&'static str> {
50 match self {
51 Self::Query => None,
52 Self::Procedure(enc) => Some(enc),
53 }
54 }
55}
56
57pub trait XrpcRequest: Serialize {
64 const NSID: &'static str;
66
67 const METHOD: XrpcMethod;
69
70 const OUTPUT_ENCODING: &'static str;
72
73 type Output<'de>: Deserialize<'de> + IntoStatic;
75
76 type Err<'de>: Error + Deserialize<'de> + IntoStatic;
78
79 fn encode_body(&self) -> Result<Vec<u8>, EncodeError> {
83 Ok(serde_json::to_vec(self)?)
84 }
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
89#[serde(bound(deserialize = "'de: 'a"))]
90pub struct GenericError<'a>(Data<'a>);
91
92impl fmt::Display for GenericError<'_> {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 self.0.fmt(f)
95 }
96}
97
98impl Error for GenericError<'_> {}
99
100impl IntoStatic for GenericError<'_> {
101 type Output = GenericError<'static>;
102 fn into_static(self) -> Self::Output {
103 GenericError(self.0.into_static())
104 }
105}