use std::{fmt, sync::Arc};
use gpui::SharedString;
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct QueryKey(Arc<[SharedString]>);
impl QueryKey {
#[must_use]
pub fn from_parts(parts: Vec<SharedString>) -> Self {
Self(parts.into())
}
#[must_use]
pub fn root() -> Self {
Self::default()
}
#[must_use]
pub fn parts(&self) -> &[SharedString] {
&self.0
}
#[must_use]
pub fn starts_with(&self, prefix: &QueryKey) -> bool {
self.0.starts_with(&prefix.0)
}
#[must_use]
pub fn child(&self, part: &impl fmt::Debug) -> Self {
let mut parts = self.0.to_vec();
parts.push(key_part(part));
Self(parts.into())
}
}
impl fmt::Display for QueryKey {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, part) in self.0.iter().enumerate() {
if index > 0 {
formatter.write_str("/")?;
}
formatter.write_str(part)?;
}
Ok(())
}
}
impl fmt::Debug for QueryKey {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_list().entries(self.0.iter()).finish()
}
}
#[doc(hidden)]
pub fn key_part(value: &impl fmt::Debug) -> SharedString {
let text = format!("{value:?}");
match text
.strip_prefix('"')
.and_then(|rest| rest.strip_suffix('"'))
{
Some(inner) => inner.to_string().into(),
None => text.into(),
}
}
#[macro_export]
macro_rules! query_key {
[$($part:expr),* $(,)?] => {
$crate::query::QueryKey::from_parts(::std::vec![$($crate::query::key::key_part(&$part)),*])
};
}