From 638a67c5e6cfe0f8637b275fd168e1256b7054a4 Mon Sep 17 00:00:00 2001 From: bbhtt Date: Sat, 21 Dec 2024 08:45:06 +0530 Subject: [PATCH 1/3] ostree: Move tests to end of file --- src/ostree.rs | 85 ++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/ostree.rs b/src/ostree.rs index 7dec869..9e0c683 100644 --- a/src/ostree.rs +++ b/src/ostree.rs @@ -835,48 +835,6 @@ impl std::fmt::Display for Delta { ) } } -#[cfg(test)] -mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - - #[test] - fn test_variant_type_strings() { - assert_eq!(type_string_element_len("1"), None); - assert_eq!(type_string_element_len("i"), Some(1)); - assert_eq!(type_string_element_len("s"), Some(1)); - assert_eq!(type_string_element_len("asas"), Some(2)); - assert_eq!(type_string_split("asas"), Some(("as", "as"))); - assert_eq!(type_string_element_len("(ssas)as"), Some(6)); - assert_eq!(type_string_element_len("(ssas"), None); - assert_eq!(type_string_element_len("(ssas)"), Some(6)); - assert_eq!(type_string_element_len("(sa{sv}sas)ias"), Some(11)); - assert_eq!(type_string_split("(ssas)ii"), Some(("(ssas)", "ii"))); - assert_eq!(type_string_split("a{sv}as"), Some(("a{sv}", "as"))); - assert_eq!(type_string_split("a{vv}as"), None); - } - - #[test] - fn test_delta_name() { - assert_eq!( - delta_part_to_hex("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c"), - Ok("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string()) - ); - assert_eq!( - hex_to_delta_part("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97"), - Ok("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c".to_string()) - ); - assert_eq!( - Delta::from_name("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c"), - Ok(Delta { - from: None, - to: "3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string() - }) - ); - assert_eq!(Delta::from_name("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c-3dpOrJG4MNyKHDDGXHpH_zd9NXugnexr5jpvSFQ77S4"), - Ok(Delta { from: Some("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string()), to: "ddda4eac91b830dc8a1c30c65c7a47ff377d357ba09dec6be63a6f48543bed2e".to_string() })); - } -} pub fn list_deltas(repo_path: &path::Path) -> Vec { let deltas_dir = get_deltas_path(repo_path); @@ -1112,3 +1070,46 @@ min-free-space-size=500MB )?; Ok(()) } + +#[cfg(test)] +mod tests { + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn test_variant_type_strings() { + assert_eq!(type_string_element_len("1"), None); + assert_eq!(type_string_element_len("i"), Some(1)); + assert_eq!(type_string_element_len("s"), Some(1)); + assert_eq!(type_string_element_len("asas"), Some(2)); + assert_eq!(type_string_split("asas"), Some(("as", "as"))); + assert_eq!(type_string_element_len("(ssas)as"), Some(6)); + assert_eq!(type_string_element_len("(ssas"), None); + assert_eq!(type_string_element_len("(ssas)"), Some(6)); + assert_eq!(type_string_element_len("(sa{sv}sas)ias"), Some(11)); + assert_eq!(type_string_split("(ssas)ii"), Some(("(ssas)", "ii"))); + assert_eq!(type_string_split("a{sv}as"), Some(("a{sv}", "as"))); + assert_eq!(type_string_split("a{vv}as"), None); + } + + #[test] + fn test_delta_name() { + assert_eq!( + delta_part_to_hex("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c"), + Ok("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string()) + ); + assert_eq!( + hex_to_delta_part("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97"), + Ok("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c".to_string()) + ); + assert_eq!( + Delta::from_name("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c"), + Ok(Delta { + from: None, + to: "3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string() + }) + ); + assert_eq!(Delta::from_name("OkiocD9GLq_Nt660BvWyrH8G62dAvtLv7RPqngWqf5c-3dpOrJG4MNyKHDDGXHpH_zd9NXugnexr5jpvSFQ77S4"), + Ok(Delta { from: Some("3a48a8703f462eafcdb7aeb406f5b2ac7f06eb6740bed2efed13ea9e05aa7f97".to_string()), to: "ddda4eac91b830dc8a1c30c65c7a47ff377d357ba09dec6be63a6f48543bed2e".to_string() })); + } +} From 97af204d85979f6e75cb7917b541257601c466e9 Mon Sep 17 00:00:00 2001 From: bbhtt Date: Sat, 21 Dec 2024 08:47:16 +0530 Subject: [PATCH 2/3] ostree: Fix needless-return clippy lints --- src/ostree.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ostree.rs b/src/ostree.rs index 9e0c683..19736a9 100644 --- a/src/ostree.rs +++ b/src/ostree.rs @@ -172,27 +172,27 @@ impl Variant { } pub fn as_string(&self) -> OstreeResult { - return self.root().parse_as_string(); + self.root().parse_as_string() } pub fn as_string_vec(&self) -> OstreeResult> { - return self.root().parse_as_string_vec(); + self.root().parse_as_string_vec() } pub fn as_u64(&self) -> OstreeResult { - return self.root().parse_as_u64(); + self.root().parse_as_u64() } pub fn as_i32(&self) -> OstreeResult { - return self.root().parse_as_i32(); + self.root().parse_as_i32() } pub fn as_i32_le(&self) -> OstreeResult { - return self.root().parse_as_i32_le(); + self.root().parse_as_i32_le() } pub fn as_bytes(&self) -> &[u8] { - return self.root().parse_as_bytes(); + self.root().parse_as_bytes() } } @@ -458,7 +458,7 @@ impl<'a> SubVariant<'a> { ))); } let array = self.parse_as_variable_width_array(0)?; - return array.iter().map(|v| v.parse_as_string()).collect(); + array.iter().map(|v| v.parse_as_string()).collect() } fn parse_as_bytes(&self) -> &'a [u8] { @@ -637,7 +637,7 @@ pub fn load_commit_file(path: &path::Path) -> OstreeResult { let variant = Variant::new("(a{sv}aya(say)sstayay)".to_string(), contents)?; - return parse_commit(&variant.root()); + parse_commit(&variant.root()) } pub fn get_commit(repo_path: &path::Path, commit: &str) -> OstreeResult { From bb49869cceb6691efd83d20d8d0b0a86091481ac Mon Sep 17 00:00:00 2001 From: bbhtt Date: Sat, 21 Dec 2024 08:57:53 +0530 Subject: [PATCH 3/3] Replace failure with thiserror https://github.com/rust-lang-deprecated/failure/pull/347 --- Cargo.lock | 123 +++++++++++++++++++++++++------------------------- Cargo.toml | 1 + src/errors.rs | 28 ++++++------ src/ostree.rs | 18 ++++---- 4 files changed, 86 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91650ea..58187a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "actix" @@ -377,8 +377,8 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "068a33520e21c1eea89726be4d6b3ce2e6b81046904367e1677287695a043abb" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "syn 1.0.109", ] @@ -530,10 +530,10 @@ dependencies = [ "mime", "mime_guess", "nom 7.1.3", - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "serde", - "syn 2.0.32", + "syn 2.0.90", ] [[package]] @@ -955,9 +955,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74398b79d81e52e130d991afeed9c86034bb1b7735f46d2f5bf7deb261d80303" dependencies = [ "diesel_table_macro_syntax", - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -977,7 +977,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" dependencies = [ - "syn 2.0.32", + "syn 2.0.90", ] [[package]] @@ -1102,8 +1102,8 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "syn 1.0.109", "synstructure", ] @@ -1175,6 +1175,7 @@ dependencies = [ "serde_derive", "serde_json", "tempfile", + "thiserror", "time 0.1.45", "tokio 1.29.1", "tokio-compat", @@ -1337,9 +1338,9 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -1467,9 +1468,9 @@ dependencies = [ "heck", "proc-macro-crate", "proc-macro-error", - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -2041,8 +2042,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cce3325ac70e67bbab5bd837a31cae01f1a6db64e0e744a33cb03a543469ef08" dependencies = [ "migrations_internals", - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", ] [[package]] @@ -2302,9 +2303,9 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -2509,9 +2510,9 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -2582,8 +2583,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "syn 1.0.109", "version_check 0.9.4", ] @@ -2594,8 +2595,8 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "version_check 0.9.4", ] @@ -2616,9 +2617,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -2640,11 +2641,11 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.31" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ - "proc-macro2 1.0.66", + "proc-macro2 1.0.92", ] [[package]] @@ -3146,9 +3147,9 @@ version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -3350,19 +3351,19 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "unicode-ident", ] [[package]] name = "syn" -version = "2.0.32" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "unicode-ident", ] @@ -3378,8 +3379,8 @@ version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "syn 1.0.109", "unicode-xid 0.2.4", ] @@ -3453,9 +3454,9 @@ version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -3667,9 +3668,9 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", ] [[package]] @@ -4183,8 +4184,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae" dependencies = [ "nom 4.2.3", - "proc-macro2 1.0.66", - "quote 1.0.31", + "proc-macro2 1.0.92", + "quote 1.0.37", "syn 1.0.109", ] @@ -4278,9 +4279,9 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", "wasm-bindgen-shared", ] @@ -4302,7 +4303,7 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" dependencies = [ - "quote 1.0.31", + "quote 1.0.37", "wasm-bindgen-macro-support", ] @@ -4312,9 +4313,9 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ - "proc-macro2 1.0.66", - "quote 1.0.31", - "syn 2.0.32", + "proc-macro2 1.0.92", + "quote 1.0.37", + "syn 2.0.90", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 18474df..3890ea2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" tempfile = "3.0" +thiserror = "1.0.43" time = "0.1" tokio = { version = "1.18", features = ["time", "macros", "rt", "rt-multi-thread"] } tokio-compat = { version = "0.1", features = ["rt-full"] } diff --git a/src/errors.rs b/src/errors.rs index 08c604c..3107ded 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -3,13 +3,13 @@ use actix_web::error::BlockingError; use actix_web::http::StatusCode; use actix_web::{error::ResponseError, HttpResponse}; use diesel::result::Error as DieselError; -use failure::Fail; use serde_json::json; use std::io; +use thiserror::Error; -#[derive(Fail, Debug, Clone)] +#[derive(Error, Debug, Clone)] pub enum DeltaGenerationError { - #[fail(display = "{}", _0)] + #[error("{0}")] Failed(String), } @@ -31,12 +31,12 @@ impl From for DeltaGenerationError { } } -#[derive(Fail, Debug, Clone)] +#[derive(Error, Debug, Clone)] pub enum JobError { - #[fail(display = "InternalError: {}", _0)] + #[error("InternalError: {0}")] InternalError(String), - #[fail(display = "DbError: {}", _0)] + #[error("DbError: {0}")] DBError(String), } @@ -95,27 +95,27 @@ impl From for JobError { } } -#[derive(Fail, Debug)] +#[derive(Error, Debug)] pub enum ApiError { - #[fail(display = "Internal Server Error ({})", _0)] + #[error("Internal Server Error ({0})")] InternalServerError(String), - #[fail(display = "NotFound")] + #[error("NotFound")] NotFound, - #[fail(display = "BadRequest: {}", _0)] + #[error("BadRequest: {0}")] BadRequest(String), - #[fail(display = "WrongRepoState({}): {}", _2, _0)] + #[error("WrongRepoState({2}): {0}")] WrongRepoState(String, String, String), - #[fail(display = "WrongPublishedState({}): {}", _2, _0)] + #[error("WrongPublishedState({2}): {0}")] WrongPublishedState(String, String, String), - #[fail(display = "InvalidToken: {}", _0)] + #[error("InvalidToken: {0}")] InvalidToken(String), - #[fail(display = "NotEnoughPermissions")] + #[error("NotEnoughPermissions")] NotEnoughPermissions(String), } diff --git a/src/ostree.rs b/src/ostree.rs index 19736a9..916c65f 100644 --- a/src/ostree.rs +++ b/src/ostree.rs @@ -1,6 +1,5 @@ use base64::{engine::general_purpose, Engine as _}; use byteorder::{ByteOrder, LittleEndian, NativeEndian}; -use failure::Fail; use futures::future; use futures::future::Either; use futures::Future; @@ -16,24 +15,25 @@ use std::thread::sleep; use std::time::Duration; use std::{collections::HashMap, path::Path}; use std::{fs, io}; +use thiserror::Error; use tokio_process::CommandExt; use walkdir::WalkDir; -#[derive(Fail, Debug, Clone, Eq, PartialEq)] +#[derive(Error, Debug, Clone, Eq, PartialEq)] pub enum OstreeError { - #[fail(display = "No such ref: {}", _0)] + #[error("No such ref: {0}")] NoSuchRef(String), - #[fail(display = "No such commit: {}", _0)] + #[error("No such commit: {0}")] NoSuchCommit(String), - #[fail(display = "No such object: {}", _0)] + #[error("No such object: {0}")] NoSuchObject(String), - #[fail(display = "Invalid utf8 string")] + #[error("Invalid utf8 string")] InvalidUtf8, - #[fail(display = "Command {} failed to start: {}", _0, _1)] + #[error("Command {0} failed to start: {1}")] ExecFailed(String, String), - #[fail(display = "Command {} exited unsucessfully with stderr: {}", _0, _1)] + #[error("Command {0} exited unsuccessfully with stderr: {1}")] CommandFailed(String, String), - #[fail(display = "Internal Error: {}", _0)] + #[error("Internal Error: {0}")] InternalError(String), }