deref expanded path
change lvwvsvnlxkkwqllqlmntnyrvtxtqtxpl
commit 841ad850e0ad1770df90ed793a12293cccc24f1e
author Alpha Chen <alpha@kejadlen.dev>
date
parent rwutpzrq
diff --git a/src/assertions.rs b/src/assertions.rs
index 6045c19..20e4853 100644
--- a/src/assertions.rs
+++ b/src/assertions.rs
@@ -144,12 +144,12 @@ impl std::fmt::Display for Symlink {
 
 impl AssertionType for Symlink {
     fn status(&self) -> Result<Status> {
-        if !Path::new(self.target.as_str()).exists() {
+        if !Path::new(&self.target).exists() {
             return Ok(Status::Missing);
         }
 
-        if let Ok(link_target) = fs::read_link(self.target.as_str()) {
-            if link_target == Path::new(self.source.as_str()) {
+        if let Ok(link_target) = fs::read_link(&self.target) {
+            if link_target == Path::new(&self.source) {
                 Ok(Status::Ok)
             } else {
                 todo!(
@@ -173,7 +173,7 @@ impl AssertionType for Symlink {
 
     fn install(&self) -> Result<()> {
         use std::os::unix::fs;
-        fs::symlink(self.source.as_str(), self.target.as_str())
+        fs::symlink(&self.source, &self.target)
             .map_err(|e| eyre!("Failed to create symlink: {}", e))?;
         debug!("created: {}", self);
         Ok(())
@@ -199,7 +199,7 @@ impl std::fmt::Display for Directory {
 
 impl AssertionType for Directory {
     fn status(&self) -> Result<Status> {
-        let path = Path::new(self.path.as_str());
+        let path = Path::new(&self.path);
         if path.is_dir() {
             Ok(Status::Ok)
         } else if path.exists() {
@@ -213,7 +213,7 @@ impl AssertionType for Directory {
     }
 
     fn install(&self) -> Result<()> {
-        std::fs::create_dir_all(self.path.as_str())
+        std::fs::create_dir_all(&self.path)
             .map_err(|e| eyre!("Failed to create directory: {}", e))?;
         debug!("created: {}", self);
         Ok(())
diff --git a/src/utils.rs b/src/utils.rs
index bc9fbef..cb8cbdd 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -2,6 +2,8 @@ use color_eyre::{Result, eyre::eyre};
 use mlua::prelude::*;
 use regex::Regex;
 use std::env;
+use std::ffi::OsStr;
+use std::ops::Deref;
 use std::path::Path;
 use std::process::Command;
 use std::sync::LazyLock;
@@ -55,6 +57,32 @@ impl FromLua for ExpandedPath {
     }
 }
 
+impl Deref for ExpandedPath {
+    type Target = str;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+impl AsRef<str> for ExpandedPath {
+    fn as_ref(&self) -> &str {
+        &self.0
+    }
+}
+
+impl AsRef<OsStr> for ExpandedPath {
+    fn as_ref(&self) -> &OsStr {
+        OsStr::new(&self.0)
+    }
+}
+
+impl AsRef<Path> for ExpandedPath {
+    fn as_ref(&self) -> &Path {
+        Path::new(&self.0)
+    }
+}
+
 impl std::fmt::Display for ExpandedPath {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "{}", self.0)