mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 09:09:03 +03:00
26 lines
671 B
Rust
26 lines
671 B
Rust
use crate::BoxResult;
|
|
use std::{borrow::Cow, path::Path};
|
|
|
|
pub fn encode_uri(v: &str) -> String {
|
|
let parts: Vec<_> = v.split('/').map(urlencoding::encode).collect();
|
|
parts.join("/")
|
|
}
|
|
|
|
pub fn decode_uri(v: &str) -> Option<Cow<str>> {
|
|
percent_encoding::percent_decode(v.as_bytes())
|
|
.decode_utf8()
|
|
.ok()
|
|
}
|
|
|
|
pub fn get_file_name(path: &Path) -> &str {
|
|
path.file_name()
|
|
.and_then(|v| v.to_str())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
pub fn try_get_file_name(path: &Path) -> BoxResult<&str> {
|
|
path.file_name()
|
|
.and_then(|v| v.to_str())
|
|
.ok_or_else(|| format!("Failed to get file name of `{}`", path.display()).into())
|
|
}
|