Skip to content

Commit

Permalink
feat: fallback to default values for Linux field, add json files tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imLinguin committed Mar 31, 2023
1 parent 03c5ca7 commit 6f09c77
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extern crate rocket;
use rocket::{http::Status, serde::json};
use structs::{GOGConfig, PlatformConfig};
use tokio::fs;

mod structs;
mod utils;
Expand All @@ -22,13 +21,13 @@ async fn get_config(id: String) -> (Status, Option<json::Json<GOGConfig>>) {

let mut gog_config = gog_config.unwrap();

let data_file = fs::canonicalize(format!("./games_data/{id}.json")).await; // This will return error if path doesn't exist
if let Ok(data_file) = data_file {
let raw_data = fs::read_to_string(&data_file).await.unwrap();
if let Ok(config) = serde_json::from_str::<PlatformConfig>(&raw_data) {
gog_config.content.linux = Some(config);
};
match utils::read_linux_config(id).await {
Some(linux_config) => {
gog_config.content.linux = Some(linux_config);
}
None => gog_config.content.linux = Some(PlatformConfig::default()),
}

(Status::Ok, Some(json::Json(gog_config)))
}

Expand Down
12 changes: 12 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ pub struct PlatformConfig {
pub cloud_storage: CloudStorage,
}

impl PlatformConfig {
pub fn default() -> PlatformConfig {
PlatformConfig {
overlay: Overlay { supported: false },
cloud_storage: CloudStorage {
enabled: false,
locations: Vec::new(),
},
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GOGConfigContent {
#[serde(rename = "MacOS")]
Expand Down
36 changes: 35 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::structs::GOGConfig;
use crate::structs::{GOGConfig, PlatformConfig};
use tokio::fs;

pub async fn get_gog_remote_config(id: String) -> Option<GOGConfig> {
let response = reqwest::get(format!("https://remote-config.gog.com/components/galaxy_client/clients/{id}?component_version=2.0.50")).await;
Expand All @@ -15,3 +16,36 @@ pub async fn get_gog_remote_config(id: String) -> Option<GOGConfig> {
Err(_) => None,
}
}

pub async fn read_linux_config(id: String) -> Option<PlatformConfig> {
let data_file = fs::canonicalize(format!("./games_data/{id}.json")).await; // This will return error if path doesn't exist
if let Ok(data_file) = data_file {
let raw_data = fs::read_to_string(&data_file).await.unwrap();
if let Ok(config) = serde_json::from_str::<PlatformConfig>(&raw_data) {
return Some(config);
};
}
None
}

#[async_test]
async fn check_configs() {
let mut read_dir = fs::read_dir("./games_data/")
.await
.expect("Failed to read games data config");

loop {
let file = read_dir.next_entry().await;
if file.is_err() {
break;
}

if let Some(file) = file.unwrap() {
let path = file.path();
let raw_data = fs::read_to_string(&path).await.unwrap();
serde_json::from_str::<PlatformConfig>(&raw_data).expect("Failed to parse json file");
} else {
break;
}
}
}

0 comments on commit 6f09c77

Please sign in to comment.