Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion rust/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@crates//:defs.bzl", "all_crate_deps")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rustfmt_config")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_shared_library", "rust_test", "rustfmt_config")

rustfmt_config(
name = "enable-rustfmt",
Expand Down Expand Up @@ -96,6 +96,18 @@ rust_library(
deps = all_crate_deps(normal = True),
)

rust_shared_library(
# The name here is used as the crate name
name = "selenium_manager_shared",
srcs = glob(
["src/**/*.rs"],
exclude = ["main.rs"],
),
edition = "2021",
visibility = ["//rust:__subpackages__"],
deps = all_crate_deps(normal = True),
)

filegroup(
name = "selenium_manager_srcs",
srcs = [
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "e68bc8d92875cfa0a3769e6e31b05c99d2669f20fda466dd415ef9dcbd361b7d",
"checksum": "04c2f1c8b7c0ac539b226b5da83b0b41311961e3c8b2fed1a3dfae76721fec9c",
"crates": {
"addr2line 0.21.0": {
"name": "addr2line",
Expand Down
7 changes: 7 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ description = """
Selenium Manager is a CLI tool that automatically manages the browser/driver infrastructure required by Selenium.
"""

[[bin]]
name = "selenium-manager"

[lib]
name = "selenium_manager"
crate-type = ["cdylib", "rlib"]

[dependencies]
clap = { version = "4.5.23", features = ["derive", "cargo"] }
log = "0.4.22"
Expand Down
2 changes: 2 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ load(
"//rust/private:rustfmt_wrapper.bzl",
_rust_binary = "rust_binary",
_rust_library = "rust_library",
_rust_shared_library = "rust_shared_library",
_rust_test = "rust_test",
_rust_test_suite = "rust_test_suite",
)

rust_binary = _rust_binary
rust_library = _rust_library
rust_shared_library = _rust_shared_library
rust_test = _rust_test
rust_test_suite = _rust_test_suite
rustfmt_config = _rustfmt_config
5 changes: 5 additions & 0 deletions rust/private/rustfmt_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(
"rustfmt_test",
_rust_binary = "rust_binary",
_rust_library = "rust_library",
_rust_shared_library = "rust_shared_library",
_rust_test = "rust_test",
_rust_test_suite = "rust_test_suite",
)
Expand All @@ -27,6 +28,10 @@ def rust_library(name, **kwargs):
_rust_library(name = name, **kwargs)
_wrap_with_fmt_test(name, kwargs.get("tags", []))

def rust_shared_library(name, **kwargs):
_rust_shared_library(name = name, **kwargs)
_wrap_with_fmt_test(name, kwargs.get("tags", []))

def rust_binary(name, **kwargs):
_rust_binary(name = name, **kwargs)
_wrap_with_fmt_test(name, kwargs.get("tags", []))
Expand Down
77 changes: 77 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,3 +1674,80 @@ fn get_index_version(full_version: &str, index: usize) -> Result<String, Error>
.ok_or(anyhow!(format!("Wrong version: {}", full_version)))?
.to_string())
}

// ----------------------------------------------------------
// Exported functions
// ----------------------------------------------------------

// this is callback function to be called each time when rust wants to send log data
type LogCallback = extern "C" fn(level: std::ffi::c_int, message: *const std::os::raw::c_char);

#[repr(C)]
pub struct WebDriverPathResult {
success: bool,
driver_path: *mut std::ffi::c_char,
error: *mut std::ffi::c_char,
}

// this is just an example how to expose function for external usage
#[no_mangle]
pub extern "C" fn get_dummy_webdriver_path(driver_name: *const std::ffi::c_char, log: LogCallback) -> WebDriverPathResult {
let result = std::panic::catch_unwind(|| {

for i in 1..6 {
let message = std::ffi::CString::new("Hello, I am logging message").unwrap();
//let message = std::ffi::CString::new(String::from("A").repeat(10_000_000)).unwrap();
log(i, message.as_ptr());
}

//panic!("Intentional panic for testing");

let driver = unsafe { std::ffi::CStr::from_ptr(driver_name).to_str().unwrap() };

return std::ffi::CString::new("This is dummy driver path for ".to_owned() + driver).unwrap().into_raw();
//return std::ffi::CString::new(String::from("A").repeat(10_000_000)).unwrap().into_raw();
});

match result {
Ok(driver_path) => WebDriverPathResult {
success: true,
driver_path,
error: std::ptr::null_mut(),
},
Err(panic) => WebDriverPathResult {
success: false,
driver_path: std::ptr::null_mut(),
error: std::ffi::CString::new(extract_panic_message(panic)).unwrap().into_raw(),
}
}
}

#[no_mangle]
pub extern "C" fn free_webdriver_path_result(result: *mut WebDriverPathResult) {
if result.is_null() {
return;
}
unsafe {
let ffi_result = &mut *result;
if !ffi_result.driver_path.is_null() {
// Reconstruct CString to drop it and free memory
let _ = std::ffi::CString::from_raw(ffi_result.driver_path);
}
if !ffi_result.error.is_null() {
// Reconstruct CString to drop it and free memory
let _ = std::ffi::CString::from_raw(ffi_result.error);
}
}
}

/// Extract panic message from `Box<dyn Any>`
fn extract_panic_message(panic: Box<dyn std::any::Any + Send>) -> String {
// Try to downcast to common panic types
if let Some(s) = panic.downcast_ref::<String>() {
s.clone()
} else if let Some(s) = panic.downcast_ref::<&str>() {
s.to_string()
} else {
"Unknown panic (non-string payload)".to_string()
}
}