lib_mal/lib.rs
1//! ## Quick Start
2//! To use `lib-mal` you will need an API key from [MyAnimeList.net](https://myanimelist.net), and a callback URL. An example of how to use `lib-mal` might look like this:
3//!
4//! ```no_run
5//! use lib_mal::ClientBuilder;
6//! use std::path::PathBuf;
7//! use lib_mal::MALError;
8//!
9//! async fn test() -> Result<(), MALError>{
10//! //this has to exactly match a URI that's been registered with the MAL api
11//! let redirect = "[YOUR_REDIRECT_URI_HERE]";
12//! //the MALClient will attempt to refresh the cached access_token, if applicable
13//! let mut client = ClientBuilder::new().secret("[YOUR_SECRET_HERE]".to_string()).caching(true).cache_dir(PathBuf::from("[SOME_CACHE_DIR]")).build_with_refresh().await?;
14//! let (auth_url, challenge, state) = client.get_auth_parts();
15//! //the user will have to have access to a browser in order to log in and give your application permission
16//! println!("Go here to log in :) -> {}", auth_url);
17//! //once the user has the URL, be sure to call client.auth to listen for the callback and complete the OAuth2 handshake
18//! client.auth(&redirect, &challenge, &state).await?;
19//! //once the user is authorized, the API should be usable
20//! //this will get the details, including all fields, for Mobile Suit Gundam
21//! let anime = client.get_anime_details(80, None).await?;
22//! //because so many fields are optional, a lot of the members of lib_mal::model::AnimeDetails are `Option`s
23//! println!("{}: started airing on {}, ended on {}, ranked #{}", anime.show.title, anime.start_date.unwrap(), anime.end_date.unwrap(), anime.rank.unwrap());
24//! Ok(())
25//!}
26//!```
27
28#[cfg(test)]
29mod test;
30
31mod builder;
32mod client;
33pub mod model;
34
35pub use builder::ClientBuilder;
36pub use client::MALClient;
37
38use serde::{Deserialize, Serialize};
39use std::error::Error;
40use std::fmt::{Debug, Display};
41
42#[derive(Serialize, Deserialize)]
43pub struct MALError {
44 pub error: String,
45 pub message: Option<String>,
46 pub info: Option<String>,
47}
48
49impl Display for MALError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 write!(f, "lib_mal encountered an error: {}", self.error)
52 }
53}
54
55impl Debug for MALError {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(
58 f,
59 "error: {} message: {} info: {}",
60 self.error,
61 self.message.as_ref().unwrap_or(&"none".to_string()),
62 self.info.as_ref().unwrap_or(&"none".to_string())
63 )
64 }
65}
66
67impl Error for MALError {}
68
69impl MALError {
70 pub fn new(msg: &str, error: &str, info: impl Into<Option<String>>) -> Self {
71 MALError {
72 error: error.to_owned(),
73 message: Some(msg.to_owned()),
74 info: info.into(),
75 }
76 }
77}
78
79pub mod prelude {
80 pub use crate::builder::ClientBuilder;
81 pub use crate::client::MALClient;
82 pub use crate::model::*;
83}