Skip to content

Commit

Permalink
perf: utilize tweaks.json to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Mar 3, 2024
1 parent 0ed2c67 commit ae71ad0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ pkgs.mkShell {
inherit buildInputs;

RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
# RUST_LOG = "trace";
RUST_LOG = "trace";
}
5 changes: 3 additions & 2 deletions src/commands/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pub fn command(app_id: Option<String>) -> Result<(), String> {
apps
};

let tweaks_list = tweaks::list();

let tweaked_apps = apps
.iter()
.filter(|app| tweaks_list.tweaks.contains(app))
.map(|app_id| tweaks::get(&app_id))
.filter(|app| app.is_some())
.map(|app| app.unwrap())
.collect::<Vec<App>>();

let (mut tweaks_applied, mut total_tweaks) = (0, 0);
Expand Down
40 changes: 29 additions & 11 deletions src/tweaks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,48 @@ pub struct Tweaks {
pub fonts: Option<Vec<String>>,
}

pub fn get_app_url(app_id: &str) -> Url {
#[derive(Debug, Deserialize)]
pub struct TweaksList {
pub sha: String,
pub short_sha: String,
pub tweaks: Vec<String>,
}

pub fn url(path: &str) -> Url {
let url = Url::parse(PROTONTWEAKS_DB).unwrap();

return url.join(&format!("{app_id}.json")).unwrap();
return url.join(&format!("{path}.json")).unwrap();
}

pub fn list() -> TweaksList {
let url = url("tweaks");

debug!("Requesting tweaks from '{url}'...");

let response = reqwest::blocking::get(url).unwrap();

trace!("Response received!");

response.json::<TweaksList>().unwrap()
}

pub fn get(app_id: &str) -> Option<App> {
let url = get_app_url(app_id);
pub fn get(app_id: &str) -> App {
let url = url(app_id);

debug!("Requesting file from '{url}'...");

let Ok(response) = reqwest::blocking::get(url) else {
return None;
};
let response =
reqwest::blocking::get(url).expect(&format!("Failed to get tweaks for {}", app_id));

trace!("Response received!");

let Ok(mut app) = response.json::<App>() else {
return None;
};
let mut app = response
.json::<App>()
.expect(&format!("Failed to parse tweak data for {}", app_id));

app.id = app_id.to_string();

Some(app)
app
}

pub fn apply(app: &App) -> Result<(u32, u32), String> {
Expand Down

0 comments on commit ae71ad0

Please sign in to comment.