allow for more config and cli args

This commit is contained in:
2025-05-28 17:33:00 +02:00
parent 5905bd6b15
commit af002b0149
5 changed files with 302 additions and 107 deletions

View File

@@ -5,7 +5,7 @@ use serde::Deserialize;
use std::fmt::Display;
#[derive(Debug, Clone)]
pub struct Id(String);
pub struct Id(pub String);
#[derive(Debug)]
pub enum ResponseResult {
@@ -41,6 +41,7 @@ pub enum Language {
Spanish,
Esperanto,
Polish,
Croatian,
}
#[derive(Debug)]
@@ -106,6 +107,23 @@ struct ChapterImagesContent {
chapter: ChapterImageDataContent,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiError {
pub id: String,
pub status: u32,
pub title: String,
pub detail: String,
pub context: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChapterImagesContentError {
pub result: String,
pub errors: Vec<ApiError>,
}
#[derive(Debug, Deserialize)]
struct ChapterImageDataContent {
hash: String,
@@ -379,6 +397,11 @@ enum ResponseConversionError {
ContentType(String),
}
#[derive(Debug)]
enum ChapterImageError {
Result(String),
}
impl TryInto<State> for &str {
type Error = ();
@@ -434,6 +457,7 @@ impl TryInto<Language> for &str {
"el" => Language::Greek,
"zh" => Language::SimplifiedChinese,
"tl" => Language::Tagalog,
"hr" => Language::Croatian,
_ => return Err(()),
})
}
@@ -904,11 +928,6 @@ fn convert_chapter_attributes(
})
}
#[derive(Debug)]
enum ChapterImageError {
Result(String),
}
fn convert_chapter_images(data: ChapterImagesContent) -> Result<ChapterImages, ChapterImageError> {
Ok(ChapterImages {
result: (data.result.as_str())
@@ -922,14 +941,19 @@ fn convert_chapter_images(data: ChapterImagesContent) -> Result<ChapterImages, C
})
}
pub fn deserialize_chapter_images(json: &str) -> ChapterImages {
pub fn deserialize_chapter_images(json: &str) -> Result<ChapterImages, ChapterImagesContentError> {
let chapter_images: ChapterImagesContent = match serde_json::from_str(json) {
Ok(v) => v,
Err(e) => {
std::fs::write("out.json", json).unwrap();
eprintln!("ERROR: {:#?}", e);
std::process::exit(1);
match serde_json::from_str::<ChapterImagesContentError>(json) {
Ok(v) => return Err(v),
Err(e) => {
// If you can't parse the error then there is no point in continuing.
eprintln!("ERROR: {:#?}", e);
std::process::exit(1);
}
}
}
};
convert_chapter_images(chapter_images).unwrap()
Ok(convert_chapter_images(chapter_images).unwrap())
}