style: apply cargo fmt across entire Rust codebase
- Remove Rust formatting check from CI workflow since formatting is now applied - Standardize import ordering and organization throughout codebase - Fix indentation, spacing, and line breaks for consistency - Clean up trailing whitespace and formatting inconsistencies - Apply rustfmt to all Rust source files including checkpoint, sandbox, commands, and test modules This establishes a consistent code style baseline for the project.
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
use headless_chrome::{Browser, LaunchOptions};
|
||||
use headless_chrome::protocol::cdp::Page;
|
||||
use headless_chrome::{Browser, LaunchOptions};
|
||||
use std::fs;
|
||||
use std::time::Duration;
|
||||
use tauri::AppHandle;
|
||||
|
||||
/// Captures a screenshot of a URL using headless Chrome
|
||||
///
|
||||
///
|
||||
/// This function launches a headless Chrome browser, navigates to the specified URL,
|
||||
/// and captures a screenshot of either the entire page or a specific element.
|
||||
///
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `app` - The Tauri application handle
|
||||
/// * `url` - The URL to capture
|
||||
/// * `selector` - Optional CSS selector for a specific element to capture
|
||||
/// * `full_page` - Whether to capture the entire page or just the viewport
|
||||
///
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<String, String>` - The path to the saved screenshot file, or an error message
|
||||
#[tauri::command]
|
||||
@@ -32,11 +32,10 @@ pub async fn capture_url_screenshot(
|
||||
);
|
||||
|
||||
// Run the browser operations in a blocking task since headless_chrome is not async
|
||||
let result = tokio::task::spawn_blocking(move || {
|
||||
capture_screenshot_sync(url, selector, full_page)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Failed to spawn blocking task: {}", e))?;
|
||||
let result =
|
||||
tokio::task::spawn_blocking(move || capture_screenshot_sync(url, selector, full_page))
|
||||
.await
|
||||
.map_err(|e| format!("Failed to spawn blocking task: {}", e))?;
|
||||
|
||||
// Log the result of the headless Chrome capture before returning
|
||||
match &result {
|
||||
@@ -61,8 +60,8 @@ fn capture_screenshot_sync(
|
||||
};
|
||||
|
||||
// Launch the browser
|
||||
let browser = Browser::new(launch_options)
|
||||
.map_err(|e| format!("Failed to launch browser: {}", e))?;
|
||||
let browser =
|
||||
Browser::new(launch_options).map_err(|e| format!("Failed to launch browser: {}", e))?;
|
||||
|
||||
// Create a new tab
|
||||
let tab = browser
|
||||
@@ -86,14 +85,17 @@ fn capture_screenshot_sync(
|
||||
// Wait explicitly for the <body> element to exist – this often prevents
|
||||
// "Unable to capture screenshot" CDP errors on some pages
|
||||
if let Err(e) = tab.wait_for_element("body") {
|
||||
log::warn!("Timed out waiting for <body> element: {} – continuing anyway", e);
|
||||
log::warn!(
|
||||
"Timed out waiting for <body> element: {} – continuing anyway",
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
// Capture the screenshot
|
||||
let screenshot_data = if let Some(selector) = selector {
|
||||
// Wait for the element and capture it
|
||||
log::info!("Waiting for element with selector: {}", selector);
|
||||
|
||||
|
||||
let element = tab
|
||||
.wait_for_element(&selector)
|
||||
.map_err(|e| format!("Failed to find element '{}': {}", selector, e))?;
|
||||
@@ -103,8 +105,11 @@ fn capture_screenshot_sync(
|
||||
.map_err(|e| format!("Failed to capture element screenshot: {}", e))?
|
||||
} else {
|
||||
// Capture the entire page or viewport
|
||||
log::info!("Capturing {} screenshot", if full_page { "full page" } else { "viewport" });
|
||||
|
||||
log::info!(
|
||||
"Capturing {} screenshot",
|
||||
if full_page { "full page" } else { "viewport" }
|
||||
);
|
||||
|
||||
// Get the page dimensions for full page screenshot
|
||||
let clip = if full_page {
|
||||
// Execute JavaScript to get the full page dimensions
|
||||
@@ -132,30 +137,30 @@ fn capture_screenshot_sync(
|
||||
)
|
||||
.map_err(|e| format!("Failed to get page dimensions: {}", e))?;
|
||||
|
||||
// Extract dimensions from the result
|
||||
let width = dimensions
|
||||
.value
|
||||
.as_ref()
|
||||
.and_then(|v| v.as_object())
|
||||
.and_then(|obj| obj.get("width"))
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(1920.0);
|
||||
// Extract dimensions from the result
|
||||
let width = dimensions
|
||||
.value
|
||||
.as_ref()
|
||||
.and_then(|v| v.as_object())
|
||||
.and_then(|obj| obj.get("width"))
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(1920.0);
|
||||
|
||||
let height = dimensions
|
||||
.value
|
||||
.as_ref()
|
||||
.and_then(|v| v.as_object())
|
||||
.and_then(|obj| obj.get("height"))
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(1080.0);
|
||||
let height = dimensions
|
||||
.value
|
||||
.as_ref()
|
||||
.and_then(|v| v.as_object())
|
||||
.and_then(|obj| obj.get("height"))
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(1080.0);
|
||||
|
||||
Some(Page::Viewport {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width,
|
||||
height,
|
||||
scale: 1.0,
|
||||
})
|
||||
Some(Page::Viewport {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width,
|
||||
height,
|
||||
scale: 1.0,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -176,13 +181,8 @@ fn capture_screenshot_sync(
|
||||
err
|
||||
);
|
||||
|
||||
tab.capture_screenshot(
|
||||
Page::CaptureScreenshotFormatOption::Png,
|
||||
None,
|
||||
clip,
|
||||
true,
|
||||
)
|
||||
.map_err(|e| format!("Failed to capture screenshot after retry: {}", e))?
|
||||
tab.capture_screenshot(Page::CaptureScreenshotFormatOption::Png, None, clip, true)
|
||||
.map_err(|e| format!("Failed to capture screenshot after retry: {}", e))?
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -208,13 +208,13 @@ fn capture_screenshot_sync(
|
||||
}
|
||||
|
||||
/// Cleans up old screenshot files from the temporary directory
|
||||
///
|
||||
///
|
||||
/// This function removes screenshot files older than the specified number of minutes
|
||||
/// to prevent accumulation of temporary files.
|
||||
///
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `older_than_minutes` - Remove files older than this many minutes (default: 60)
|
||||
///
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<usize, String>` - The number of files deleted, or an error message
|
||||
#[tauri::command]
|
||||
@@ -222,24 +222,29 @@ pub async fn cleanup_screenshot_temp_files(
|
||||
older_than_minutes: Option<u64>,
|
||||
) -> Result<usize, String> {
|
||||
let minutes = older_than_minutes.unwrap_or(60);
|
||||
log::info!("Cleaning up screenshot files older than {} minutes", minutes);
|
||||
|
||||
log::info!(
|
||||
"Cleaning up screenshot files older than {} minutes",
|
||||
minutes
|
||||
);
|
||||
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let cutoff_time = chrono::Utc::now() - chrono::Duration::minutes(minutes as i64);
|
||||
let mut deleted_count = 0;
|
||||
|
||||
|
||||
// Read directory entries
|
||||
let entries = fs::read_dir(&temp_dir)
|
||||
.map_err(|e| format!("Failed to read temp directory: {}", e))?;
|
||||
|
||||
let entries =
|
||||
fs::read_dir(&temp_dir).map_err(|e| format!("Failed to read temp directory: {}", e))?;
|
||||
|
||||
for entry in entries {
|
||||
if let Ok(entry) = entry {
|
||||
let path = entry.path();
|
||||
|
||||
|
||||
// Check if it's a claudia screenshot file
|
||||
if let Some(filename) = path.file_name() {
|
||||
if let Some(filename_str) = filename.to_str() {
|
||||
if filename_str.starts_with("claudia_screenshot_") && filename_str.ends_with(".png") {
|
||||
if filename_str.starts_with("claudia_screenshot_")
|
||||
&& filename_str.ends_with(".png")
|
||||
{
|
||||
// Check file age
|
||||
if let Ok(metadata) = fs::metadata(&path) {
|
||||
if let Ok(modified) = metadata.modified() {
|
||||
@@ -258,7 +263,7 @@ pub async fn cleanup_screenshot_temp_files(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
log::info!("Cleaned up {} old screenshot files", deleted_count);
|
||||
Ok(deleted_count)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user