Skip to main content

uller/
lib.rs

1//! This crate provide useful interface for creating links
2//! and
3//! download data by created links
4
5#[cfg(feature = "macro")]
6pub use uller_macro::*;
7pub use url::Url;
8
9// for Qller
10/// Trait for unification [Url] builder
11pub trait MakeLink {
12    fn url_generate(&self) -> crate::Url;
13}
14#[cfg(feature = "juller")]
15// for Jller
16/// Desirialize to json any struct by [link](MakeLink)
17#[async_trait::async_trait]
18pub trait JsonDownload<T>: MakeLink
19where
20    T: for<'a> serde::Deserialize<'a>,
21{
22    /// Make url and parse to `<T>`
23    async fn download(&self) -> Result<T, Box<dyn std::error::Error>> {
24        Ok(reqwest::get(self.url_generate()).await?.json().await?)
25    }
26    /// Make url and parse to `<T>` with url anotate
27    async fn download_verbose(&self) -> Result<T, Box<dyn std::error::Error>> {
28        let url = self.url_generate();
29        println!("{:#?}", String::from(url.clone()));
30        Ok(reqwest::get(url).await?.json().await?)
31    }
32}
33
34#[cfg(feature = "buller")]
35// for Bller
36/// Download bytes by [link](MakeLink)
37#[async_trait::async_trait]
38pub trait BytesDownload: MakeLink {
39    /// Make url and parse to Bytes
40    async fn download(&self) -> Result<bytes::Bytes, Box<dyn std::error::Error>> {
41        Ok(reqwest::get(self.url_generate()).await?.bytes().await?)
42    }
43    /// Make url and parse to Bytes with url anotate
44    async fn download_verbose(&self) -> Result<bytes::Bytes, Box<dyn std::error::Error>> {
45        let url = self.url_generate();
46        println!("{:#?}", String::from(url.clone()));
47        Ok(reqwest::get(url).await?.bytes().await?)
48    }
49}
50
51/// Just all that you need.
52pub mod prelude {
53    #[cfg(feature = "buller")]
54    pub use crate::BytesDownload;
55    #[cfg(feature = "juller")]
56    pub use crate::JsonDownload;
57
58    pub use crate::MakeLink;
59    pub use crate::Url;
60
61    #[cfg(feature = "buller")]
62    pub use uller_macro::Buller;
63    #[cfg(feature = "juller")]
64    pub use uller_macro::Juller;
65    #[cfg(feature = "macro")]
66    pub use uller_macro::Qller;
67}