booru-rs 0.3.0

An async Booru client for Rust
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Client implementations for various booru sites.
//!
//! This module provides the [`Client`] trait and [`ClientBuilder`] for constructing
//! and using booru API clients.
//!
//! # Available Clients
//!
//! - [`DanbooruClient`] — For [danbooru.donmai.us](https://danbooru.donmai.us) (2 tag limit)
//! - [`GelbooruClient`] — For [gelbooru.com](https://gelbooru.com) (unlimited tags)
//! - [`SafebooruClient`] — For [safebooru.org](https://safebooru.org) (unlimited tags, SFW only)
//!
//! # Example
//!
//! ```no_run
//! use booru_rs::prelude::*;
//!
//! # async fn example() -> Result<()> {
//! // Using the builder pattern
//! let posts = GelbooruClient::builder()
//!     .tags(["cat_ears", "blue_eyes"])?
//!     .rating(GelbooruRating::General)
//!     .sort(Sort::Score)
//!     .limit(10)
//!     .build()
//!     .get()
//!     .await?;
//!
//! // Get a specific post by ID
//! let post = DanbooruClient::builder()
//!     .build()
//!     .get_by_id(12345)
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Custom HTTP Client
//!
//! By default, all clients share a connection-pooled HTTP client. You can provide
//! your own client for custom configuration:
//!
//! ```no_run
//! use booru_rs::prelude::*;
//!
//! # fn example() -> Result<()> {
//! let custom_client = reqwest::Client::builder()
//!     .timeout(std::time::Duration::from_secs(60))
//!     .build()
//!     .unwrap();
//!
//! let client = GelbooruClient::builder()
//!     .tag("nature")?
//!     .build();
//!
//! // Or use with_client to create a builder with a custom HTTP client:
//! let client = <GelbooruClient as Client>::builder()
//!     .tag("nature")?
//!     .build();
//! # Ok(())
//! # }
//! ```

use std::sync::LazyLock;
use std::time::Duration;

use crate::error::{BooruError, Result};

#[cfg(feature = "danbooru")]
pub mod danbooru;
#[cfg(feature = "gelbooru")]
pub mod gelbooru;
pub mod generic;
#[cfg(feature = "rule34")]
pub mod rule34;
#[cfg(feature = "safebooru")]
pub mod safebooru;

/// Shared HTTP client with connection pooling and timeouts.
///
/// This client is lazily initialized and reused across all requests
/// for better performance.
static SHARED_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
    reqwest::Client::builder()
        .timeout(Duration::from_secs(30))
        .connect_timeout(Duration::from_secs(10))
        .pool_max_idle_per_host(10)
        .pool_idle_timeout(Duration::from_secs(30))
        .build()
        .expect("Failed to create HTTP client")
});

/// Returns a reference to the shared HTTP client.
#[inline]
pub fn shared_client() -> &'static reqwest::Client {
    &SHARED_CLIENT
}

/// Builder for constructing booru API clients.
///
/// This builder allows you to configure various options before
/// creating a client to query a booru site.
///
/// # Example
///
/// ```no_run
/// use booru_rs::danbooru::{DanbooruClient, DanbooruRating};
/// use booru_rs::client::Client;
///
/// # async fn example() -> booru_rs::error::Result<()> {
/// let client = DanbooruClient::builder()
///     .tag("cat_ears")?
///     .rating(DanbooruRating::General)
///     .limit(10)
///     .build();
///
/// let posts = client.get().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ClientBuilder<T: Client> {
    pub(crate) client: reqwest::Client,
    pub(crate) key: Option<String>,
    pub(crate) user: Option<String>,
    pub(crate) tags: Vec<String>,
    pub(crate) limit: u32,
    pub(crate) url: String,
    pub(crate) page: u32,
    _marker: std::marker::PhantomData<T>,
}

impl<T: Client> Clone for ClientBuilder<T> {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            key: self.key.clone(),
            user: self.user.clone(),
            tags: self.tags.clone(),
            limit: self.limit,
            url: self.url.clone(),
            page: self.page,
            _marker: std::marker::PhantomData,
        }
    }
}

/// Core trait for booru API clients.
///
/// This trait defines the interface that all booru clients must implement.
/// It provides compile-time type safety for client-specific features like
/// ratings and tag limits.
///
/// # Associated Types
///
/// - `Post`: The post type returned by this client
/// - `Rating`: The rating type specific to this booru site
///
/// # Associated Constants
///
/// - `URL`: The base URL for the API
/// - `SORT`: The prefix for sort/order tags
/// - `MAX_TAGS`: Optional limit on the number of tags per query
pub trait Client: From<ClientBuilder<Self>> + Sized + Send + Sync {
    /// The post type returned by this client.
    type Post: Send;

    /// The rating type for this booru site.
    type Rating: Into<String> + Send;

    /// Base URL for the booru API.
    const URL: &'static str;

    /// Prefix used for sorting tags (e.g., "order:" or "sort:").
    const SORT: &'static str;

    /// Maximum number of tags allowed per query, or `None` for unlimited.
    const MAX_TAGS: Option<usize>;

    /// Creates a new builder for this client.
    #[must_use]
    fn builder() -> ClientBuilder<Self> {
        ClientBuilder::new()
    }

    /// Retrieves a single post by its unique ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or if the post is not found.
    fn get_by_id(&self, id: u32) -> impl std::future::Future<Output = Result<Self::Post>> + Send;

    /// Retrieves posts matching the configured query.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails or if the response cannot be parsed.
    fn get(&self) -> impl std::future::Future<Output = Result<Vec<Self::Post>>> + Send;
}

impl<T: Client> ClientBuilder<T> {
    /// Creates a new builder with default settings.
    ///
    /// Uses the shared HTTP client for connection pooling.
    #[must_use]
    pub fn new() -> Self {
        Self {
            client: SHARED_CLIENT.clone(),
            key: None,
            user: None,
            tags: Vec::new(),
            limit: 100,
            url: T::URL.to_string(),
            page: 0,
            _marker: std::marker::PhantomData,
        }
    }

    /// Creates a new builder with a custom HTTP client.
    ///
    /// Use this when you need custom HTTP configuration (e.g., proxy, custom TLS).
    #[must_use]
    pub fn with_client(client: reqwest::Client) -> Self {
        Self {
            client,
            key: None,
            user: None,
            tags: Vec::new(),
            limit: 100,
            url: T::URL.to_string(),
            page: 0,
            _marker: std::marker::PhantomData,
        }
    }

    /// Sets a custom base URL for the API.
    ///
    /// This is primarily useful for testing with mock servers.
    #[must_use]
    pub fn with_custom_url(mut self, url: &str) -> Self {
        self.url = url.to_string();
        self
    }

    /// Sets the API key and username for authenticated requests.
    ///
    /// Some booru sites require or benefit from authentication.
    #[must_use]
    pub fn set_credentials(mut self, key: impl Into<String>, user: impl Into<String>) -> Self {
        self.key = Some(key.into());
        self.user = Some(user.into());
        self
    }

    /// Adds a tag to the search query.
    ///
    /// # Errors
    ///
    /// Returns [`BooruError::TagLimitExceeded`] if adding this tag would exceed
    /// the client's maximum tag limit.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use booru_rs::danbooru::DanbooruClient;
    /// use booru_rs::client::Client;
    ///
    /// # fn example() -> booru_rs::error::Result<()> {
    /// let client = DanbooruClient::builder()
    ///     .tag("cat_ears")?
    ///     .tag("blue_eyes")?
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn tag(mut self, tag: impl Into<String>) -> Result<Self> {
        if let Some(max) = T::MAX_TAGS
            && self.tags.len() >= max
        {
            return Err(BooruError::TagLimitExceeded {
                client: std::any::type_name::<T>()
                    .rsplit("::")
                    .next()
                    .unwrap_or("Unknown"),
                max,
                actual: self.tags.len() + 1,
            });
        }
        self.tags.push(tag.into());
        Ok(self)
    }

    /// Adds a rating filter to the search query.
    ///
    /// The rating type is specific to each booru site, ensuring
    /// compile-time type safety.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use booru_rs::danbooru::{DanbooruClient, DanbooruRating};
    /// use booru_rs::client::Client;
    ///
    /// let client = DanbooruClient::builder()
    ///     .rating(DanbooruRating::General)
    ///     .build();
    /// ```
    #[must_use]
    pub fn rating(mut self, rating: T::Rating) -> Self {
        self.tags.push(format!("rating:{}", rating.into()));
        self
    }

    /// Sets the maximum number of posts to retrieve.
    ///
    /// Default is 100, which is also typically the maximum allowed by most APIs.
    #[must_use]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = limit;
        self
    }

    /// Enables random ordering of results.
    #[must_use]
    pub fn random(mut self) -> Self {
        self.tags.push(format!("{}random", T::SORT));
        self
    }

    /// Adds a sort order to the query.
    #[must_use]
    pub fn sort(mut self, order: generic::Sort) -> Self {
        self.tags.push(format!("{}{}", T::SORT, order));
        self
    }

    /// Excludes posts with the specified tag.
    ///
    /// Multiple blacklist tags can be added by calling this method multiple times.
    #[must_use]
    pub fn blacklist_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(format!("-{}", tag.into()));
        self
    }

    /// Overrides the default API URL.
    ///
    /// Useful for testing or accessing mirror sites.
    #[must_use]
    pub fn default_url(mut self, url: impl Into<String>) -> Self {
        self.url = url.into();
        self
    }

    /// Sets the page number for pagination.
    ///
    /// Page numbering starts at 0.
    #[must_use]
    pub fn page(mut self, page: u32) -> Self {
        self.page = page;
        self
    }

    /// Adds multiple tags to the search query at once.
    ///
    /// # Errors
    ///
    /// Returns [`BooruError::TagLimitExceeded`] if adding these tags would exceed
    /// the client's maximum tag limit.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use booru_rs::prelude::*;
    ///
    /// # fn example() -> Result<()> {
    /// let client = GelbooruClient::builder()
    ///     .tags(["cat_ears", "blue_eyes", "1girl"])?
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn tags<I, S>(mut self, tags: I) -> Result<Self>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for tag in tags {
            self = self.tag(tag)?;
        }
        Ok(self)
    }

    /// Excludes multiple tags from the search query at once.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use booru_rs::prelude::*;
    ///
    /// # fn example() -> Result<()> {
    /// let client = GelbooruClient::builder()
    ///     .tag("cat_ears")?
    ///     .blacklist_tags(["ugly", "low_quality"])
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn blacklist_tags<I, S>(mut self, tags: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for tag in tags {
            self = self.blacklist_tag(tag);
        }
        self
    }

    /// Returns the current number of tags in the query.
    #[must_use]
    pub fn tag_count(&self) -> usize {
        self.tags.len()
    }

    /// Returns `true` if the builder has any tags configured.
    #[must_use]
    pub fn has_tags(&self) -> bool {
        !self.tags.is_empty()
    }

    /// Builds the client with the configured options.
    #[must_use]
    pub fn build(self) -> T {
        T::from(self)
    }
}

impl<T: Client> Default for ClientBuilder<T> {
    fn default() -> Self {
        Self::new()
    }
}

// Re-exports for convenience
#[cfg(feature = "danbooru")]
pub use danbooru::DanbooruClient;
#[cfg(feature = "gelbooru")]
pub use gelbooru::GelbooruClient;
#[cfg(feature = "rule34")]
pub use rule34::Rule34Client;
#[cfg(feature = "safebooru")]
pub use safebooru::SafebooruClient;