impl TryFrom instead of having conversion functions
This commit is contained in:
@@ -5,13 +5,13 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.38"
|
||||
crossterm = "0.28.1"
|
||||
crossterm = "0.29"
|
||||
futures = "0.3.30"
|
||||
icy_sixel = "0.1.2"
|
||||
image = { version = "0.25.2", default-features = false, features = ["jpeg", "png"] }
|
||||
reqwest = "0.12.5"
|
||||
reqwest-middleware = "0.3.2"
|
||||
reqwest-retry = "0.6.0"
|
||||
reqwest-middleware = "0.4"
|
||||
reqwest-retry = "0.7"
|
||||
serde = { version = "1.0.204", features = ["derive"] }
|
||||
serde_json = "1.0.121"
|
||||
tokio = { version = "1.39.2", default-features = false, features = ["macros", "rt-multi-thread"] }
|
||||
|
||||
@@ -149,7 +149,7 @@ async fn main() {
|
||||
let mut chapters = match get_chapters(client, &manga.id).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
@@ -458,7 +458,7 @@ async fn select_manga_from_search(
|
||||
let choice = match select::select(&entries) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: Failed to select: {:?}", e);
|
||||
eprintln!("ERROR: Failed to select: {e:?}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -625,21 +625,21 @@ enum ChapterImageError {
|
||||
Result(String),
|
||||
}
|
||||
|
||||
impl TryInto<State> for &str {
|
||||
impl TryFrom<&str> for State {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<State, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"published" => State::Published,
|
||||
_ => return Err(()),
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<ContentRating> for &str {
|
||||
impl TryFrom<&str> for ContentRating {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<ContentRating, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"safe" => ContentRating::Safe,
|
||||
"suggestive" => ContentRating::Suggestive,
|
||||
"erotica" => ContentRating::Erotica,
|
||||
@@ -648,11 +648,11 @@ impl TryInto<ContentRating> for &str {
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<Language> for &str {
|
||||
impl TryFrom<&str> for Language {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<Language, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"ab" => Language::Abkhazian,
|
||||
"aa" => Language::Afar,
|
||||
"af" => Language::Afrikaans,
|
||||
@@ -883,11 +883,11 @@ impl TryInto<Language> for &str {
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<PublicationDemographic> for &str {
|
||||
impl TryFrom<&str> for PublicationDemographic {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<PublicationDemographic, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"shounen" => PublicationDemographic::Shounen,
|
||||
"josei" => PublicationDemographic::Josei,
|
||||
"shoujo" => PublicationDemographic::Shoujo,
|
||||
@@ -896,11 +896,11 @@ impl TryInto<PublicationDemographic> for &str {
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<DataType> for &str {
|
||||
impl TryFrom<&str> for DataType {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<DataType, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"manga" => DataType::Manga,
|
||||
"chapter" => DataType::Chapter,
|
||||
"cover_art" => DataType::CoverArt,
|
||||
@@ -915,11 +915,11 @@ impl TryInto<DataType> for &str {
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<Status> for &str {
|
||||
impl TryFrom<&str> for Status {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<Status, ()> {
|
||||
Ok(match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match s {
|
||||
"ongoing" => Status::Ongoing,
|
||||
"completed" => Status::Completed,
|
||||
"hiatus" => Status::Hiatus,
|
||||
@@ -928,21 +928,21 @@ impl TryInto<Status> for &str {
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryInto<ResponseResult> for &str {
|
||||
impl TryFrom<&str> for ResponseResult {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<ResponseResult, ()> {
|
||||
match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
match s {
|
||||
"ok" => Ok(ResponseResult::Ok),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl TryInto<Response> for &str {
|
||||
impl TryFrom<&str> for Response {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<Response, ()> {
|
||||
match self {
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
match s {
|
||||
"collection" => Ok(Response::Collection),
|
||||
"entity" => Ok(Response::Entity),
|
||||
_ => Err(()),
|
||||
@@ -950,9 +950,9 @@ impl TryInto<Response> for &str {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_response_to_result(
|
||||
search_response: SearchResponse,
|
||||
) -> Result<SearchResult, ResponseConversionError> {
|
||||
impl TryFrom<SearchResponse> for SearchResult {
|
||||
type Error = ResponseConversionError;
|
||||
fn try_from(search_response: SearchResponse) -> Result<Self, Self::Error> {
|
||||
let response = (search_response.response.as_str())
|
||||
.try_into()
|
||||
.map_err(|_| ResponseConversionError::Result(search_response.response))?;
|
||||
@@ -964,7 +964,7 @@ fn convert_response_to_result(
|
||||
Ok(Vec::with_capacity(search_response.data.len()));
|
||||
for m in search_response.data {
|
||||
if let Ok(ref mut d) = data {
|
||||
match convert_data_to_manga(m) {
|
||||
match m.try_into() {
|
||||
Ok(v) => d.push(v),
|
||||
Err(e) => {
|
||||
data = Err(e);
|
||||
@@ -978,6 +978,7 @@ fn convert_response_to_result(
|
||||
result,
|
||||
data: data?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -1023,9 +1024,9 @@ impl Display for ContentRating {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_attributes(
|
||||
attributes: ContentAttributes,
|
||||
) -> Result<MangaAttributes, AttributeConversionError> {
|
||||
impl TryFrom<ContentAttributes> for MangaAttributes {
|
||||
type Error = AttributeConversionError;
|
||||
fn try_from(attributes: ContentAttributes) -> Result<Self, Self::Error> {
|
||||
Ok(MangaAttributes {
|
||||
title: attributes.title,
|
||||
alt_titles: attributes.alt_titles,
|
||||
@@ -1089,7 +1090,8 @@ fn convert_attributes(
|
||||
.map_err(|_| AttributeConversionError::DataType(m.type_name))?,
|
||||
id: Id(m.id),
|
||||
relationships: {
|
||||
let mut relationships = Vec::with_capacity(m.relationships.len());
|
||||
let mut relationships =
|
||||
Vec::with_capacity(m.relationships.len());
|
||||
for m in m.relationships {
|
||||
relationships.push(({
|
||||
|| {
|
||||
@@ -1109,7 +1111,8 @@ fn convert_attributes(
|
||||
},
|
||||
)
|
||||
}
|
||||
})()?);
|
||||
})(
|
||||
)?);
|
||||
}
|
||||
relationships
|
||||
},
|
||||
@@ -1161,54 +1164,58 @@ fn convert_attributes(
|
||||
.as_ref()
|
||||
.map(|m| Id(m.clone())),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_id_query(json: &str) -> IdQueryResult {
|
||||
let id_query_response: IdQueryResponse = match serde_json::from_str(json) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
std::fs::write("out.json", json).unwrap();
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
convert_id_query(id_query_response).unwrap()
|
||||
id_query_response.try_into().unwrap()
|
||||
}
|
||||
|
||||
fn convert_id_query(response: IdQueryResponse) -> Result<IdQueryResult, AttributeConversionError> {
|
||||
impl TryFrom<IdQueryResponse> for IdQueryResult {
|
||||
type Error = AttributeConversionError;
|
||||
fn try_from(response: IdQueryResponse) -> Result<Self, Self::Error> {
|
||||
Ok(IdQueryResult {
|
||||
result: response.result.as_str().try_into().unwrap(),
|
||||
response: response.response.as_str().try_into().unwrap(),
|
||||
data: convert_data_to_manga(response.data).unwrap(),
|
||||
data: response.data.try_into().unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_chapter_feed(json: &str) -> ChapterFeed {
|
||||
let chapter_feed_response: ChapterFeedResponse = match serde_json::from_str(json) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
std::fs::write("chapter_feed.json", json).unwrap();
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
convert_chapter_feed(chapter_feed_response).unwrap()
|
||||
chapter_feed_response.try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn deserializer(json: &str) -> SearchResult {
|
||||
let search_response: SearchResponse = match serde_json::from_str(json) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
std::fs::write("search_result.json", json).unwrap();
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
let search_result = convert_response_to_result(search_response);
|
||||
let search_result = search_response.try_into();
|
||||
match search_result {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: Failed to convert search response: {:#?}", e);
|
||||
eprintln!("ERROR: Failed to convert search response: {e:#?}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -1235,9 +1242,10 @@ enum ChapterRelationshipError {
|
||||
Id(String),
|
||||
}
|
||||
|
||||
fn convert_chapter_feed(
|
||||
feed: ChapterFeedResponse,
|
||||
) -> Result<ChapterFeed, ChapterFeedConversionError> {
|
||||
impl TryFrom<ChapterFeedResponse> for ChapterFeed {
|
||||
type Error = ChapterFeedConversionError;
|
||||
|
||||
fn try_from(feed: ChapterFeedResponse) -> Result<Self, Self::Error> {
|
||||
// Now this is a bit of an abomination. It uses closures such that you can use the ? syntax
|
||||
// sugar to return an error. Also uses for loops instead of iterators since they do not take
|
||||
// ownership. I think I should have just kept the iterators.
|
||||
@@ -1250,7 +1258,9 @@ fn convert_chapter_feed(
|
||||
.try_into()
|
||||
.map_err(|_| ChapterConversionError::DataType(m.type_name))?,
|
||||
id: Id(m.id),
|
||||
attributes: convert_chapter_attributes(m.attributes)
|
||||
attributes: m
|
||||
.attributes
|
||||
.try_into()
|
||||
.map_err(ChapterConversionError::Attributes)?,
|
||||
relationships: {
|
||||
let mut relationships = Vec::with_capacity(m.relationships.len());
|
||||
@@ -1295,6 +1305,7 @@ fn convert_chapter_feed(
|
||||
offset: feed.offset,
|
||||
total: feed.total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -1307,9 +1318,9 @@ enum ChapterAttributeConversionError {
|
||||
TranslatedLanguage(String),
|
||||
}
|
||||
|
||||
fn convert_chapter_attributes(
|
||||
attributes: ChapterAttributesContent,
|
||||
) -> Result<ChapterAttributes, ChapterAttributeConversionError> {
|
||||
impl TryFrom<ChapterAttributesContent> for ChapterAttributes {
|
||||
type Error = ChapterAttributeConversionError;
|
||||
fn try_from(attributes: ChapterAttributesContent) -> Result<Self, Self::Error> {
|
||||
Ok(ChapterAttributes {
|
||||
volume: match &attributes.volume {
|
||||
Some(v) => match v.parse() {
|
||||
@@ -1337,13 +1348,18 @@ fn convert_chapter_attributes(
|
||||
translated_language: (attributes.translated_language.as_str())
|
||||
.try_into()
|
||||
.map_err(|_| {
|
||||
ChapterAttributeConversionError::TranslatedLanguage(attributes.translated_language)
|
||||
ChapterAttributeConversionError::TranslatedLanguage(
|
||||
attributes.translated_language,
|
||||
)
|
||||
})?,
|
||||
version: attributes.version,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_chapter_images(data: ChapterImagesContent) -> Result<ChapterImages, ChapterImageError> {
|
||||
impl TryFrom<ChapterImagesContent> for ChapterImages {
|
||||
type Error = ChapterImageError;
|
||||
fn try_from(data: ChapterImagesContent) -> Result<Self, Self::Error> {
|
||||
Ok(ChapterImages {
|
||||
result: (data.result.as_str())
|
||||
.try_into()
|
||||
@@ -1354,6 +1370,7 @@ fn convert_chapter_images(data: ChapterImagesContent) -> Result<ChapterImages, C
|
||||
data: data.chapter.data,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_chapter_images(json: &str) -> Result<ChapterImages, ChapterImagesContentError> {
|
||||
@@ -1364,22 +1381,28 @@ pub fn deserialize_chapter_images(json: &str) -> Result<ChapterImages, ChapterIm
|
||||
Ok(v) => return Err(v),
|
||||
Err(e) => {
|
||||
// If you can't parse the error then there is no point in continuing.
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(convert_chapter_images(chapter_images).unwrap())
|
||||
Ok(chapter_images.try_into().unwrap())
|
||||
}
|
||||
|
||||
fn convert_data_to_manga(m: ContentData) -> Result<Manga, ResponseConversionError> {
|
||||
impl TryFrom<ContentData> for Manga {
|
||||
type Error = ResponseConversionError;
|
||||
fn try_from(m: ContentData) -> Result<Self, Self::Error> {
|
||||
Ok(Manga {
|
||||
id: Id(m.id),
|
||||
data_type: (m.type_name.as_str()).try_into().map_err(|_| {
|
||||
ResponseConversionError::AttributeError(AttributeConversionError::DataType(m.type_name))
|
||||
ResponseConversionError::AttributeError(AttributeConversionError::DataType(
|
||||
m.type_name,
|
||||
))
|
||||
})?,
|
||||
attributes: convert_attributes(m.attributes)
|
||||
attributes: m
|
||||
.attributes
|
||||
.try_into()
|
||||
.map_err(ResponseConversionError::AttributeError)?,
|
||||
relationships: {
|
||||
let mut relationships = Vec::with_capacity(m.relationships.len());
|
||||
@@ -1389,11 +1412,9 @@ fn convert_data_to_manga(m: ContentData) -> Result<Manga, ResponseConversionErro
|
||||
|| {
|
||||
Ok::<Relationship, AttributeConversionError>(Relationship {
|
||||
id: Id(m.id),
|
||||
data_type: m
|
||||
.type_name
|
||||
.as_str()
|
||||
.try_into()
|
||||
.map_err(|_| AttributeConversionError::DataType(m.type_name))?,
|
||||
data_type: m.type_name.as_str().try_into().map_err(|_| {
|
||||
AttributeConversionError::DataType(m.type_name)
|
||||
})?,
|
||||
attributes: {
|
||||
if let Some(attributes) = m.attributes {
|
||||
Some(CoverAttributes {
|
||||
@@ -1443,4 +1464,5 @@ fn convert_data_to_manga(m: ContentData) -> Result<Manga, ResponseConversionErro
|
||||
relationships
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ fn get_input() -> Option<Action> {
|
||||
_ => return None,
|
||||
}),
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
exit();
|
||||
}
|
||||
_ => None,
|
||||
@@ -72,7 +72,7 @@ fn get_input() -> Option<Action> {
|
||||
}
|
||||
Ok(false) => None,
|
||||
Err(e) => {
|
||||
eprintln!("ERROR: {:#?}", e);
|
||||
eprintln!("ERROR: {e:#?}");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ pub fn choose_volumes(input: &str) -> Option<VolumeSelection> {
|
||||
{
|
||||
Ok(v) => Some(VolumeSelection::List(v)),
|
||||
Err(e) => {
|
||||
eprintln!("Invalid number in list: {:#?}", e);
|
||||
eprintln!("Invalid number in list: {e:#?}");
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -224,7 +224,7 @@ pub fn choose_chapters(input: &str) -> Option<ChapterSelection> {
|
||||
.map(|m| match m.parse() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("Invalid input: {:#?}", e);
|
||||
eprintln!("Invalid input: {e:#?}");
|
||||
invalid = true;
|
||||
0.
|
||||
}
|
||||
@@ -247,7 +247,7 @@ pub fn choose_chapters(input: &str) -> Option<ChapterSelection> {
|
||||
}
|
||||
|
||||
pub fn get_input(msg: &str) -> String {
|
||||
print!("{}", msg);
|
||||
print!("{msg}");
|
||||
io::stdout().flush().expect("failed to flush stdout");
|
||||
|
||||
let mut input = String::new();
|
||||
@@ -404,7 +404,7 @@ pub fn args() -> Config {
|
||||
Some(a) => match a.parse() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse value for result-limit: {:?}, type: u32", e);
|
||||
eprintln!("Failed to parse value for result-limit: {e:?}, type: u32");
|
||||
std::process::exit(1);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user