asimov_readwise_module/api/
types.rs

1// This is free and unencumbered software released into the public domain.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct PaginatedResponse<T> {
7    pub count: Option<u32>,
8    pub next: Option<String>,
9    pub previous: Option<String>,
10    pub results: Option<Vec<T>>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct HighlightRequest {
15    pub text: String,
16    pub title: Option<String>,
17    pub author: Option<String>,
18    pub source_url: Option<String>,
19    pub source_type: Option<String>,
20    pub category: Option<String>,
21    pub note: Option<String>,
22    pub location: Option<i32>,
23    pub location_type: Option<String>,
24    pub highlighted_at: Option<String>,
25    pub highlight_url: Option<String>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct Highlight {
30    pub id: Option<u64>,
31    pub title: Option<String>,
32    pub author: Option<String>,
33    pub category: Option<String>,
34    pub source: Option<String>,
35    pub num_highlights: Option<u32>,
36    pub last_highlight_at: Option<String>,
37    pub updated: Option<String>,
38    pub cover_image_url: Option<String>,
39    pub highlights_url: Option<String>,
40    pub source_url: Option<String>,
41    pub modified_highlights: Option<Vec<u64>>,
42    pub text: Option<String>,
43    pub source_type: Option<String>,
44    pub note: Option<String>,
45    pub location: Option<i32>,
46    pub location_type: Option<String>,
47    pub highlighted_at: Option<String>,
48    pub highlight_url: Option<String>,
49}
50
51pub type HighlightsResponse = PaginatedResponse<Highlight>;
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct Book {
55    pub id: Option<u64>,
56    pub title: Option<String>,
57    pub author: Option<String>,
58    pub category: Option<String>,
59    pub source: Option<String>,
60    pub num_highlights: Option<u32>,
61    pub last_highlight_at: Option<String>,
62    pub updated: Option<String>,
63    pub cover_image_url: Option<String>,
64    pub highlights_url: Option<String>,
65    pub source_url: Option<String>,
66    pub asin: Option<String>,
67    pub tags: Option<Vec<String>>,
68    pub document_note: Option<String>,
69}
70
71pub type BookListResponse = PaginatedResponse<Book>;
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct Tag {
75    pub id: Option<u64>,
76    pub name: Option<String>,
77    pub created_at: Option<String>,
78    pub updated_at: Option<String>,
79}
80
81pub type TagsResponse = PaginatedResponse<Tag>;
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct SimpleTag {
85    pub name: Option<String>,
86    pub updated: Option<i64>,
87    pub count: Option<u32>,
88}
89
90pub type SimpleTagsResponse = Vec<SimpleTag>;
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
93#[serde(untagged)]
94pub enum ApiResponse {
95    Highlights(HighlightsResponse),
96    BookList(BookListResponse),
97    Tags(TagsResponse),
98    SimpleTags(SimpleTagsResponse),
99}
100
101pub use Book as BookResponse;
102pub use Highlight as HighlightResponse;
103
104#[derive(Debug, Clone, PartialEq, Eq, Hash)]
105pub enum ReadwiseType {
106    Highlights,
107    Booklist,
108    Tags,
109}
110
111impl ReadwiseType {
112    pub const HIGHLIGHTS_ID: &'static str = "readwise-highlights";
113    pub const BOOKLIST_ID: &'static str = "readwise-booklist";
114    pub const TAGS_ID: &'static str = "readwise-tags";
115
116    pub fn as_str(&self) -> &'static str {
117        match self {
118            ReadwiseType::Highlights => Self::HIGHLIGHTS_ID,
119            ReadwiseType::Booklist => Self::BOOKLIST_ID,
120            ReadwiseType::Tags => Self::TAGS_ID,
121        }
122    }
123}