Skip to content

Commit

Permalink
GH-36: Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SetZero committed May 14, 2023
1 parent 4d72087 commit 1de6377
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rust-mumble-app",
"private": true,
"version": "0.1.4",
"version": "0.1.5",
"type": "module",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "app"
version = "0.1.4"
version = "0.1.5"
description = "Fancy Mumble"
authors = ["you"]
license = "MIT"
Expand Down
45 changes: 20 additions & 25 deletions src-tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::path::Path;
use std::str;
use tokio::runtime::Runtime;

const DOWNLOAD_MUMBLE_PROTO_DIR: &'static str =
const DOWNLOAD_MUMBLE_PROTO_DIR: &str =
"https://raw.githubusercontent.com/mumble-voip/mumble/82bcd1eb3d53aa9bfc1f6ff539961b0c29336266/src/Mumble.proto";
const MUMBLE_PROTO_SHA256: &'static str =
const MUMBLE_PROTO_SHA256: &str =
"0f86d85938ff2268e3eb05ce0120805fb049ad0d062f4d01c6657b048dcc9245";
const PATCHED_MUMBLE_PROTO_HASH: &'static str =
const PATCHED_MUMBLE_PROTO_HASH: &str =
"ebadea7bcb720da05149076b1b0ec7a9ff1107a5107a4137b75e8e45fb52f68d";

fn apply(diff: Patch, old: &str) -> String {
Expand All @@ -20,7 +20,7 @@ fn apply(diff: Patch, old: &str) -> String {
let mut old_line = 0;
for hunk in diff.hunks {
while old_line < hunk.old_range.start - 1 {
out.push(old_lines[old_line as usize]);
out.push(old_lines[usize::try_from(old_line).expect("usize::try_from failed")]);
old_line += 1;
}
old_line += hunk.old_range.count;
Expand All @@ -32,7 +32,7 @@ fn apply(diff: Patch, old: &str) -> String {
}
}
while old_line < old_lines.len() as u64 {
out.push(old_lines[old_line as usize]);
out.push(old_lines[usize::try_from(old_line).expect("usize::try_from failed")]);
old_line += 1;
}
out.push(""); // add a newline at the end
Expand All @@ -44,18 +44,16 @@ fn get_data_hash_str(data: &[u8]) -> String {
hasher.update(data);
let file_hash = hasher.finalize();

format!("{:x}", file_hash)
format!("{file_hash:x}")
}

fn read_file_as_bytes(file_path: &Path) -> Result<String, Box<dyn std::error::Error>> {
fs::read_to_string(file_path).map_err(|e| e.into())
fs::read_to_string(file_path).map_err(std::convert::Into::into)
}

fn write_to_file(data: &[u8], file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
fn write_to_file(data: &[u8], file_path: &Path) {
let mut file = File::create(file_path).expect("Failed to create file");
file.write_all(data).expect("Failed to write file");

Ok(())
}

async fn download_file(
Expand All @@ -69,42 +67,39 @@ async fn download_file(
let response_bytes = response.as_bytes();

let file_hash = get_data_hash_str(response_bytes);
if &file_hash != sha256 {
if file_hash != sha256 {
return Err(format!(
"File hash does not match expected hash: {file_hash}, actual hash: {sha256}"
)
.into());
}

write_to_file(response_bytes, file_path)?;
write_to_file(response_bytes, file_path);

Ok(response_bytes.to_vec())
}

fn main() -> io::Result<()> {
let mumble_proto_path = Path::new("src/proto/Mumble.proto");
let mumble_proto_patch = Path::new("src/proto/Mumble.proto.patch");
let mumble_proto = Path::new("src/proto/Mumble.proto");
let patch_file = Path::new("src/proto/Mumble.proto.patch");

let mumble_proto_bytes = read_file_as_bytes(mumble_proto_path).unwrap_or_default();
let hash = get_data_hash_str(&mumble_proto_bytes.as_bytes());
let mumble_proto_bytes = read_file_as_bytes(mumble_proto).unwrap_or_default();
let hash = get_data_hash_str(mumble_proto_bytes.as_bytes());

// download Mumble proto and patch it
if hash != PATCHED_MUMBLE_PROTO_HASH {
let rt = Runtime::new()?;
rt.block_on(async {
let resonse_file = download_file(
DOWNLOAD_MUMBLE_PROTO_DIR,
MUMBLE_PROTO_SHA256,
mumble_proto_path,
)
.await
.expect("Failed to download Mumble.proto");
let resonse_file =
download_file(DOWNLOAD_MUMBLE_PROTO_DIR, MUMBLE_PROTO_SHA256, mumble_proto)
.await
.expect("Failed to download Mumble.proto");
let response_str = str::from_utf8(&resonse_file).expect("Failed to parse response");

let patch_output = read_file_as_bytes(mumble_proto_patch).expect("Failed to read file");
let patch_output = read_file_as_bytes(patch_file).expect("Failed to read file");
let patch = Patch::from_single(patch_output.as_str()).expect("Failed to parse patch");
let new_content = apply(patch, response_str);
write_to_file(new_content.as_bytes(), mumble_proto_path).expect("Failed to write file");
write_to_file(new_content.as_bytes(), mumble_proto);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "Fancy Mumble",
"version": "0.1.4"
"version": "0.1.5"
},
"tauri": {
"allowlist": {
Expand Down

0 comments on commit 1de6377

Please sign in to comment.