1pub mod hosts;
2pub mod route_index;
3pub mod router;
4
5pub use hosts::{ContextError, ContextHosts};
6pub use route_index::{
7 ClientContextMeta, ClientLinkEntry, ClientLinks, ContextLinksRule, ContextSpec, ContextTarget,
8 HrefOptions, RouteEntry, RouteIndex,
9};
10pub use router::{
11 ContextRoute, ContextRoutes, SetCurrentContext, SharedRouteIndex, join_uri,
12};
13
14const CONTEXT_KEY: &str = "saola.current_context";
15
16#[derive(Debug, Clone, Default)]
18pub struct ContextRegistry {
19 contexts: Vec<ContextSpec>,
20 routes: Vec<RouteEntry>,
21}
22
23impl ContextRegistry {
24 pub fn new() -> Self {
25 Self::default()
26 }
27
28 pub fn add_context(&mut self, spec: ContextSpec) -> &mut Self {
29 self.contexts.push(spec);
30 self
31 }
32
33 pub fn add_route(&mut self, entry: RouteEntry) -> &mut Self {
34 self.routes.push(entry);
35 self
36 }
37
38 pub fn contexts(&self) -> &[ContextSpec] {
39 &self.contexts
40 }
41
42 pub fn routes(&self) -> &[RouteEntry] {
43 &self.routes
44 }
45
46 pub fn build_index(&self, primary_url: &str) -> Result<RouteIndex, ContextError> {
47 let mut sorted_contexts = self.contexts.clone();
48 sorted_contexts.sort_by(|a, b| b.priority.cmp(&a.priority).then_with(|| a.slug.cmp(&b.slug)));
49 RouteIndex::new(sorted_contexts, self.routes.clone(), primary_url)
50 }
51}
52
53pub fn set_current_context(slug: &str) {
55 laravel::context::add_hidden(CONTEXT_KEY, slug);
56}
57
58pub fn current_context() -> Option<String> {
60 laravel::context::get_hidden::<String>(CONTEXT_KEY)
61}