Skip to content

Commit 7014c3e

Browse files
oriongonzaOrionGonzalezdiemol
authored
[rust] Now the logger accepts any kind of type that can be converted to string (#11865)
* Now the logger accepts any kind of type that can be converted to string * Result<(), Box<dyn Error>>, as God intended --------- Co-authored-by: ogonzalez <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent 93a00d5 commit 7014c3e

File tree

9 files changed

+54
-50
lines changed

9 files changed

+54
-50
lines changed

rust/src/chrome.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ pub struct ChromeManager {
4949
}
5050

5151
impl ChromeManager {
52-
pub fn new() -> Result<Box<Self>, String> {
52+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
5353
let browser_name = CHROME_NAME;
5454
let driver_name = CHROMEDRIVER_NAME;
5555
let config = ManagerConfig::default(browser_name, driver_name);
5656
let default_timeout = config.timeout.to_owned();
57-
let default_proxy = config.proxy.to_owned();
57+
let default_proxy = &config.proxy;
5858
Ok(Box::new(ChromeManager {
5959
browser_name,
6060
driver_name,
61-
config,
6261
http_client: create_http_client(default_timeout, default_proxy)?,
62+
config,
6363
log: Logger::default(),
6464
}))
6565
}

rust/src/edge.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ pub struct EdgeManager {
4949
}
5050

5151
impl EdgeManager {
52-
pub fn new() -> Result<Box<Self>, String> {
52+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
5353
let browser_name = EDGE_NAMES[0];
5454
let driver_name = EDGEDRIVER_NAME;
5555
let config = ManagerConfig::default(browser_name, driver_name);
5656
let default_timeout = config.timeout.to_owned();
57-
let default_proxy = config.proxy.to_owned();
57+
let default_proxy = &config.proxy;
5858
Ok(Box::new(EdgeManager {
5959
browser_name,
6060
driver_name,
61-
config,
6261
http_client: create_http_client(default_timeout, default_proxy)?,
62+
config,
6363
log: Logger::default(),
6464
}))
6565
}

rust/src/firefox.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ pub struct FirefoxManager {
4848
}
4949

5050
impl FirefoxManager {
51-
pub fn new() -> Result<Box<Self>, String> {
51+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
5252
let browser_name = FIREFOX_NAME;
5353
let driver_name = GECKODRIVER_NAME;
5454
let config = ManagerConfig::default(browser_name, driver_name);
5555
let default_timeout = config.timeout.to_owned();
56-
let default_proxy = config.proxy.to_owned();
56+
let default_proxy = &config.proxy;
5757
Ok(Box::new(FirefoxManager {
5858
browser_name,
5959
driver_name,
60-
config,
6160
http_client: create_http_client(default_timeout, default_proxy)?,
61+
config,
6262
log: Logger::default(),
6363
}))
6464
}

rust/src/iexplorer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ pub struct IExplorerManager {
5454
}
5555

5656
impl IExplorerManager {
57-
pub fn new() -> Result<Box<Self>, String> {
57+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
5858
let browser_name = IE_NAMES[0];
5959
let driver_name = IEDRIVER_NAME;
6060
let config = ManagerConfig::default(browser_name, driver_name);
6161
let default_timeout = config.timeout.to_owned();
62-
let default_proxy = config.proxy.to_owned();
62+
let default_proxy = &config.proxy;
6363
Ok(Box::new(IExplorerManager {
6464
browser_name,
6565
driver_name,
66-
config,
6766
http_client: create_http_client(default_timeout, default_proxy)?,
67+
config,
6868
log: Logger::default(),
6969
}))
7070
}

rust/src/lib.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ pub trait SeleniumManager {
440440
Ok(())
441441
}
442442

443-
fn update_http_client(&mut self) -> Result<(), String> {
444-
let proxy = self.get_proxy().to_string();
443+
fn update_http_client(&mut self) -> Result<(), Box<dyn Error>> {
444+
let proxy = self.get_proxy();
445445
let timeout = self.get_timeout();
446446
let http_client = create_http_client(timeout, proxy)?;
447447
self.set_http_client(http_client);
@@ -453,7 +453,9 @@ pub trait SeleniumManager {
453453
// Public functions
454454
// ----------------------------------------------------------
455455

456-
pub fn get_manager_by_browser(browser_name: String) -> Result<Box<dyn SeleniumManager>, String> {
456+
pub fn get_manager_by_browser(
457+
browser_name: String,
458+
) -> Result<Box<dyn SeleniumManager>, Box<dyn Error>> {
457459
let browser_name_lower_case = browser_name.to_ascii_lowercase();
458460
if browser_name_lower_case.eq(CHROME_NAME) {
459461
Ok(ChromeManager::new()?)
@@ -468,11 +470,16 @@ pub fn get_manager_by_browser(browser_name: String) -> Result<Box<dyn SeleniumMa
468470
} else if SAFARITP_NAMES.contains(&browser_name_lower_case.as_str()) {
469471
Ok(SafariTPManager::new()?)
470472
} else {
471-
Err(format!("Invalid browser name: {browser_name}"))
473+
Err(Box::new(std::io::Error::new(
474+
std::io::ErrorKind::InvalidInput,
475+
format!("Invalid browser name: {browser_name}"),
476+
)))
472477
}
473478
}
474479

475-
pub fn get_manager_by_driver(driver_name: String) -> Result<Box<dyn SeleniumManager>, String> {
480+
pub fn get_manager_by_driver(
481+
driver_name: String,
482+
) -> Result<Box<dyn SeleniumManager>, Box<dyn Error>> {
476483
if driver_name.eq_ignore_ascii_case(CHROMEDRIVER_NAME) {
477484
Ok(ChromeManager::new()?)
478485
} else if driver_name.eq_ignore_ascii_case(GECKODRIVER_NAME) {
@@ -484,7 +491,10 @@ pub fn get_manager_by_driver(driver_name: String) -> Result<Box<dyn SeleniumMana
484491
} else if driver_name.eq_ignore_ascii_case(SAFARIDRIVER_NAME) {
485492
Ok(SafariManager::new()?)
486493
} else {
487-
Err(format!("Invalid driver name: {driver_name}"))
494+
Err(Box::new(std::io::Error::new(
495+
std::io::ErrorKind::InvalidInput,
496+
format!("Invalid driver name: {driver_name}"),
497+
)))
488498
}
489499
}
490500

@@ -502,20 +512,13 @@ pub fn clear_cache(log: &Logger) {
502512
}
503513
}
504514

505-
pub fn create_http_client(timeout: u64, proxy: String) -> Result<Client, String> {
506-
let mut client_builder = Client::builder()
515+
pub fn create_http_client(timeout: u64, proxy: &str) -> Result<Client, Box<dyn Error>> {
516+
let client_builder = Client::builder()
507517
.danger_accept_invalid_certs(true)
508518
.use_rustls_tls()
509519
.timeout(Duration::from_secs(timeout));
510520
if !proxy.is_empty() {
511-
match Proxy::all(proxy) {
512-
Ok(p) => {
513-
client_builder = client_builder.proxy(p);
514-
}
515-
Err(err) => {
516-
return Err(err.to_string());
517-
}
518-
};
521+
Proxy::all(proxy)?;
519522
}
520523
Ok(client_builder.build().unwrap_or_default())
521524
}

rust/src/logger.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use log::LevelFilter::{Debug, Info, Trace};
2424
use serde::{Deserialize, Serialize};
2525
use std::cell::RefCell;
2626
use std::env;
27+
use std::fmt::Display;
2728
use std::io::Write;
2829
use std::ops::Deref;
2930
use Color::{Blue, Cyan, Green, Red, Yellow};
@@ -62,10 +63,10 @@ pub struct JsonOutput {
6263

6364
impl Logger {
6465
pub fn default() -> Self {
65-
Logger::create("".to_string(), false, false)
66+
Logger::create("", false, false)
6667
}
6768

68-
pub fn create(output: String, debug: bool, trace: bool) -> Self {
69+
pub fn create(output: &str, debug: bool, trace: bool) -> Self {
6970
let output_type;
7071
if output.eq_ignore_ascii_case("json") {
7172
output_type = OutputType::Json;
@@ -130,24 +131,24 @@ impl Logger {
130131
}
131132
}
132133

133-
pub fn error(&self, message: String) {
134-
self.logger(message, Level::Error);
134+
pub fn error<T: Display>(&self, message: T) {
135+
self.logger(message.to_string(), Level::Error);
135136
}
136137

137-
pub fn warn(&self, message: String) {
138-
self.logger(message, Level::Warn);
138+
pub fn warn<T: Display>(&self, message: T) {
139+
self.logger(message.to_string(), Level::Warn);
139140
}
140141

141-
pub fn info(&self, message: String) {
142-
self.logger(message, Level::Info);
142+
pub fn info<T: Display>(&self, message: T) {
143+
self.logger(message.to_string(), Level::Info);
143144
}
144145

145-
pub fn debug(&self, message: String) {
146-
self.logger(message, Level::Debug);
146+
pub fn debug<T: Display>(&self, message: T) {
147+
self.logger(message.to_string(), Level::Debug);
147148
}
148149

149-
pub fn trace(&self, message: String) {
150-
self.logger(message, Level::Trace);
150+
pub fn trace<T: Display>(&self, message: T) {
151+
self.logger(message.to_string(), Level::Trace);
151152
}
152153

153154
fn logger(&self, message: String, level: Level) {

rust/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn main() {
104104
let cli = Cli::parse();
105105
let debug = cli.debug || BooleanKey("debug", false).get_value();
106106
let trace = cli.trace || BooleanKey("trace", false).get_value();
107-
let log = Logger::create(cli.output, debug, trace);
107+
let log = Logger::create(&cli.output, debug, trace);
108108

109109
if cli.clear_cache || BooleanKey("clear-cache", false).get_value() {
110110
clear_cache(&log);
@@ -128,7 +128,7 @@ fn main() {
128128
flush_and_exit(DATAERR, &log);
129129
})
130130
} else {
131-
log.error("You need to specify a browser or driver".to_string());
131+
log.error("You need to specify a browser or driver");
132132
flush_and_exit(DATAERR, &log);
133133
};
134134

@@ -143,8 +143,8 @@ fn main() {
143143
.and_then(|_| selenium_manager.resolve_driver())
144144
.map(|path| {
145145
let log = selenium_manager.get_logger();
146-
log.info(path.display().to_string());
147-
flush_and_exit(OK, log);
146+
log.info(path.display());
147+
flush_and_exit(OK, &log);
148148
})
149149
.unwrap_or_else(|err| {
150150
let log = selenium_manager.get_logger();

rust/src/safari.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ pub struct SafariManager {
3939
}
4040

4141
impl SafariManager {
42-
pub fn new() -> Result<Box<Self>, String> {
42+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
4343
let browser_name = SAFARI_NAME;
4444
let driver_name = SAFARIDRIVER_NAME;
4545
let config = ManagerConfig::default(browser_name, driver_name);
4646
let default_timeout = config.timeout.to_owned();
47-
let default_proxy = config.proxy.to_owned();
47+
let default_proxy = &config.proxy;
4848
Ok(Box::new(SafariManager {
4949
browser_name,
5050
driver_name,
51-
config,
5251
http_client: create_http_client(default_timeout, default_proxy)?,
52+
config,
5353
log: Logger::default(),
5454
}))
5555
}

rust/src/safaritp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ pub struct SafariTPManager {
4444
}
4545

4646
impl SafariTPManager {
47-
pub fn new() -> Result<Box<Self>, String> {
47+
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
4848
let browser_name = SAFARITP_NAMES[0];
4949
let driver_name = SAFARITPDRIVER_NAME;
5050
let config = ManagerConfig::default(browser_name, driver_name);
5151
let default_timeout = config.timeout.to_owned();
52-
let default_proxy = config.proxy.to_owned();
52+
let default_proxy = &config.proxy;
5353
Ok(Box::new(SafariTPManager {
5454
browser_name,
5555
driver_name,
56-
config,
5756
http_client: create_http_client(default_timeout, default_proxy)?,
57+
config,
5858
log: Logger::default(),
5959
}))
6060
}

0 commit comments

Comments
 (0)