Repository: sxyazi/yazi
File: yazi-fs/src/path.rs (Rust)

Code before:
	let local = !url.scheme.is_virtual();

	// Windows paths that only have a drive letter but no root, e.g. "D:"
	#[cfg(windows)]
	if local && b.len() == 2 && b[1] == b':' && b[0].is_ascii_alphabetic() {
		url.with(format!(r"{}:\", b[0].to_ascii_uppercase() as char)).into();
	}

	let b = re.replace_all(b, |caps: &regex::bytes::Captures| {
		let name = caps.get(2).or_else(|| caps.get(1)).unwrap();
		str::from_utf8(name.as_bytes())
			.ok()
			.and_then(env::var_os)
			.map_or_else(|| caps.get(0).unwrap().as_bytes().to_owned(), |s| s.into_encoded_bytes())
	});
	if matches!(b, Cow::Borrowed(_)) {
		return url.into();
	}

	let p = unsafe { PathBuf::from(OsString::from_encoded_bytes_unchecked(b.into_owned())) };
	if let Some(rest) = p.strip_prefix("~").ok().filter(|_| local) {
		url.with(clean_path(&dirs::home_dir().unwrap_or_default().join(rest))).into()
	} else if p.is_absolute() {
		url.with(clean_path(&p)).into()
	} else {
		clean_url(CWD.load().join(p))
	}
}

pub fn skip_url(url: &Url, n: usize) -> Cow<'_, OsStr> {
	let mut it = url.components();
	for _ in 0..n {
		if it.next().is_none() {
			return OsStr::new("").into();
		}
	}
	it.os_str()
}

pub async fn unique_name<F>(u: Url, append: F) -> io::Result<Url>
where
	F: Future<Output = bool>,
{
	match services::symlink_metadata(&u).await {
		Ok(_) => _unique_name(u, append.await).await,
		Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(u),
		Err(e) => Err(e),
	}
}

async fn _unique_name(mut url: Url, append: bool) -> io::Result<Url> {

Review comment (suggestion):
The function expects to return `Cow<'_, Url>` but `clean_url` now returns `Cow<'a, Url>` with a different lifetime. This should be `.into_owned().into()` to match the return type.
```suggestion
		clean_url(CWD.load().join(p)).into_owned().into()
```

Code after:
	let local = !url.scheme.is_virtual();

	// Windows paths that only have a drive letter but no root, e.g. "D:"
	#[cfg(windows)]
	if local && b.len() == 2 && b[1] == b':' && b[0].is_ascii_alphabetic() {
		return url.with(format!(r"{}:\", b[0].to_ascii_uppercase() as char)).into();
	}

	let b = re.replace_all(b, |caps: &regex::bytes::Captures| {
		let name = caps.get(2).or_else(|| caps.get(1)).unwrap();
		str::from_utf8(name.as_bytes())
			.ok()
			.and_then(env::var_os)
			.map_or_else(|| caps.get(0).unwrap().as_bytes().to_owned(), |s| s.into_encoded_bytes())
	});

	let path: Cow<_> = unsafe {
		match b {
			Cow::Borrowed(b) => Path::new(OsStr::from_encoded_bytes_unchecked(b)).into(),
			Cow::Owned(b) => PathBuf::from(OsString::from_encoded_bytes_unchecked(b)).into(),
		}
	};

	if let Some(rest) = path.strip_prefix("~").ok().filter(|_| local) {
		url.with(clean_path(&dirs::home_dir().unwrap_or_default().join(rest))).into()
	} else if path.is_absolute() {
		url.with(clean_path(&path)).into()
	} else {
		clean_url(CWD.load().join(path))
	}
}

pub fn skip_url(url: &Url, n: usize) -> Cow<'_, OsStr> {
	let mut it = url.components();
	for _ in 0..n {
		if it.next().is_none() {
			return OsStr::new("").into();
		}
	}
	it.os_str()
}

pub async fn unique_name<F>(u: Url, append: F) -> io::Result<Url>
where
	F: Future<Output = bool>,
{
	match services::symlink_metadata(&u).await {
		Ok(_) => _unique_name(u, append.await).await,
		Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(u),
		Err(e) => Err(e),
	}