From 62a863c8e2b17f96560393298999ff7533cae050 Mon Sep 17 00:00:00 2001 From: uselessgoddess Date: Sat, 8 Apr 2023 23:37:14 +0300 Subject: [PATCH] Start review scary rust code from `decode_github_content`: - keep old `deprecated` function - use normal `fn` args instead of `struct` - avoid long names - add `rustfmt.toml` and deny clippy lints --- .../rustfmt.toml | 6 ++++ .../src/decode_github_content.rs | 36 ++++++++++++------- .../src/main.rs | 4 ++- 3 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 rust/update-conan-recipe-and-create-pull-request/rustfmt.toml diff --git a/rust/update-conan-recipe-and-create-pull-request/rustfmt.toml b/rust/update-conan-recipe-and-create-pull-request/rustfmt.toml new file mode 100644 index 00000000..675a9af2 --- /dev/null +++ b/rust/update-conan-recipe-and-create-pull-request/rustfmt.toml @@ -0,0 +1,6 @@ +error_on_line_overflow = true +error_on_unformatted = true +version = "Two" + +imports_granularity = "One" +use_small_heuristics = "Max" diff --git a/rust/update-conan-recipe-and-create-pull-request/src/decode_github_content.rs b/rust/update-conan-recipe-and-create-pull-request/src/decode_github_content.rs index c38fbf76..aaa6f500 100644 --- a/rust/update-conan-recipe-and-create-pull-request/src/decode_github_content.rs +++ b/rust/update-conan-recipe-and-create-pull-request/src/decode_github_content.rs @@ -1,17 +1,29 @@ -use base64::Engine; -use crate::add_version_to_conandata_yml::AddVersionToConandataYmlArgument; +use {crate::Result, base64::DecodeError}; +#[deprecated] pub struct DecodeGithubContentArgument<'a> { pub content: &'a String, } -pub fn decode_github_content(argument: &DecodeGithubContentArgument<'_>) -> Option { - let DecodeGithubContentArgument { - content - } = argument; - let mut content_without_github_shit = content.as_bytes().to_owned(); - content_without_github_shit.retain(|b| !b" \n\t\r\x0b\x0c".contains(b)); - let decoded_content = base64::engine::general_purpose::STANDARD - .decode(&content_without_github_shit).unwrap(); - Some(String::from_utf8_lossy(&decoded_content).into_owned()) -} \ No newline at end of file +#[deprecated(note = "keep `decode_github_content` for soft migration into `decode`")] +pub fn decode_github_content( + &DecodeGithubContentArgument { content }: &DecodeGithubContentArgument, +) -> Option { + decode( + // This leads to unnecessary cloning + content.clone(), + ) + .ok() +} + +// use `String` because it is useful for `octorust` types +pub fn decode(mut content: String) -> Result { + // base64 alphabet is always utf8 safe + Ok(unsafe { + // `base64::decode` is deprecated but it is very simple use case + String::from_utf8_unchecked(base64::decode({ + content.retain(|b| !b.is_whitespace()); + content.into_bytes() + })?) + }) +} diff --git a/rust/update-conan-recipe-and-create-pull-request/src/main.rs b/rust/update-conan-recipe-and-create-pull-request/src/main.rs index 9240b9e3..85905028 100644 --- a/rust/update-conan-recipe-and-create-pull-request/src/main.rs +++ b/rust/update-conan-recipe-and-create-pull-request/src/main.rs @@ -1,4 +1,6 @@ -extern crate core; +#![deny(clippy::all, clippy::perf)] // `clippy::perf` will teach you to write good code always + +pub(crate) use anyhow::Result; use std::error::Error; use std::iter::zip;