1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::{
    fmt::{self, Display},
    ops::{Range, RangeFrom, RangeTo},
    path::PathBuf,
};

use chrono::NaiveDateTime;
use image::DynamicImage;
use url::Url;

use crate::Error;

/// Logged-in user information
#[must_use]
#[derive(Debug)]
pub struct UserInfo {
    /// User's nickname
    pub nickname: String,
    /// User's avatar
    pub avatar: Option<Url>,
}

/// Novel information
#[must_use]
#[derive(Debug, Default)]
pub struct NovelInfo {
    /// Novel id
    pub id: u32,
    /// Novel name
    pub name: String,
    /// Author name
    pub author_name: String,
    /// Url of the novel cover
    pub cover_url: Option<Url>,
    /// Novel introduction
    pub introduction: Option<Vec<String>>,
    /// Novel word count
    pub word_count: Option<u32>,
    /// Is the novel a VIP
    pub is_vip: Option<bool>,
    /// Is the novel finished
    pub is_finished: Option<bool>,
    /// Novel creation time
    pub create_time: Option<NaiveDateTime>,
    /// Novel last update time
    pub update_time: Option<NaiveDateTime>,
    /// Novel category
    pub category: Option<Category>,
    /// Novel tags
    pub tags: Option<Vec<Tag>>,
}

impl PartialEq for NovelInfo {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

/// Novel category
#[must_use]
#[derive(Debug, Clone, PartialEq)]
pub struct Category {
    /// Category id
    pub id: Option<u16>,
    /// Parent category id
    pub parent_id: Option<u16>,
    /// Category name
    pub name: String,
}

impl Display for Category {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

/// Novel tag
#[must_use]
#[derive(Debug, Clone, PartialEq)]
pub struct Tag {
    /// Tag id
    pub id: Option<u16>,
    /// Tag name
    pub name: String,
}

impl Display for Tag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

/// Volume information
pub type VolumeInfos = Vec<VolumeInfo>;

/// Volume information
#[must_use]
#[derive(Debug)]
pub struct VolumeInfo {
    /// Volume title
    pub title: String,
    /// Chapter information
    pub chapter_infos: Vec<ChapterInfo>,
}

/// Chapter information
#[must_use]
#[derive(Debug, Default)]
pub struct ChapterInfo {
    /// Novel id
    pub novel_id: Option<u32>,
    /// Chapter id
    pub id: u32,
    /// Chapter title
    pub title: String,
    /// Whether this chapter can only be read by VIP users
    pub is_vip: Option<bool>,
    /// Chapter price
    pub price: Option<u16>,
    /// Is the chapter accessible
    pub payment_required: Option<bool>,
    /// Is the chapter valid
    pub is_valid: Option<bool>,
    /// Word count
    pub word_count: Option<u32>,
    /// Chapter creation time
    pub create_time: Option<NaiveDateTime>,
    /// Chapter last update time
    pub update_time: Option<NaiveDateTime>,
}

impl ChapterInfo {
    /// Is this chapter available
    pub fn payment_required(&self) -> bool {
        !self.payment_required.as_ref().is_some_and(|x| !x)
    }

    /// Is this chapter valid
    pub fn is_valid(&self) -> bool {
        !self.is_valid.as_ref().is_some_and(|x| !x)
    }

    /// Is this chapter available for download
    pub fn can_download(&self) -> bool {
        !self.payment_required() && self.is_valid()
    }
}

/// Content information
pub type ContentInfos = Vec<ContentInfo>;

/// Content information
#[must_use]
#[derive(Debug)]
pub enum ContentInfo {
    /// Text content
    Text(String),
    /// Image content
    Image(Url),
}

/// Options used by the search
#[derive(Debug, Default)]
pub struct Options {
    /// Keyword
    pub keyword: Option<String>,
    /// Is it finished
    pub is_finished: Option<bool>,
    /// Whether this chapter can only be read by VIP users
    pub is_vip: Option<bool>,
    /// Category
    pub category: Option<Category>,
    /// Included tags
    pub tags: Option<Vec<Tag>>,
    /// Excluded tags
    pub excluded_tags: Option<Vec<Tag>>,
    /// The number of days since the last update
    pub update_days: Option<u8>,
    /// Word count
    pub word_count: Option<WordCountRange>,
}

/// Word count range
#[derive(Debug)]
pub enum WordCountRange {
    /// Set minimum and maximum word count
    Range(Range<u32>),
    /// Set minimum word count
    RangeFrom(RangeFrom<u32>),
    /// Set maximum word count
    RangeTo(RangeTo<u32>),
}

/// Traits that abstract client behavior
#[trait_variant::make(Send)]
pub trait Client {
    /// set proxy
    fn proxy(&mut self, proxy: Url);

    /// Do not use proxy (environment variables used to set proxy are ignored)
    fn no_proxy(&mut self);

    /// Set the certificate path for use with packet capture tools
    fn cert(&mut self, cert_path: PathBuf);

    /// Stop the client, save the data
    async fn shutdown(&self) -> Result<(), Error>;

    /// Add cookie
    async fn add_cookie(&self, cookie_str: &str, url: &Url) -> Result<(), Error>;

    /// Login in
    async fn log_in(&self, username: String, password: Option<String>) -> Result<(), Error>;

    /// Check if you are logged in
    async fn logged_in(&self) -> Result<bool, Error>;

    /// Get the information of the logged-in user
    async fn user_info(&self) -> Result<UserInfo, Error>;

    /// Get user's existing money
    async fn money(&self) -> Result<u32, Error>;

    /// Sign in
    async fn sign_in(&self) -> Result<(), Error>;

    /// Get the favorite novel of the logged-in user and return the novel id
    async fn bookshelf_infos(&self) -> Result<Vec<u32>, Error>;

    /// Get Novel Information
    async fn novel_info(&self, id: u32) -> Result<Option<NovelInfo>, Error>;

    /// Get volume Information
    async fn volume_infos(&self, id: u32) -> Result<Option<VolumeInfos>, Error>;

    /// Get content Information
    async fn content_infos(&self, info: &ChapterInfo) -> Result<ContentInfos, Error>;

    /// Buy chapter
    async fn buy_chapter(&self, info: &ChapterInfo) -> Result<(), Error>;

    /// Download image
    async fn image(&self, url: &Url) -> Result<DynamicImage, Error>;

    /// Get all categories
    async fn categories(&self) -> Result<&Vec<Category>, Error>;

    /// Get all tags
    async fn tags(&self) -> Result<&Vec<Tag>, Error>;

    /// Search all matching novels
    async fn search_infos(
        &self,
        option: &Options,
        page: u16,
        size: u16,
    ) -> Result<Option<Vec<u32>>, Error>;
}