Skip to content

Commit

Permalink
[rust] Make public the function for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Feb 2, 2023
1 parent 764aff6 commit e92ab45
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
26 changes: 12 additions & 14 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
};
use crate::{
create_default_http_client, SeleniumManager, BETA, DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA,
ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, FALLBACK_RETRIES, NIGHTLY, REG_QUERY, STABLE,
WMIC_COMMAND, WMIC_COMMAND_ENV,
create_default_http_client, format_one_arg, format_two_args, SeleniumManager, BETA,
DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA, ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86,
FALLBACK_RETRIES, NIGHTLY, REG_QUERY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
};

const BROWSER_NAME: &str = "chrome";
Expand Down Expand Up @@ -121,26 +121,24 @@ impl SeleniumManager for ChromeManager {
Some(path) => {
browser_path = path;
commands = vec![
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
self.format_two_args(WMIC_COMMAND_ENV, ENV_LOCALAPPDATA, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_LOCALAPPDATA, browser_path),
];
if !self.is_browser_version_unstable() {
commands.push(
self.format_one_arg(
REG_QUERY,
r#"HKCU\Software\Google\Chrome\BLBeacon"#,
),
);
commands.push(format_one_arg(
REG_QUERY,
r#"HKCU\Software\Google\Chrome\BLBeacon"#,
));
}
}
_ => return None,
}
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
commands = vec![format_one_arg(WMIC_COMMAND, browser_path)];
}
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
commands = vec![format_one_arg(DASH_DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}
Expand Down
26 changes: 12 additions & 14 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
};
use crate::{
create_default_http_client, Logger, SeleniumManager, BETA, DASH_DASH_VERSION, DEV,
ENV_LOCALAPPDATA, ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY, REG_QUERY, STABLE,
WMIC_COMMAND, WMIC_COMMAND_ENV,
create_default_http_client, format_one_arg, format_two_args, Logger, SeleniumManager, BETA,
DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA, ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY,
REG_QUERY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
};

const BROWSER_NAME: &str = "edge";
Expand Down Expand Up @@ -121,26 +121,24 @@ impl SeleniumManager for EdgeManager {
Some(path) => {
browser_path = path;
commands = vec![
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
self.format_two_args(WMIC_COMMAND, ENV_LOCALAPPDATA, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
format_two_args(WMIC_COMMAND, ENV_LOCALAPPDATA, browser_path),
];
if !self.is_browser_version_unstable() {
commands.push(
self.format_one_arg(
REG_QUERY,
r#"HKCU\Software\Microsoft\Edge\BLBeacon"#,
),
);
commands.push(format_one_arg(
REG_QUERY,
r#"HKCU\Software\Microsoft\Edge\BLBeacon"#,
));
}
}
_ => return None,
}
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
commands = vec![format_one_arg(WMIC_COMMAND, browser_path)];
}
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
commands = vec![format_one_arg(DASH_DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}
Expand Down
13 changes: 7 additions & 6 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
};
use crate::{
create_default_http_client, Logger, SeleniumManager, BETA, DASH_VERSION, DEV,
ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
create_default_http_client, format_one_arg, format_two_args, Logger, SeleniumManager, BETA,
DASH_VERSION, DEV, ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY, STABLE, WMIC_COMMAND,
WMIC_COMMAND_ENV,
};

const BROWSER_NAME: &str = "firefox";
Expand Down Expand Up @@ -120,17 +121,17 @@ impl SeleniumManager for FirefoxManager {
Some(path) => {
browser_path = path;
commands = vec![
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
self.format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES, browser_path),
format_two_args(WMIC_COMMAND_ENV, ENV_PROGRAM_FILES_X86, browser_path),
];
}
_ => return None,
}
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
commands = vec![format_one_arg(WMIC_COMMAND, browser_path)];
}
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_VERSION, browser_path)]
commands = vec![format_one_arg(DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}
Expand Down
29 changes: 15 additions & 14 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ pub trait SeleniumManager {
}

fn find_driver_in_path(&self) -> (Option<String>, Option<String>) {
match self.run_shell_command_with_log(
self.format_one_arg(DASH_DASH_VERSION, self.get_driver_name()),
) {
match self
.run_shell_command_with_log(format_one_arg(DASH_DASH_VERSION, self.get_driver_name()))
{
Ok(output) => {
let parsed_version = parse_version(output).unwrap_or_default();
if !parsed_version.is_empty() {
Expand All @@ -232,9 +232,10 @@ pub trait SeleniumManager {
} else {
WHICH_COMMAND
};
let driver_path = match self.run_shell_command_with_log(
self.format_one_arg(which_command, self.get_driver_name()),
) {
let driver_path = match self.run_shell_command_with_log(format_one_arg(
which_command,
self.get_driver_name(),
)) {
Ok(path) => Some(path),
Err(_) => None,
};
Expand Down Expand Up @@ -310,14 +311,6 @@ pub trait SeleniumManager {
get_index_version(full_version, 1)
}

fn format_one_arg(&self, string: &str, arg1: &str) -> String {
string.replacen("{}", arg1, 1)
}

fn format_two_args(&self, string: &str, arg1: &str, arg2: &str) -> String {
string.replacen("{}", arg1, 1).replacen("{}", arg2, 2)
}

// ----------------------------------------------------------
// Getters and setters for configuration parameters
// ----------------------------------------------------------
Expand Down Expand Up @@ -520,3 +513,11 @@ pub fn run_shell_command(os: &str, command: String) -> Result<String, Box<dyn Er
.to_string(),
)
}

pub fn format_one_arg(string: &str, arg1: &str) -> String {
string.replacen("{}", arg1, 1)
}

pub fn format_two_args(string: &str, arg1: &str, arg2: &str) -> String {
string.replacen("{}", arg1, 1).replacen("{}", arg2, 2)
}

0 comments on commit e92ab45

Please sign in to comment.