-
Notifications
You must be signed in to change notification settings - Fork 32
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
error_on_line_overflow = true | ||
error_on_unformatted = true | ||
version = "Two" | ||
|
||
imports_granularity = "One" | ||
use_small_heuristics = "Max" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to pass
|
||
let DecodeGithubContentArgument { | ||
content | ||
} = argument; | ||
Comment on lines
-9
to
-11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unwrapping it here is bad, this function returns |
||
Some(String::from_utf8_lossy(&decoded_content).into_owned()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
#[deprecated(note = "keep `decode_github_content` for soft migration into `decode`")] | ||
pub fn decode_github_content( | ||
&DecodeGithubContentArgument { content }: &DecodeGithubContentArgument, | ||
) -> Option<String> { | ||
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<String, DecodeError> { | ||
// 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() | ||
})?) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
use std::error::Error; | ||
use std::iter::zip; | ||
|
There was a problem hiding this comment.
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:
&str
if you want only read some dataString
if you want to use allocated space in this string