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

remove regex dependency #1817

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update to `hmac` 0.10
- Update to `hyper-rustls` 0.21.
- Disable `chrono`'s `oldtime` feature
- Remove dependency on `regex`


## [0.45.0] - 2020-07-22
Expand Down
31 changes: 18 additions & 13 deletions rusoto/credential/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::path::{Path, PathBuf};

use async_trait::async_trait;
use dirs_next::home_dir;
use regex::Regex;
use serde::Deserialize;
use tokio::process::Command;

Expand Down Expand Up @@ -253,9 +252,20 @@ fn parse_credential_process_output(v: &[u8]) -> Result<AwsCredentials, Credentia
}
}

// should probably constantize with lazy_static!
fn new_profile_regex() -> Regex {
Regex::new(r"^\[(profile )?([^\]]+)\]$").expect("Failed to compile regex")
/// Parses a profile header, returning the profile name.
fn parse_profile_name(line: &str) -> Option<&str> {
// NOTE(benesch): should this handle whitespace inside the brackets? Seems
// like we should maybe use a proper INI parser here? Someone should
// investigate how other AWS SDKs work.
if let Some(line) = line.trim().strip_suffix("]") {
if let Some(profile_name) = line.strip_prefix("[profile ") {
return Some(profile_name);
}
if let Some(profile_name) = line.strip_prefix("[") {
return Some(profile_name);
}
}
None
}

fn parse_config_file(file_path: &Path) -> Option<HashMap<String, HashMap<String, String>>> {
Expand All @@ -267,7 +277,6 @@ fn parse_config_file(file_path: &Path) -> Option<HashMap<String, HashMap<String,
}
}
};
let profile_regex = new_profile_regex();
let file = File::open(file_path).expect("expected file");
let file_lines = BufReader::new(&file);
let result: (HashMap<String, HashMap<String, String>>, Option<String>) = file_lines
Expand All @@ -279,10 +288,8 @@ fn parse_config_file(file_path: &Path) -> Option<HashMap<String, HashMap<String,
.find(|l| !l.starts_with('#') && !l.is_empty())
})
.fold(Default::default(), |(mut result, profile), line| {
if profile_regex.is_match(&line) {
let caps = profile_regex.captures(&line).unwrap();
let next_profile = caps.get(2).map(|value| value.as_str().to_string());
(result, next_profile)
if let Some(next_profile) = parse_profile_name(&line) {
(result, Some(next_profile.to_owned()))
} else {
match &line
.splitn(2, '=')
Expand Down Expand Up @@ -326,7 +333,6 @@ fn parse_credentials_file(

let file = File::open(file_path)?;

let profile_regex = new_profile_regex();
let mut profiles: HashMap<String, AwsCredentials> = HashMap::new();
let mut access_key: Option<String> = None;
let mut secret_key: Option<String> = None;
Expand All @@ -349,7 +355,7 @@ fn parse_credentials_file(
}

// handle the opening of named profile blocks
if profile_regex.is_match(&unwrapped_line) {
if let Some(new_profile_name) = parse_profile_name(&unwrapped_line) {
if let (Some(profile), Some(access), Some(secret)) =
(profile_name, access_key, secret_key)
{
Expand All @@ -361,8 +367,7 @@ fn parse_credentials_file(
secret_key = None;
token = None;

let caps = profile_regex.captures(&unwrapped_line).unwrap();
profile_name = Some(caps.get(2).unwrap().as_str().to_string());
profile_name = Some(new_profile_name.to_owned());
continue;
}

Expand Down