style(src-tauri): cargo fmt
Pure mechanical rustfmt reformat (default config, no rustfmt.toml).
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
//! User-visible files: exports written to a path from the save dialog, and the
|
||||
//! reveal/open helpers used afterwards.
|
||||
use super::raw::{decode_header_path, header_map, raw_body, required_header, write_atomic, Headers};
|
||||
use super::raw::{
|
||||
decode_header_path, header_map, raw_body, required_header, write_atomic, Headers,
|
||||
};
|
||||
use crate::AppState;
|
||||
use rusqlite::{params, Connection};
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -25,7 +27,9 @@ pub fn write_export_file_impl(
|
||||
if bytes.is_empty() {
|
||||
return Err("Nothing to write: the file is empty".to_string());
|
||||
}
|
||||
let parent = path.parent().ok_or_else(|| "The export path has no folder".to_string())?;
|
||||
let parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| "The export path has no folder".to_string())?;
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("Could not create {}: {e}", parent.display()))?;
|
||||
write_atomic(&path, bytes)?;
|
||||
@@ -38,8 +42,12 @@ pub fn write_export_file_impl(
|
||||
}
|
||||
|
||||
pub fn get_last_export_dir_impl(conn: &Connection) -> Result<String, String> {
|
||||
conn.query_row("SELECT last_export_dir FROM app_settings WHERE id = 1", [], |r| r.get(0))
|
||||
.map_err(|e| e.to_string())
|
||||
conn.query_row(
|
||||
"SELECT last_export_dir FROM app_settings WHERE id = 1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn require_existing(path: &str) -> Result<PathBuf, String> {
|
||||
@@ -71,7 +79,11 @@ fn reveal_fallback_command(path: &Path) -> Command {
|
||||
}
|
||||
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
|
||||
{
|
||||
let folder = if path.is_dir() { path } else { path.parent().unwrap_or(path) };
|
||||
let folder = if path.is_dir() {
|
||||
path
|
||||
} else {
|
||||
path.parent().unwrap_or(path)
|
||||
};
|
||||
let mut cmd = Command::new("xdg-open");
|
||||
cmd.arg(folder);
|
||||
cmd
|
||||
@@ -90,7 +102,9 @@ fn open_fallback_command(path: &Path) -> Command {
|
||||
}
|
||||
|
||||
fn spawn_detached(mut cmd: Command) -> Result<(), String> {
|
||||
let mut child = cmd.spawn().map_err(|e| format!("Could not start the file manager: {e}"))?;
|
||||
let mut child = cmd
|
||||
.spawn()
|
||||
.map_err(|e| format!("Could not start the file manager: {e}"))?;
|
||||
// Reap the child so it does not linger as a zombie; the result is irrelevant
|
||||
// (explorer.exe, for one, exits non-zero even on success).
|
||||
std::thread::spawn(move || {
|
||||
@@ -100,7 +114,10 @@ fn spawn_detached(mut cmd: Command) -> Result<(), String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn write_export_file(request: Request<'_>, state: State<'_, AppState>) -> Result<String, String> {
|
||||
pub async fn write_export_file(
|
||||
request: Request<'_>,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<String, String> {
|
||||
let bytes = raw_body(&request)?;
|
||||
let headers = header_map(&request);
|
||||
let conn = state.db.lock().map_err(|e| e.to_string())?;
|
||||
@@ -125,7 +142,11 @@ pub fn reveal_in_folder(app: tauri::AppHandle, path: String) -> Result<(), Strin
|
||||
#[tauri::command]
|
||||
pub fn open_file(app: tauri::AppHandle, path: String) -> Result<(), String> {
|
||||
let path = require_existing(&path)?;
|
||||
if app.opener().open_path(path.to_string_lossy(), None::<&str>).is_ok() {
|
||||
if app
|
||||
.opener()
|
||||
.open_path(path.to_string_lossy(), None::<&str>)
|
||||
.is_ok()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
spawn_detached(open_fallback_command(&path))
|
||||
@@ -141,8 +162,11 @@ mod tests {
|
||||
}
|
||||
|
||||
fn encode(path: &Path) -> String {
|
||||
percent_encoding::utf8_percent_encode(&path.to_string_lossy(), percent_encoding::NON_ALPHANUMERIC)
|
||||
.to_string()
|
||||
percent_encoding::utf8_percent_encode(
|
||||
&path.to_string_lossy(),
|
||||
percent_encoding::NON_ALPHANUMERIC,
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -152,8 +176,12 @@ mod tests {
|
||||
assert_eq!(get_last_export_dir_impl(&conn).unwrap(), "");
|
||||
|
||||
// Non-ASCII, spaces and a folder that does not exist yet.
|
||||
let target = dir.path().join("New folder").join("\u{9ac}\u{9be}\u{982}\u{9b2}\u{9be} 1.pdf");
|
||||
let written = write_export_file_impl(&conn, b"%PDF-1.7", &headers(&encode(&target))).unwrap();
|
||||
let target = dir
|
||||
.path()
|
||||
.join("New folder")
|
||||
.join("\u{9ac}\u{9be}\u{982}\u{9b2}\u{9be} 1.pdf");
|
||||
let written =
|
||||
write_export_file_impl(&conn, b"%PDF-1.7", &headers(&encode(&target))).unwrap();
|
||||
assert_eq!(PathBuf::from(&written), target);
|
||||
assert_eq!(std::fs::read(&target).unwrap(), b"%PDF-1.7");
|
||||
assert_eq!(
|
||||
@@ -170,7 +198,10 @@ mod tests {
|
||||
fn rejects_empty_relative_and_missing_paths() {
|
||||
let conn = crate::db::open_in_memory().unwrap();
|
||||
for bad in ["", " ", "out.pdf", "sub%2Fout.pdf", "..%2Fout.pdf"] {
|
||||
assert!(write_export_file_impl(&conn, b"x", &headers(bad)).is_err(), "{bad}");
|
||||
assert!(
|
||||
write_export_file_impl(&conn, b"x", &headers(bad)).is_err(),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
assert!(write_export_file_impl(&conn, b"x", &Headers::new()).is_err());
|
||||
assert_eq!(get_last_export_dir_impl(&conn).unwrap(), "");
|
||||
@@ -191,7 +222,9 @@ mod tests {
|
||||
let dir = tempdir().unwrap();
|
||||
assert!(require_existing("").is_err());
|
||||
let missing = dir.path().join("nope.pdf");
|
||||
assert!(require_existing(&missing.to_string_lossy()).unwrap_err().contains("does not exist"));
|
||||
assert!(require_existing(&missing.to_string_lossy())
|
||||
.unwrap_err()
|
||||
.contains("does not exist"));
|
||||
assert!(require_existing(&dir.path().to_string_lossy()).is_ok());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user