use std::fmt::Write as _;
use gpui::{App, SharedString};
use super::{location, navigate, replace, Pattern, RouteMatch};
pub trait Route: Sized + 'static {
const PATTERN: &'static str;
fn params(&self) -> Vec<(&'static str, String)>;
fn from_match(route: &RouteMatch) -> Option<Self>;
fn href(&self) -> SharedString {
build_path(Self::PATTERN, &self.params())
}
#[must_use]
fn parse(path: &str) -> Option<Self> {
let location = super::Location::parse(path);
let (params, _) = Pattern::parse(Self::PATTERN).matches(location.path())?;
Self::from_match(&RouteMatch {
pattern: Self::PATTERN.into(),
params,
location,
})
}
}
fn build_path(pattern: &str, params: &[(&'static str, String)]) -> SharedString {
let value = |name: &str| {
params
.iter()
.find(|(param, _)| *param == name)
.map(|(_, value)| value.as_str())
.unwrap_or_default()
};
let mut path = String::new();
for segment in pattern.split('/').filter(|segment| !segment.is_empty()) {
if let Some(name) = segment.strip_prefix(':') {
path.push('/');
path.push_str(&encode(value(name)));
} else if let Some(name) = segment.strip_prefix('*') {
let rest = value(name);
if !rest.is_empty() {
path.push('/');
let encoded: Vec<String> = rest.split('/').map(encode).collect();
path.push_str(&encoded.join("/"));
}
} else {
path.push('/');
path.push_str(segment);
}
}
if path.is_empty() {
path.push('/');
}
path.into()
}
pub(crate) fn encode(component: &str) -> String {
let mut encoded = String::with_capacity(component.len());
for byte in component.bytes() {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
encoded.push(char::from(byte));
} else {
write!(encoded, "%{byte:02X}").ok();
}
}
encoded
}
pub fn navigate_to<R: Route>(route: &R, cx: &mut App) {
navigate(route.href(), cx);
}
pub fn replace_to<R: Route>(route: &R, cx: &mut App) {
replace(route.href(), cx);
}
pub fn use_params<R: Route>(cx: &mut App) -> Option<R> {
let location = location(cx);
R::parse(&location.to_string())
}
#[doc(hidden)]
#[must_use]
pub const fn pattern_has_param(pattern: &str, name: &str) -> bool {
let pattern = pattern.as_bytes();
let name = name.as_bytes();
let mut start = 0;
while start < pattern.len() {
let mut end = start;
while end < pattern.len() && pattern[end] != b'/' {
end += 1;
}
if end > start
&& (pattern[start] == b':' || pattern[start] == b'*')
&& end - start - 1 == name.len()
{
let mut index = 0;
while index < name.len() && pattern[start + 1 + index] == name[index] {
index += 1;
}
if index == name.len() {
return true;
}
}
start = end + 1;
}
false
}
#[doc(hidden)]
#[must_use]
pub const fn pattern_param_count(pattern: &str) -> usize {
let pattern = pattern.as_bytes();
let mut count = 0;
let mut index = 0;
while index < pattern.len() {
if (pattern[index] == b':' || pattern[index] == b'*')
&& (index == 0 || pattern[index - 1] == b'/')
{
count += 1;
}
index += 1;
}
count
}
#[macro_export]
macro_rules! typed_route {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident = $pattern:literal;
) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
$vis struct $name;
const _: () = ::core::assert!(
$crate::router::typed::pattern_param_count($pattern) == 0,
"this pattern has parameters; declare them as fields: `struct Name = \"..\" {{ field: Type }}`"
);
impl $crate::router::Route for $name {
const PATTERN: &'static str = $pattern;
fn params(&self) -> ::std::vec::Vec<(&'static str, ::std::string::String)> {
::std::vec::Vec::new()
}
fn from_match(_: &$crate::router::RouteMatch) -> ::core::option::Option<Self> {
::core::option::Option::Some(Self)
}
}
impl ::core::fmt::Display for $name {
fn fmt(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
formatter.write_str(&$crate::router::Route::href(self))
}
}
};
(
$(#[$meta:meta])*
$vis:vis struct $name:ident = $pattern:literal {
$($(#[$field_meta:meta])* $field_vis:vis $field:ident : $type:ty),+ $(,)?
}
) => {
$(#[$meta])*
#[derive(Clone, Debug, PartialEq)]
$vis struct $name {
$($(#[$field_meta])* $field_vis $field: $type,)+
}
$(
const _: () = ::core::assert!(
$crate::router::typed::pattern_has_param($pattern, ::core::stringify!($field)),
::core::concat!("the pattern has no `:", ::core::stringify!($field), "` parameter")
);
)+
const _: () = ::core::assert!(
$crate::router::typed::pattern_param_count($pattern) == [$(::core::stringify!($field)),+].len(),
"every parameter in the pattern needs a field"
);
impl $crate::router::Route for $name {
const PATTERN: &'static str = $pattern;
fn params(&self) -> ::std::vec::Vec<(&'static str, ::std::string::String)> {
::std::vec![$((::core::stringify!($field), ::std::string::ToString::to_string(&self.$field)),)+]
}
fn from_match(route: &$crate::router::RouteMatch) -> ::core::option::Option<Self> {
::core::option::Option::Some(Self {
$($field: route.param(::core::stringify!($field))?.parse().ok()?,)+
})
}
}
impl ::core::fmt::Display for $name {
fn fmt(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
formatter.write_str(&$crate::router::Route::href(self))
}
}
};
}