Repository: sxyazi/yazi
File: yazi-shared/src/url/url.rs (Rust)

Code before:
	pub fn components(&self) -> Components<'_> { Components::new(self) }

	#[inline]
	pub fn covariant(&self, other: &Self) -> bool {
		self.scheme.covariant(&other.scheme) && self.loc == other.loc
	}

	#[inline]
	pub fn display(&self) -> Display<'_> { Display::new(self) }

	#[inline]
	pub fn os_str(&self) -> Cow<'_, OsStr> { self.components().os_str() }

	pub fn parent_url(&self) -> Option<Url> {
		let parent = self.loc.parent()?;
		let base = self.loc.base();

		Some(match &self.scheme {
			Scheme::Regular => Self { loc: parent.into(), scheme: Scheme::Regular },
			Scheme::Search(_) => Self { loc: parent.into(), scheme: Scheme::Regular },
			Scheme::SearchItem if parent == base => {
				Self { loc: parent.into(), scheme: Scheme::Search(String::new()) }
			}
			Scheme::SearchItem => {
				Self { loc: Loc::with(base, parent.to_owned()), scheme: Scheme::SearchItem }
			}
			Scheme::Archive(_) => Self { loc: parent.into(), scheme: self.scheme.clone() },
			Scheme::Sftp(_) => Self { loc: parent.into(), scheme: self.scheme.clone() },
		})
	}

	pub fn strip_prefix(&self, base: impl AsRef<Url>) -> Option<Self> {
		let base = base.as_ref();
		if !self.scheme.covariant(&base.scheme) {
			return None;
		}

		Some(Self {
			loc:    self.loc.strip_prefix(base.loc.as_path()).ok()?.into(),
			scheme: self.scheme.clone(),
		})
	}

	#[inline]
	pub fn as_path(&self) -> Option<&Path> {
		Some(self.loc.as_path()).filter(|_| !self.scheme.is_virtual())
	}

	#[inline]
	pub fn set_name(&mut self, name: impl AsRef<OsStr>) { self.loc.set_name(name); }


Review comment (suggestion):
[nitpick] The change from `base.loc.as_path()` to `&base.loc` suggests that `Loc` now implements `AsRef<Path>`. While this works, the original explicit conversion was more self-documenting about the intent to get a path reference.
```suggestion
			loc:    self.loc.strip_prefix(base.loc.as_path()).ok()?.into(),
```

Code after:
	pub fn parent_url(&self) -> Option<Url> {
		let parent = self.loc.parent()?;
		let base = self.loc.base();

		Some(match &self.scheme {
			Scheme::Regular => Self { loc: parent.into(), scheme: Scheme::Regular },
			Scheme::Search(_) => Self { loc: parent.into(), scheme: Scheme::Regular },
			Scheme::SearchItem if parent == base => {
				Self { loc: parent.into(), scheme: Scheme::Search(String::new()) }
			}
			Scheme::SearchItem => {
				Self { loc: Loc::with(base, parent.to_owned()), scheme: Scheme::SearchItem }
			}
			Scheme::Archive(_) => Self { loc: parent.into(), scheme: self.scheme.clone() },
			Scheme::Sftp(_) => Self { loc: parent.into(), scheme: self.scheme.clone() },
		})
	}

	pub fn strip_prefix(&self, base: impl AsRef<Url>) -> Option<Self> {
		let base = base.as_ref();
		if !self.scheme.covariant(&base.scheme) {
			return None;
		}

		Some(Self {
			loc:    self.loc.strip_prefix(&base.loc).ok()?.into(),
			scheme: self.scheme.clone(),
		})
	}

	#[inline]
	pub fn as_path(&self) -> Option<&Path> {
		Some(self.loc.as_path()).filter(|_| !self.scheme.is_virtual())
	}

	#[inline]
	pub fn set_name(&mut self, name: impl AsRef<OsStr>) { self.loc.set_name(name); }

	#[inline]
	pub fn pair(&self) -> Option<(Self, UrnBuf)> { Some((self.parent_url()?, self.loc.urn_owned())) }

	#[inline]
	pub fn hash_u64(&self) -> u64 { foldhash::fast::FixedState::default().hash_one(self) }

	#[inline]
	pub fn rebase(&self, parent: &Path) -> Self {
		debug_assert!(self.is_regular());
		self.loc.rebase(parent).into()
	}
}