Adopt camino for UTF-8 paths
Assisted-by: GLM-5.3 via pi
change vqznpvqsxuynptptxzwwrwnlyvryorul
commit 8770a7a36d5e51c140e69576f743523b0c8fec9b
author Alpha Chen <alpha@kejadlen.dev>
date
parent nnrumttq
diff --git a/Cargo.lock b/Cargo.lock
index f349bd8..701ea1b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -144,6 +144,12 @@ version = "1.12.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
 
+[[package]]
+name = "camino"
+version = "1.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbbad30e4b4c14a39e3cc8aed085a12a327257c316619c93581e017bc52be591"
+
 [[package]]
 name = "cc"
 version = "1.5.1"
@@ -631,6 +637,7 @@ name = "parallax"
 version = "0.1.0"
 dependencies = [
  "assert_cmd",
+ "camino",
  "clap",
  "clap_complete",
  "fs-err",
diff --git a/Cargo.toml b/Cargo.toml
index e05c244..a6d7734 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ name = "parallax"
 path = "src/bin/parallax/main.rs"
 
 [dependencies]
+camino = "*"
 clap = { version = "*", features = ["derive", "env"] }
 clap_complete = "*"
 fs-err = "*"
diff --git a/src/bin/parallax/main.rs b/src/bin/parallax/main.rs
index 59d13b3..a81e54b 100644
--- a/src/bin/parallax/main.rs
+++ b/src/bin/parallax/main.rs
@@ -1,5 +1,5 @@
+use camino::Utf8PathBuf;
 use std::io::stdout;
-use std::path::PathBuf;
 
 use clap::CommandFactory;
 use clap::Parser;
@@ -23,7 +23,7 @@ const VERSION: &str = env!("PARALLAX_VERSION");
 struct Cli {
     /// Path to the config file
     #[arg(long, global = true, default_value = "parallax.fnl")]
-    config: PathBuf,
+    config: Utf8PathBuf,
 
     /// Generate shell completions and exit
     #[arg(long, value_enum)]
@@ -39,7 +39,7 @@ enum Commands {
     Run {
         /// Override the output directory
         #[arg(long)]
-        output: Option<PathBuf>,
+        output: Option<Utf8PathBuf>,
     },
 
     /// Print the parallax's flow names in order
diff --git a/src/error.rs b/src/error.rs
index 59af5ec..e4c9748 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,4 +1,4 @@
-use std::path::PathBuf;
+use camino::Utf8PathBuf;
 
 use miette::Diagnostic;
 
@@ -11,12 +11,15 @@ pub enum Error {
     )]
     Config(#[from] mlua::Error),
 
-    #[error("{} must call ({what} ...)", .path.display())]
+    #[error("{} must call ({what} ...)", .path)]
     #[diagnostic(
         code(parallax::config_registration),
         help("a config registers with (setup {{...}}), (server fn), and (flow :name fn)")
     )]
-    ConfigRegistration { path: PathBuf, what: &'static str },
+    ConfigRegistration {
+        path: Utf8PathBuf,
+        what: &'static str,
+    },
 
     #[error("io error: {0}")]
     #[diagnostic(code(parallax::io))]
diff --git a/src/parallax.rs b/src/parallax.rs
index 81e5315..bf9e60e 100644
--- a/src/parallax.rs
+++ b/src/parallax.rs
@@ -1,6 +1,6 @@
+use camino::Utf8Path;
+use camino::Utf8PathBuf;
 use std::mem;
-use std::path::Path;
-use std::path::PathBuf;
 use std::sync::Arc;
 use std::sync::Mutex;
 use std::sync::MutexGuard;
@@ -43,34 +43,34 @@ pub struct Parallax {
     setup: Setup,
     name: String,
     #[allow(dead_code)]
-    path: PathBuf,
+    path: Utf8PathBuf,
     #[allow(dead_code)]
     lua: Lua,
 }
 
 impl Parallax {
-    pub fn load(path: &Path) -> Result<Parallax, Error> {
+    pub fn load(path: &Utf8Path) -> Result<Parallax, Error> {
         let source = fs_err::read_to_string(path)?;
         // The Fennel compiler requires the debug library, which mlua's
         // safe default omits. Configs are the user's own trusted files,
         // so the full stdlib is fine here.
         let lua = unsafe { Lua::unsafe_new() };
         let stash = Arc::new(Mutex::new(Registrations::default()));
-        let filename = path.to_string_lossy().into_owned();
+        let filename = path.as_str().to_owned();
 
         let Registrations {
             setup,
             server,
             flows,
         } = run_config(&lua, &source, &filename, &stash)
-            .with_context(|_| format!("loading {}", path.display()))?;
+            .with_context(|_| format!("loading {path}"))?;
 
         let setup = setup.ok_or_else(|| Error::ConfigRegistration {
-            path: path.to_path_buf(),
+            path: path.to_owned(),
             what: "setup",
         })?;
         let server = server.ok_or_else(|| Error::ConfigRegistration {
-            path: path.to_path_buf(),
+            path: path.to_owned(),
             what: "server",
         })?;
         let name = setup.name.clone().unwrap_or_else(|| default_name(path));
@@ -80,7 +80,7 @@ impl Parallax {
             server,
             setup,
             name,
-            path: path.to_path_buf(),
+            path: path.to_owned(),
             lua,
         })
     }
@@ -94,15 +94,14 @@ impl Parallax {
     }
 
     /// Runs every flow in every viewport.
-    pub async fn run(&self, _output: Option<&Path>) -> Result<(), Error> {
+    pub async fn run(&self, _output: Option<&Utf8Path>) -> Result<(), Error> {
         Err(Error::NotImplemented)
     }
 }
 
-fn default_name(path: &Path) -> String {
+fn default_name(path: &Utf8Path) -> String {
     path.parent()
-        .and_then(Path::file_name)
-        .and_then(std::ffi::OsStr::to_str)
+        .and_then(Utf8Path::file_name)
         .unwrap_or("parallax")
         .to_owned()
 }