diff --git a/assetpath/Cargo.toml b/assetpath/Cargo.toml index 31ab8d6..636bcb2 100644 --- a/assetpath/Cargo.toml +++ b/assetpath/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] serde = { version = "1.0.203", features = ["derive"] } +anyhow = { version = "1.0.86", features = ["backtrace"] } diff --git a/assetpath/src/lib.rs b/assetpath/src/lib.rs index eb7f6ad..d1524b8 100644 --- a/assetpath/src/lib.rs +++ b/assetpath/src/lib.rs @@ -1,5 +1,10 @@ +use anyhow::anyhow; use serde::{Deserialize, Serialize}; -use std::{fmt::Display, path::Path, str::FromStr}; +use std::{ + fmt::Display, + path::{Path, PathBuf}, + str::FromStr, +}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Default)] pub struct AssetPath { @@ -138,3 +143,18 @@ impl From for AssetPath { Self { prefix: None, path } } } + +impl TryFrom for AssetPath { + type Error = anyhow::Error; + + fn try_from(value: PathBuf) -> anyhow::Result { + let mut me: Self = value + .to_str() + .ok_or(anyhow!("failed to convert PathBuf to &str"))? + .into(); + + me.assume_prefix_free(); + + Ok(me) + } +}