Skip to content

Commit

Permalink
fix path imports
Browse files Browse the repository at this point in the history
add base traits for enums and structures
  • Loading branch information
TOwInOK committed Oct 25, 2024
1 parent 8fdf76f commit 36d86cd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "anime-grubber"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
authors = ["TOwInOK <[email protected]>"]
description = "A convenient library for extracting images of cute characters from websites."
Expand Down
14 changes: 7 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ anime-grubber = "0"
## Использование

```rust
use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let instance = Faifu::new(Categories::SFW(SFW::Dance));
let instance = Waifu::new(Categories::SFW(SFW::Dance));
let image = instance.get().await?;
println!("Fetched image URL: {}", image);
Ok(())
Expand All @@ -53,12 +53,12 @@ async fn main() -> anyhow::Result<()> {
Этот пример показывает, как можно получить одно изображение из категории **SFW::Dance**:

```rust
use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
use tracing::info;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let instance = Faifu::new(Categories::SFW(SFW::Dance));
let instance = Waifu::new(Categories::SFW(SFW::Dance));
let image = instance.get().await?;
info!("Fetched image URL: {}", image);
Ok(())
Expand All @@ -70,12 +70,12 @@ async fn main() -> anyhow::Result<()> {
Получение сразу нескольких изображений из той же категории:

```rust
use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
use tracing::info;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let instance = Faifu::new(Categories::SFW(SFW::Dance));
let instance = Waifu::new(Categories::SFW(SFW::Dance));
let images = instance.get_many().await?;
assert_eq!(images.len(), 30);
info!("{:#?}", images);
Expand Down Expand Up @@ -106,4 +106,4 @@ tracing::subscriber::set_global_default(

## Лицензия

Этот проект распространяется под лицензией **MIT**. Подробности см. в файле [LICENSE](./LICENSE).
Этот проект распространяется под лицензией **MIT**. Подробности см. в файле [LICENSE](./LICENSE).
22 changes: 11 additions & 11 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait Agent {
/// Asynchronously fetches a single image based on the current category and aspect.
///
/// This method constructs a URL using the category and aspect generated from
/// the current state of the `Faifu` instance. It performs an HTTP GET request
/// the current state of the `Waifu` instance. It performs an HTTP GET request
/// to retrieve the image. If the image is not found, it returns an error.
///
/// # Returns
Expand All @@ -26,12 +26,12 @@ pub trait Agent {
/// # Example
/// ```rust
/// use anime_grubber::agent::Agent;
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
///
/// #[tokio::main]
/// async fn main() {
/// let faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// let image_url = faifu.get().await.expect("Failed to fetch image");
/// let Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// let image_url = Waifu.get().await.expect("Failed to fetch image");
/// println!("Fetched image URL: {}", image_url);
/// }
/// ```
Expand All @@ -40,7 +40,7 @@ pub trait Agent {
/// Asynchronously fetches multiple images based on the current category and aspect.
///
/// This method constructs a URL using the category and aspect generated from
/// the current state of the `Faifu` instance. It performs an HTTP POST request
/// the current state of the `Waifu` instance. It performs an HTTP POST request
/// to retrieve an array of images. If the images are not found, it returns an error.
///
/// # Returns
Expand All @@ -50,12 +50,12 @@ pub trait Agent {
/// # Example
/// ```rust
/// use anime_grubber::agent::Agent;
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
///
/// #[tokio::main]
/// async fn main() {
/// let faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// let many_images = faifu.get_many().await.expect("Failed to fetch images");
/// let Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// let many_images = Waifu.get_many().await.expect("Failed to fetch images");
/// for image_url in many_images {
/// println!("Fetched image URL: {}", image_url);
/// }
Expand All @@ -74,12 +74,12 @@ pub trait Agent {
/// # Example
/// ```rust
/// use anime_grubber::agent::Agent;
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
///
/// #[tokio::main]
/// async fn main() {
/// let faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// let random_image_url = faifu.get_random().await.expect("Failed to fetch random image");
/// let Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// let random_image_url = Waifu.get_random().await.expect("Failed to fetch random image");
/// println!("Fetched random image URL: {}", random_image_url);
/// }
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! List of avaiable agents
/// [Vaifu.pics api represintation](https://waifu.pics/docs)
pub mod vailfupics;
pub mod waifu_pics;
36 changes: 18 additions & 18 deletions src/agents/vailfupics.rs → src/agents/waifu_pics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use tracing::{debug, info, instrument, trace};
const SOLO_URL: &str = "https://api.waifu.pics";
const MANY_URL: &str = "https://api.waifu.pics/many";

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A struct representing an image-fetching agent from the [Waifu.pics API](https://waifu.pics/docs).
///
/// The `Faifu` struct implements the `Agent` trait and provides methods to retrieve
/// The `Waifu` struct implements the `Agent` trait and provides methods to retrieve
/// images based on specified categories. It supports fetching single images, multiple images,
/// and random images. The struct utilizes asynchronous operations for efficient network
/// requests.
Expand All @@ -23,51 +23,51 @@ const MANY_URL: &str = "https://api.waifu.pics/many";
/// # Example
/// ```rust
/// use anime_grubber::agent::Agent;
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
///
/// async fn test() {
/// let mut faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// let image_url = faifu.get().await.expect("Problem with intrnet");
/// let many_images = faifu.get_many().await.expect("Problem with intrnet");
/// let random_image = faifu.get_random().await.expect("Problem with intrnet");
/// let mut Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// let image_url = Waifu.get().await.expect("Problem with intrnet");
/// let many_images = Waifu.get_many().await.expect("Problem with intrnet");
/// let random_image = Waifu.get_random().await.expect("Problem with intrnet");
/// }
/// ```
pub struct Faifu {
pub struct Waifu {
categorie: Categories,
}

impl Faifu {
/// Creates a new instance of `Faifu` with the specified category.
impl Waifu {
/// Creates a new instance of `Waifu` with the specified category.
///
/// # Parameters
/// - `categorie`: The category of images to fetch, which can be either SFW (Safe for Work)
/// or NSFW (Not Safe for Work).
///
/// # Returns
/// Returns a new `Faifu` instance.
/// Returns a new `Waifu` instance.
///
/// # Example
/// ```rust
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW};
///
/// let faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// let Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// ```
#[instrument(skip(categorie))]
pub fn new(categorie: Categories) -> Self {
Self { categorie }
}

/// Updates the category of the `Faifu` instance.
/// Updates the category of the `Waifu` instance.
///
/// # Parameters
/// - `categorie`: The new category of images to fetch.
///
/// # Example
/// ```rust
/// use anime_grubber::agents::vailfupics::{Faifu, Categories, SFW, NSFW};
/// use anime_grubber::agents::waifu_pics::{Waifu, Categories, SFW, NSFW};
///
/// let mut faifu = Faifu::new(Categories::SFW(SFW::Dance));
/// faifu.set_categorie(Categories::NSFW(NSFW::Neko));
/// let mut Waifu = Waifu::new(Categories::SFW(SFW::Dance));
/// Waifu.set_categorie(Categories::NSFW(NSFW::Neko));
/// ```
#[instrument(skip(self, categorie))]
pub fn set_categorie(&mut self, categorie: Categories) {
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Faifu {
}

#[async_trait]
impl Agent for Faifu {
impl Agent for Waifu {
#[instrument(skip(self))]
async fn get(&self) -> Result<String> {
info!("Fetch data");
Expand Down
4 changes: 2 additions & 2 deletions src/gen_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#[macro_export]
macro_rules! gen_enum {
($name:tt, [ $($variant:ident),* $(,)? ]) => {
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum $name {
$(
$variant,
Expand All @@ -61,7 +61,7 @@ macro_rules! gen_enum {
}
};
($name:tt, [ $($variant:ident($nested:ty)),* $(,)? ]) => {
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum $name {
$(
$variant($nested),
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//!
//! You can start with
//! ```
//! use anime_grubber::{agents::vailfupics::{Faifu, Categories, SFW}, agent::Agent};
//! use anime_grubber::{agents::waifu_pics::{Waifu, Categories, SFW}, agent::Agent};
//!
//! #[tokio::main]
//! async fn main() {
//! let instance = Faifu::new(Categories::SFW(SFW::Dance));
//! let instance = Waifu::new(Categories::SFW(SFW::Dance));
//! let image = instance.get().await.expect("shit happends");
//! println!("Fetched image URL: {}", image);
//! }
Expand All @@ -17,6 +17,8 @@ pub mod agent;
pub mod agents;
/// pub errors of this crate
pub mod error;
mod gen_enum;
mod gen_url;
mod result;
pub mod gen_enum;
pub mod gen_url;
pub mod result;

pub use crate::{agent::Agent, agents::*, error::Error, result::Result};
6 changes: 3 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod test {
use anime_grubber::{
agent::Agent,
agents::vailfupics::{Categories, Faifu, SFW},
agents::waifu_pics::{Categories, Waifu, SFW},
url,
};
use std::sync::LazyLock;
Expand All @@ -25,15 +25,15 @@ mod test {
#[tokio::test]
async fn check_solo() -> anyhow::Result<()> {
LazyLock::force(&TS);
let instanse = Faifu::new(Categories::SFW(SFW::Dance));
let instanse = Waifu::new(Categories::SFW(SFW::Dance));
let image = instanse.get().await?;
info!("{image}");
Ok(())
}
#[tokio::test]
async fn check_many() -> anyhow::Result<()> {
LazyLock::force(&TS);
let instanse = Faifu::new(Categories::SFW(SFW::Dance));
let instanse = Waifu::new(Categories::SFW(SFW::Dance));
let images = instanse.get_many().await?;
assert_eq!(images.len(), 30);
info!("{:#?}", images);
Expand Down

0 comments on commit 36d86cd

Please sign in to comment.