hackmd_rs/
note.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    context::Context,
5    error::{Error, Result},
6    user::SimplifiedUser,
7};
8
9#[derive(Debug, Deserialize, PartialEq, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Note {
12    pub id: String,
13    pub title: String,
14    pub tags: Vec<String>,
15    pub created_at: usize,
16    // TODO: should be an enum, not enough documentation
17    pub publish_type: String,
18    pub published_at: Option<String>,
19    pub permalink: Option<String>,
20    pub short_id: String,
21    pub content: String,
22    pub last_changed_at: usize,
23    pub last_changed_user: Option<SimplifiedUser>,
24    pub user_path: String,
25    pub team_path: Option<String>,
26    pub read_permission: String,
27    pub write_permission: String,
28}
29
30impl Note {
31    pub async fn get(context: &Context, id: &str) -> Result<Note> {
32        let path = format!("notes/{id}");
33
34        context.get(&path).await
35    }
36
37    pub async fn update(context: &Context, id: &str, update: &NoteUpdate) -> Result<()> {
38        update.patch(context, id).await
39    }
40}
41
42#[derive(Debug, Deserialize, PartialEq)]
43pub struct Notes {
44    #[serde(flatten)]
45    notes: Vec<SimplifiedNote>,
46}
47
48impl Notes {
49    pub async fn all(context: &Context) -> Result<Notes> {
50        context
51            .client
52            .get(Context::make_url("notes"))
53            .header("Authorization", &context.bearer)
54            .send()
55            .await?
56            .json()
57            .await
58            .map_err(Error::from)
59    }
60}
61
62#[derive(Debug, Deserialize, PartialEq, Serialize)]
63#[serde(rename_all = "camelCase")]
64pub struct SimplifiedNote {
65    id: String,
66    title: String,
67    tags: Vec<String>,
68    created_at: usize,
69    // TODO: should be an enum, not enough documentation
70    publish_type: String,
71    published_at: Option<String>,
72    permalink: Option<String>,
73    short_id: String,
74    last_changed_at: usize,
75    last_changed_user: SimplifiedUser,
76    user_path: String,
77    team_path: Option<String>,
78    read_permission: String,
79    write_permission: String,
80}
81
82#[derive(Debug, Serialize)]
83#[serde(rename_all = "camelCase")]
84pub struct NoteUpdate {
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub content: Option<String>,
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub read_permission: Option<String>,
89}
90
91impl NoteUpdate {
92    pub async fn patch(&self, context: &Context, id: &str) -> Result<()> {
93        let path = format!("notes/{id}");
94
95        context.patch(&path, self).await
96    }
97}