diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ff1927af..9d0513d9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,6 +14,18 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - name: Caching + uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - name: Format + run: cargo fmt + - name: Clippy + run: cargo clippy - name: Test run: cargo test build: diff --git a/src/main.rs b/src/main.rs index e14f9546..06f4074f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse { for route in routes.routes.iter() { if route.path == req.path() { - let body_str = String::from_utf8(body.to_vec()).unwrap_or(String::from("")); + let body_str = String::from_utf8(body.to_vec()).unwrap_or_else(|_| String::from("")); // Init KV let kv_namespace = match &route.config { @@ -68,10 +68,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse { let connector = data_connectors.read().unwrap(); let kv_store = connector.kv.find_store(namespace); - match kv_store { - Some(store) => Some(store.clone()), - None => None, - } + kv_store.map(|store| store.clone()) } None => None, }; @@ -95,7 +92,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse { for (key, val) in handler_result.headers.iter() { // Note that QuickJS is replacing the "-" character // with "_" on property keys. Here, we rollback it - builder.insert_header((key.replace("_", "-").as_str(), val.as_str())); + builder.insert_header((key.replace('_', "-").as_str(), val.as_str())); } // Write to the state if required diff --git a/src/router.rs b/src/router.rs index 0fa527cd..356ecd12 100644 --- a/src/router.rs +++ b/src/router.rs @@ -57,14 +57,14 @@ impl Route { Self { path: Self::retrieve_route(base_path, &filepath), handler: filepath, - runner: runner, - config: config, + runner, + config, } } // Process the given path to return the proper route for the API. // It will transform paths like test/index.wasm into /test. - fn retrieve_route(base_path: &Path, path: &PathBuf) -> String { + fn retrieve_route(base_path: &Path, path: &Path) -> String { // TODO: Improve this entire method // @ref #13 if let Some(api_path) = path.to_str() { @@ -72,7 +72,7 @@ impl Route { .to_string() .replace(".wasm", "") .replace(".js", "") - .replace(base_path.to_str().unwrap_or_else(|| "./"), ""); + .replace(base_path.to_str().unwrap_or("./"), ""); let mut normalized = String::from("/") + &parsed_path.replace("index", ""); // Remove trailing / to avoid 404 errors @@ -83,7 +83,7 @@ impl Route { normalized } else { // TODO: Manage better unexpected characters in paths - String::from(path.to_str().unwrap_or_else(|| "/unknown")) + String::from(path.to_str().unwrap_or("/unknown")) } } } @@ -106,7 +106,7 @@ pub fn initialize_routes(base_path: &Path) -> Vec { for entry in glob_items { match entry { Ok(filepath) => { - routes.push(Route::new(&base_path, filepath)); + routes.push(Route::new(base_path, filepath)); } Err(e) => println!("Could not read the file {:?}", e), } diff --git a/src/runner.rs b/src/runner.rs index 15c7876f..85044ef3 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -6,7 +6,7 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use wasi_common::{pipe::ReadPipe, pipe::WritePipe}; use wasmtime::*; use wasmtime_wasi::sync::WasiCtxBuilder; @@ -39,8 +39,8 @@ impl WasmInput { url: request.uri().to_string(), method: String::from(request.method().as_str()), headers: build_headers_hash(request.headers()), - body: body, - kv: kv.unwrap_or(HashMap::new()), + body, + kv: kv.unwrap_or_default(), } } } @@ -113,8 +113,7 @@ impl Runner { RunnerHandlerType::JavaScript, module, fs::read_to_string(path) - .expect(&format!("Error reading {}", path.display())) - .to_string(), + .unwrap_or_else(|_| panic!("Error reading {}", path.display())), ) } else { let module = Module::from_file(&engine, path)?; @@ -130,7 +129,7 @@ impl Runner { }) } - fn is_js_file(path: &PathBuf) -> bool { + fn is_js_file(path: &Path) -> bool { match path.extension() { Some(os_str) => os_str == "js", None => false, @@ -162,9 +161,9 @@ impl Runner { // WASI context let wasi = WasiCtxBuilder::new() - .stdin(Box::new(stdin.clone())) + .stdin(Box::new(stdin)) .stdout(Box::new(stdout.clone())) - .stderr(Box::new(stderr.clone())) + .stderr(Box::new(stderr)) .inherit_args()? .build(); let mut store = Store::new(&self.engine, wasi);