1use std::{fmt, sync::Arc};
4
5use gpui::SharedString;
6
7#[derive(Clone, PartialEq, Eq, Hash, Default)]
21pub struct QueryKey(Arc<[SharedString]>);
22
23impl QueryKey {
24 #[must_use]
26 pub fn from_parts(parts: Vec<SharedString>) -> Self {
27 Self(parts.into())
28 }
29
30 #[must_use]
32 pub fn root() -> Self {
33 Self::default()
34 }
35
36 #[must_use]
38 pub fn parts(&self) -> &[SharedString] {
39 &self.0
40 }
41
42 #[must_use]
44 pub fn starts_with(&self, prefix: &QueryKey) -> bool {
45 self.0.starts_with(&prefix.0)
46 }
47
48 #[must_use]
50 pub fn child(&self, part: &impl fmt::Debug) -> Self {
51 let mut parts = self.0.to_vec();
52 parts.push(key_part(part));
53 Self(parts.into())
54 }
55}
56
57impl fmt::Display for QueryKey {
58 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59 for (index, part) in self.0.iter().enumerate() {
60 if index > 0 {
61 formatter.write_str("/")?;
62 }
63 formatter.write_str(part)?;
64 }
65 Ok(())
66 }
67}
68
69impl fmt::Debug for QueryKey {
70 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
71 formatter.debug_list().entries(self.0.iter()).finish()
72 }
73}
74
75#[doc(hidden)]
77pub fn key_part(value: &impl fmt::Debug) -> SharedString {
78 let text = format!("{value:?}");
79 match text
80 .strip_prefix('"')
81 .and_then(|rest| rest.strip_suffix('"'))
82 {
83 Some(inner) => inner.to_string().into(),
84 None => text.into(),
85 }
86}
87
88#[macro_export]
97macro_rules! query_key {
98 [$($part:expr),* $(,)?] => {
99 $crate::query::QueryKey::from_parts(::std::vec![$($crate::query::key::key_part(&$part)),*])
100 };
101}