Skip to content

Commit

Permalink
Implement StoreLinks and StoreLink objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Hukadan committed Mar 25, 2023
1 parent 5b9a652 commit ac5fea7
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 12 deletions.
26 changes: 20 additions & 6 deletions pobsd-parser/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::split_line::split_line;
use crate::store_links::{StoreLink, StoreLinks};
use std::fmt;

/* ------------------------ FIELD ENUM -----------------------*/
Expand Down Expand Up @@ -32,7 +33,7 @@ pub enum Field {
Status(Option<String>),
/// Store the result of a Store line of the database
/// Stores are stored in a vector
Store(Option<Vec<String>>),
Store(Option<StoreLinks>),
/// Store the result of a Genre line of the database
/// Genres are stored in a vector
Genres(Option<Vec<String>>),
Expand Down Expand Up @@ -97,7 +98,16 @@ impl fmt::Display for Field {
None => write!(f, "Status"),
},
Field::Store(name) => match name {
Some(name) => write!(f, "Store\t{}", name.join(" ")),
Some(StoreLinks(name)) => {
write!(
f,
"Store\t{}",
name.iter()
.map(|a| a.url.to_string())
.collect::<Vec<String>>()
.join(" ")
)
}
None => write!(f, "Store"),
},
Field::Genres(name) => match name {
Expand Down Expand Up @@ -188,11 +198,12 @@ impl Field {
// Store does not use the same separator than Genre and Tags
"Store" => match right {
Some(right) => {
let mut items: Vec<String> = Vec::new();
let mut items: Vec<StoreLink> = Vec::new();
for item in right.split(' ') {
items.push(item.trim().into());
let store = StoreLink::from(item.trim());
items.push(store);
}
Field::Store(Some(items))
Field::Store(Some(StoreLinks(items)))
}
None => Field::Store(None),
},
Expand Down Expand Up @@ -358,7 +369,10 @@ mod field_tests {
let input = "Store\tfirst second";
let field = Field::from(&input);
assert_eq!(
Field::Store(Some(vec!["first".into(), "second".into()])),
Field::Store(Some(StoreLinks(vec![
StoreLink::from("first"),
StoreLink::from("second")
]))),
field
);
assert_eq!(format!("{}", field), input);
Expand Down
24 changes: 18 additions & 6 deletions pobsd-parser/src/game.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::store_links::StoreLinks;
use std::cmp::{Ordering, PartialOrd};
use std::fmt;

Expand Down Expand Up @@ -44,7 +45,7 @@ pub struct Game {
/// The executable in the package.
pub runtime: Option<String>,
/// A vector with store urls.
pub stores: Option<Vec<String>>,
pub stores: Option<StoreLinks>,
/// Hints (as the name imply).
pub hints: Option<String>,
/// A vector of genres associated with the game.
Expand Down Expand Up @@ -134,7 +135,15 @@ impl fmt::Display for Game {
None => "Runtime".to_string(),
};
let stores = match &self.stores {
Some(stores) => format!("Store\t{}", stores.join(" ")),
Some(stores) => format!(
"Store\t{}",
stores
.inner_ref()
.into_iter()
.map(|a| a.url.to_string())
.collect::<Vec<String>>()
.join(" ")
),
None => "Store".to_string(),
};
let hints = match &self.hints {
Expand Down Expand Up @@ -209,12 +218,16 @@ impl fmt::Display for Game {

#[cfg(test)]
mod game_tests {
use crate::store_links::StoreLink;

use super::*;
fn create_game() -> Game {
let mut game = Game::default();
let tags: Vec<String> = vec!["tag1".to_string(), "tag2".to_string()];
let genres: Vec<String> = vec!["genre1".to_string(), "genre2".to_string()];
let stores: Vec<String> = vec!["store1".to_string(), "store2".to_string()];
let store_links: Vec<StoreLink> = stores.into_iter().map(|a| StoreLink::from(&a)).collect();
let stores = StoreLinks(store_links);
game.uid = 1221;
game.name = "game name".to_string();
game.cover = Some("cover.jpg".to_string());
Expand Down Expand Up @@ -305,10 +318,9 @@ IgdbId";
engine: None,
setup: None,
runtime: Some("HumblePlay".to_string()),
stores: Some(vec![
"https://www.humblebundle.com/store/aaaaaaaaaaaaaaaaaaaaaaaaa-for-the-awesome"
.to_string(),
]),
stores: Some(StoreLinks(vec![StoreLink::from(
"https://www.humblebundle.com/store/aaaaaaaaaaaaaaaaaaaaaaaaa-for-the-awesome",
)])),
hints: Some("Demo on HumbleBundle store page".to_string()),
genres: None,
tags: None,
Expand Down
1 change: 1 addition & 0 deletions pobsd-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub(crate) mod field;
pub mod game;
pub mod parser;
pub(crate) mod split_line;
pub mod store_links;

pub use self::game::Game;
pub use self::parser::Parser;
Expand Down
108 changes: 108 additions & 0 deletions pobsd-parser/src/store_links.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use regex::Regex;

#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub enum Store {
Steam,
Gog,
#[default]
Unknown,
}
#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub struct StoreLink {
pub store: Store,
pub url: String,
}

impl StoreLink {
pub fn from(url: &str) -> Self {
if url.contains("steampowered") {
Self {
store: Store::Steam,
url: url.to_string(),
}
} else if url.contains("gog.com") {
Self {
store: Store::Gog,
url: url.to_string(),
}
} else {
Self {
store: Store::Unknown,
url: url.to_string(),
}
}
}
pub fn get_id(&self) -> Option<usize> {
let re = Regex::new(r"https://store.steampowered.com/app/(\d+)(/?.+)?").unwrap();
match &self.store {
Store::Steam => {
let cap = re.captures(&self.url).unwrap();
if let Some(cap) = cap.get(1) {
return cap.as_str().parse::<usize>().ok();
};
None
}
_ => None,
}
}
}

#[derive(Serialize, Clone, Default, Debug, PartialEq, Eq)]
pub struct StoreLinks(pub Vec<StoreLink>);

impl StoreLinks {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, store: StoreLink) {
self.0.push(store)
}
pub fn inner_ref(&self) -> &Vec<StoreLink> {
&self.0
}
pub fn inner_mut_ref(&mut self) -> &mut Vec<StoreLink> {
&mut self.0
}
pub fn into_inner(self) -> Vec<StoreLink> {
self.0
}
}

#[cfg(test)]
mod store_link_tests {
use super::*;
#[test]
fn test_get_id_steam() {
let store = StoreLink {
store: Store::Steam,
url: "https://store.steampowered.com/app/1878910/LoupLaine/".to_string(),
};
assert_eq!(store.get_id(), Some(1878910));

let store = StoreLink {
store: Store::Steam,
url: "https://store.steampowered.com/app/1878910".to_string(),
};
assert_eq!(store.get_id(), Some(1878910));

let store = StoreLink {
store: Store::Steam,
url: "https://store.steampowered.com/app/1878910/".to_string(),
};
assert_eq!(store.get_id(), Some(1878910));

let store = StoreLink {
store: Store::Steam,
url: "https://store.steampowered.com/app/1878910/LoupLaine".to_string(),
};
assert_eq!(store.get_id(), Some(1878910));
}
#[test]
fn test_get_id_gog() {
let store = StoreLink {
store: Store::Gog,
url: "https://store.steampowered.com/app/1878910/LoupLaine/".to_string(),
};
assert_eq!(store.get_id(), None);
}
}

0 comments on commit ac5fea7

Please sign in to comment.