Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start review scary rust code from decode_github_content: #229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

uselessgoddess
Copy link
Member

  • keep old deprecated function
  • use normal fn args instead of struct
  • avoid long names
  • add rustfmt.toml and deny clippy lints

- keep old `deprecated` function
- use normal `fn` args instead of `struct`
- avoid long names
- add `rustfmt.toml` and deny clippy lints
pub struct DecodeGithubContentArgument<'a> {
pub content: &'a String,
}

pub fn decode_github_content(argument: &DecodeGithubContentArgument<'_>) -> Option<String> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to pass struct by ref, it just adds another one ref level for each of its fields:

  • content will be &&String


#[deprecated]
pub struct DecodeGithubContentArgument<'a> {
pub content: &'a String,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To pass string as view you have to decide:

  • you should pass as &str if you want only read some data
  • pass as String if you want to use allocated space in this string

Comment on lines -9 to -11
let DecodeGithubContentArgument {
content
} = argument;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can match in fn sig - any let .. = .. and ..: Type is always pattern matching
let expr and fn arg

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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwrapping it here is bad, this function returns Option.

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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base64 always use ASCII chars – String::from_utf8_lossy is unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cow::into_owned also is bad

Copy link
Member Author

@uselessgoddess uselessgoddess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notes for @FreePhoenix888

extern crate core;
#![deny(clippy::all, clippy::perf)] // `clippy::perf` will teach you to write good code always

pub(crate) use anyhow::Result;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyhow::Result supports custom errors, so we can override std::Result by it

@uselessgoddess uselessgoddess self-assigned this Apr 8, 2023
@uselessgoddess uselessgoddess added the rust Pull requests that update Rust code label Apr 8, 2023
@uselessgoddess
Copy link
Member Author

@FreePhoenix888 where is your review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant