hocuspocus_rs_ws/
api_types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize)]
4pub struct NewDocResponse {
5    #[serde(rename = "docId")]
6    pub doc_id: String,
7}
8
9#[derive(Copy, Clone, Serialize, Deserialize, PartialEq)]
10pub enum Authorization {
11    #[serde(rename = "read-only")]
12    ReadOnly,
13    #[serde(rename = "full")]
14    Full,
15    #[serde(rename = "none")]
16    None,
17}
18
19impl Authorization {
20    fn full() -> Self {
21        Self::Full
22    }
23}
24
25#[derive(Deserialize)]
26pub struct AuthDocRequest {
27    #[serde(default = "Authorization::full")]
28    pub authorization: Authorization,
29    #[serde(rename = "userId")]
30    pub user_id: Option<String>,
31    #[serde(skip_serializing_if = "Option::is_none", rename = "validForSeconds")]
32    pub valid_for_seconds: Option<u64>,
33}
34
35impl Default for AuthDocRequest {
36    fn default() -> Self {
37        Self {
38            authorization: Authorization::Full,
39            user_id: None,
40            valid_for_seconds: None,
41        }
42    }
43}
44
45#[derive(Serialize, Deserialize)]
46pub struct ClientToken {
47    /// The URL compatible with the y-websocket provider. The provider will append
48    /// a document ID to this string and establish a WebSocket connection.
49    pub url: String,
50
51    /// The base URL for document-level endpoints.
52    #[serde(rename = "baseUrl")]
53    pub base_url: Option<String>,
54
55    /// The document ID.
56    #[serde(rename = "docId")]
57    pub doc_id: String,
58
59    /// An optional token that can be used to authenticate the client to the server.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub token: Option<String>,
62
63    /// The authorization level of the client.
64    #[serde(rename = "authorization")]
65    pub authorization: Authorization,
66}
67
68#[derive(Deserialize, Debug)]
69pub struct DocCreationRequest {
70    /// The ID of the document to create. If not provided, a random ID will be generated.
71    #[serde(skip_serializing_if = "Option::is_none", rename = "docId")]
72    pub doc_id: Option<String>,
73}
74
75/// Validate that the document name contains only alphanumeric characters, dashes, and underscores.
76/// This is the same alphabet used by nanoid when we generate a document name.
77pub fn validate_doc_name(doc_name: &str) -> bool {
78    if doc_name.is_empty() {
79        return false;
80    }
81    for c in doc_name.chars() {
82        if !c.is_ascii_alphanumeric() && c != '-' && c != '_' {
83            return false;
84        }
85    }
86    true
87}