diff --git a/src/data.rs b/src/data.rs index af91751..a568f84 100644 --- a/src/data.rs +++ b/src/data.rs @@ -70,12 +70,48 @@ impl Card { for printing in &self.card_sets { write!(s, "{}: {} ({})", printing.set_name, printing.set_code, printing.set_rarity)?; if let Some(date) = SETS_BY_NAME.get(&printing.set_name.to_lowercase()).and_then(|s| s.tcg_date) { - write!(s, " - {}", date)?; + write!(s, " - {date}")?; } s.push_str("
"); } Ok(s) } + + pub fn short_info(&self) -> Result { + let mut s = String::new(); + s.push_str(&self.name); + s.push('\n'); + self.basic_info(&mut s, "\n")?; + Ok(s) + } + + fn basic_info(&self, f: &mut W, newline: &str) -> fmt::Result { + if let Some(level) = self.level { + if self.card_type.contains("XYZ") { + f.write_str("Rank ")?; + } else { + f.write_str("Level ")?; + } + write!(f, "{level} ")?; + } else if let Some(lr) = self.link_rating { + write!(f, "Link {lr} ")?; + } + if let Some(attr) = &self.attribute { + write!(f, "{attr}/")?; + } + write!(f, "{} {}", self.r#type, self.card_type)?; + if self.card_type.contains(&String::from("Monster")) { + f.write_str(newline)?; + match (self.atk, self.def) { + (Some(atk), Some(def)) => write!(f, "{atk} ATK / {def} DEF")?, + (Some(atk), None) if self.link_rating.is_some() => write!(f, "{atk} ATK")?, + (None, Some(def)) => write!(f, "? ATK / {def} DEF")?, + (Some(atk), None) => write!(f, "{atk} ATK / ? DEF")?, + (None, None) => write!(f, "? ATK / ? DEF")?, + } + } + Ok(()) + } } impl Display for Card { @@ -92,30 +128,7 @@ impl Display for Card { _ => String::new(), } )?; - if let Some(level) = self.level { - if self.card_type.contains("XYZ") { - f.write_str("Rank ")?; - } else { - f.write_str("Level ")?; - } - write!(f, "{level} ")?; - } else if let Some(lr) = self.link_rating { - write!(f, "Link {lr} ")?; - } - if let Some(attr) = &self.attribute { - write!(f, "{attr}/")?; - } - write!(f, "{} {}", self.r#type, self.card_type)?; - if self.card_type.contains(&String::from("Monster")) { - f.write_str("
")?; - match (self.atk, self.def) { - (Some(atk), Some(def)) => write!(f, "{atk} ATK / {def} DEF")?, - (Some(atk), None) if self.link_rating.is_some() => write!(f, "{atk} ATK")?, - (None, Some(def)) => write!(f, "? ATK / {def} DEF")?, - (Some(atk), None) => write!(f, "{atk} ATK / ? DEF")?, - (None, None) => write!(f, "? ATK / ? DEF")?, - } - } + self.basic_info(f, "
")?; write!(f, "

{}

", &self.text)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 8dd4991..a9703f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,9 +59,10 @@ struct Query { #[derive(Debug)] struct PageData { - title: String, - query: Option, - body: String, + description: String, + title: String, + query: Option, + body: String, } const HEADER: &str = include_str!("../static/header.html"); @@ -84,7 +85,12 @@ async fn search(q: Option, web::Form>>) -> AnyRe let mut res = String::with_capacity(10_000); let data = match q { Some(q) => compute_results(q)?, - None => PageData { title: "YGO card search".to_owned(), query: None, body: "Enter a query above to search".to_owned() }, + None => PageData { + title: "YGO card search".to_owned(), + description: "Enter a query above to search".to_owned(), + query: None, + body: "Enter a query above to search".to_owned(), + }, }; add_data(&mut res, &data)?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) @@ -95,16 +101,23 @@ async fn card_info(card_id: web::Path) -> AnyResult { let mut res = String::with_capacity(2_000); let data = match CARDS_BY_ID.get(&card_id) { Some(card) => PageData { - title: format!("{} - YGO Card Database", card.name), - query: None, - body: format!( - r#"
{card}{}
"#, + title: format!("{} - YGO Card Database", card.name), + description: card.short_info()?, + query: None, + body: format!( + r#"
Card Image: {}{card} {}
"#, + card.name, IMG_HOST.as_str(), card.id, card.extended_info().unwrap_or_else(|_| String::new()), ), }, - None => PageData { title: "Card not found - YGO Card Database".to_owned(), query: None, body: "Card not found".to_owned() }, + None => PageData { + description: "Card not found - YGO Card Database".to_owned(), + title: "Card not found - YGO Card Database".to_owned(), + query: None, + body: "Card not found".to_owned(), + }, }; add_data(&mut res, &data)?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) @@ -113,7 +126,12 @@ async fn card_info(card_id: web::Path) -> AnyResult { #[get("/help")] async fn help() -> AnyResult { let mut res = String::with_capacity(HEADER.len() + HELP_CONTENT.len() + FOOTER.len() + 250); - let data = PageData { query: None, title: "Query Syntax - YGO Card Database".to_owned(), body: HELP_CONTENT.to_owned() }; + let data = PageData { + query: None, + title: "Query Syntax - YGO Card Database".to_owned(), + body: HELP_CONTENT.to_owned(), + description: String::new(), + }; add_data(&mut res, &data)?; Ok(HttpResponse::Ok().insert_header(header::ContentType::html()).body(res)) } @@ -124,7 +142,8 @@ fn add_searchbox(res: &mut String, query: &Option) -> std::fmt::Result { r#"
-
"#, + +"#, match &query { Some(q) => q.replace('"', """), None => String::new(), @@ -138,7 +157,12 @@ fn compute_results(raw_query: String) -> AnyResult { Ok(q) => q, Err(e) => { let s = format!("Could not parse query: {e:?}"); - return Ok(PageData { title: s.clone(), query: Some(raw_query), body: s }); + return Ok(PageData { + description: s.clone(), + query: Some(raw_query), + body: s, + title: "YGO Card Database".to_owned(), + }); } }; let now = Instant::now(); @@ -151,24 +175,37 @@ fn compute_results(raw_query: String) -> AnyResult { let readable_query = format!("Showing {} results where {}", matches.len(), raw_filters.iter().map(|f| f.to_string()).join(" and "),); write!(body, "{readable_query} (took {:?})", now.elapsed())?; if matches.is_empty() { - return Ok(PageData { title: readable_query.clone(), query: Some(raw_query), body }); + return Ok(PageData { + description: readable_query, + query: Some(raw_query), + body, + title: "No results - YGO Card Database".to_owned(), + }); } body.push_str("
"); - for card in matches { + for card in &matches { write!( body, - r#"{card}"#, + r#"Card Image: {}{card}"#, card.id, + card.name, IMG_HOST.as_str(), card.id )?; } body.push_str("
"); - Ok(PageData { title: readable_query.clone(), query: Some(raw_query), body }) + Ok(PageData { + description: readable_query, + query: Some(raw_query), + body, + title: format!("{} results - YGO Card Database", matches.len()), + }) } fn add_data(res: &mut String, pd: &PageData) -> AnyResult<()> { - res.push_str(&HEADER.replacen("{TITLE}", &pd.title, 1).replacen("{IMG_HOST}", &IMG_HOST, 1)); + res.push_str( + &HEADER.replacen("{DESCRIPTION}", &pd.description, 2).replacen("{IMG_HOST}", &IMG_HOST, 1).replacen("{TITLE}", &pd.title, 2), + ); add_searchbox(res, &pd.query)?; res.push_str(&pd.body); res.push_str(FOOTER); diff --git a/static/header.html b/static/header.html index 920a68d..3b6de3a 100644 --- a/static/header.html +++ b/static/header.html @@ -1,6 +1,10 @@ - + - + + + + + {TITLE}