Run cargo fmt

This commit is contained in:
Leon Grünewald 2024-01-01 21:50:57 +01:00
parent f950fa5476
commit eac4798620
4 changed files with 36 additions and 33 deletions

View file

@ -1,9 +1,7 @@
use std::io::Write;
use anyhow::Result;
use base64::Engine;
use base64::engine::{GeneralPurpose};
use crate::post::{Post, Posts}; use crate::post::{Post, Posts};
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT}; use anyhow::Result;
use base64::{engine::GeneralPurpose, Engine};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT};
use reqwest::Response; use reqwest::Response;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -11,7 +9,7 @@ pub struct Client<'a> {
auth: Authentication<'a>, auth: Authentication<'a>,
useragent: &'a str, useragent: &'a str,
host: &'static str, host: &'static str,
http_client: reqwest::Client http_client: reqwest::Client,
} }
impl<'a> Client<'a> { impl<'a> Client<'a> {
@ -31,7 +29,7 @@ impl<'a> Client<'a> {
auth, auth,
useragent, useragent,
host: "https://e621.net", host: "https://e621.net",
http_client http_client,
}) })
} }
@ -40,7 +38,12 @@ impl<'a> Client<'a> {
base64_engine.encode(format!("{username}:{apikey}")) base64_engine.encode(format!("{username}:{apikey}"))
} }
async fn list_posts_raw(&mut self, limit: Option<u16>, tags: Option<Vec<String>>, page: Option<u32>) -> Result<Response> { async fn list_posts_raw(
&mut self,
limit: Option<u16>,
tags: Option<Vec<String>>,
page: Option<u32>,
) -> Result<Response> {
let mut url = url::Url::parse(format!("{}/posts.json", self.host).as_str())?; let mut url = url::Url::parse(format!("{}/posts.json", self.host).as_str())?;
let mut query_params = Vec::new(); let mut query_params = Vec::new();
@ -60,29 +63,32 @@ impl<'a> Client<'a> {
url.set_query(Some(&query_params.join("&"))); url.set_query(Some(&query_params.join("&")));
Ok(self.http_client.get(url.as_str()) Ok(self.http_client.get(url.as_str()).send().await?)
.send()
.await?)
} }
pub async fn list_posts(&mut self, limit: Option<u16>, tags: Option<Vec<String>>, page: Option<u32>) -> Result<Vec<Post>> { pub async fn list_posts(
let res = self.list_posts_raw(limit, tags, page).await?; &mut self,
let text = res.text().await?; limit: Option<u16>,
tags: Option<Vec<String>>,
page: Option<u32>,
) -> Result<Vec<Post>> {
let res = self.list_posts_raw(limit, tags, page).await?;
let text = res.text().await?;
/* /*
let mut debug_file = std::fs::OpenOptions::new() let mut debug_file = std::fs::OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
.open("./debug.json")?; .open("./debug.json")?;
debug_file.write_all(text.as_bytes())?; debug_file.write_all(text.as_bytes())?;
*/ */
Ok(serde_json::from_str::<Posts>(text.as_str())?.into()) Ok(serde_json::from_str::<Posts>(text.as_str())?.into())
} }
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum Authentication<'a> { pub enum Authentication<'a> {
Authorized {username: &'a str, apikey: &'a str}, Authorized { username: &'a str, apikey: &'a str },
Unauthorized Unauthorized,
} }

View file

@ -1,3 +1 @@
enum Error { enum Error {}
}

View file

@ -1,3 +1,3 @@
pub mod client; pub mod client;
pub mod post;
mod errors; mod errors;
pub mod post;

View file

@ -5,7 +5,7 @@ pub type ImageDimension = u64;
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct Posts { pub struct Posts {
#[serde(alias = "posts")] #[serde(alias = "posts")]
inner: Vec<Post> inner: Vec<Post>,
} }
impl Into<Vec<Post>> for Posts { impl Into<Vec<Post>> for Posts {
@ -14,7 +14,6 @@ impl Into<Vec<Post>> for Posts {
} }
} }
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct Post { pub struct Post {
pub id: Id, pub id: Id,
@ -32,7 +31,7 @@ pub struct Post {
pub has_notes: bool, pub has_notes: bool,
pub is_favorited: bool, pub is_favorited: bool,
pub change_seq: Option<u128>, pub change_seq: Option<u128>,
pub relationships: PostRelationships pub relationships: PostRelationships,
} }
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
@ -43,7 +42,7 @@ pub struct PostTags {
pub character: Vec<String>, pub character: Vec<String>,
pub invalid: Vec<String>, pub invalid: Vec<String>,
pub meta: Vec<String>, pub meta: Vec<String>,
pub lore: Vec<String> pub lore: Vec<String>,
} }
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
@ -53,7 +52,7 @@ pub struct PostFile {
pub ext: String, pub ext: String,
pub size: FileSize, pub size: FileSize,
pub md5: String, pub md5: String,
pub url: String pub url: String,
} }
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]