Skip to main content

flams_router_content/
backend.rs

1use ftml_ontology::narrative::elements::SectionLevel;
2use ftml_uris::{DocumentElementUri, DocumentUri, FtmlUri, Uri};
3
4pub struct FtmlBackend;
5impl ftml_backend::GlobalBackend for FtmlBackend {
6    type Error = <Self::Backend as ftml_backend::FtmlBackend>::Error; // TODO
7    #[cfg(feature = "ssr")]
8    type Backend = Self;
9    #[cfg(all(feature = "hydrate", not(feature = "ssr")))]
10    type Backend = ftml_backend::CachedBackend<Self>;
11    #[inline]
12    fn get() -> &'static Self::Backend {
13        #[cfg(feature = "ssr")]
14        static SLF: FtmlBackend = FtmlBackend;
15        #[cfg(all(feature = "hydrate", not(feature = "ssr")))]
16        static SLF: std::sync::LazyLock<ftml_backend::CachedBackend<FtmlBackend>> =
17            std::sync::LazyLock::new(|| ftml_backend::CachedBackend::new(FtmlBackend));
18        &SLF
19    }
20}
21impl ftml_backend::FlamsBackend for FtmlBackend {
22    #[inline]
23    fn stripped(&self) -> bool {
24        true
25    }
26    fn content_link_url(&self, uri: ftml_uris::UriRef<'_>) -> String {
27        format!("/?uri={}", uri.url_encoded())
28    }
29    fn document_link_url(&self, uri: &ftml_uris::DocumentUri) -> String {
30        format!("/?uri={}", uri.url_encoded())
31    }
32    fn resource_link_url(
33        &self,
34        uri: &ftml_uris::DocumentUri,
35        kind: &'static str,
36    ) -> Option<String> {
37        Some(format!("/doc?uri={}&format={kind}", uri.url_encoded()))
38    }
39
40    fn check_term(
41        &self,
42        global_context: &[ftml_uris::ModuleUri],
43        in_term: either::Either<&ftml_ontology::terms::Term, &DocumentElementUri>,
44        subterm: either::Either<
45            &ftml_ontology::terms::Term,
46            &ftml_ontology::terms::termpaths::TermPath,
47        >,
48    ) -> impl Future<
49        Output = Result<
50            ftml_backend::BackendCheckResult,
51            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
52        >,
53    > + Send
54    + use<> {
55        super::server_fns::check_term(global_context.to_vec(), in_term.cloned(), subterm.cloned())
56    }
57
58    fn get_fragment(
59        &self,
60        uri: Option<ftml_uris::Uri>,
61        rp: Option<String>,
62        a: Option<ftml_uris::ArchiveId>,
63        p: Option<String>,
64        d: Option<String>,
65        m: Option<String>,
66        l: Option<ftml_uris::Language>,
67        e: Option<String>,
68        s: Option<String>,
69        context: Option<ftml_uris::NarrativeUri>,
70    ) -> impl Future<
71        Output = Result<
72            (ftml_uris::Uri, Box<[ftml_ontology::utils::Css]>, Box<str>),
73            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
74        >,
75    >
76    + 'static
77    + use<> {
78        super::server_fns::fragment(uri, rp, a, p, d, m, l, e, s, context)
79    }
80
81    fn get_document_html(
82        &self,
83        uri: Option<ftml_uris::DocumentUri>,
84        rp: Option<String>,
85        a: Option<ftml_uris::ArchiveId>,
86        p: Option<String>,
87        d: Option<String>,
88        l: Option<ftml_uris::Language>,
89    ) -> impl Future<
90        Output = Result<
91            (DocumentUri, Box<[ftml_ontology::utils::Css]>, Box<str>),
92            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
93        >,
94    > + Send
95    + 'static {
96        super::server_fns::document(uri, rp, a, p, d, l)
97    }
98
99    fn get_toc(
100        &self,
101        uri: Option<DocumentUri>,
102        rp: Option<String>,
103        a: Option<ftml_uris::ArchiveId>,
104        p: Option<String>,
105        d: Option<String>,
106        l: Option<ftml_uris::Language>,
107    ) -> impl Future<
108        Output = Result<
109            (
110                Box<[ftml_ontology::utils::Css]>,
111                SectionLevel,
112                Box<[ftml_ontology::narrative::documents::TocElem]>,
113            ),
114            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
115        >,
116    >
117    + 'static
118    + Send {
119        let fut = super::server_fns::toc(uri, rp, a, p, d, l);
120        async move {
121            fut.await
122                .map_err(|e| ftml_backend::BackendError::ToDo(e.to_string()))
123        }
124    }
125
126    fn get_module(
127        &self,
128        uri: Option<ftml_uris::ModuleUri>,
129        a: Option<ftml_uris::ArchiveId>,
130        p: Option<String>,
131        m: Option<String>,
132    ) -> impl Future<
133        Output = Result<
134            ftml_ontology::domain::modules::ModuleLike,
135            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
136        >,
137    > + Send
138    + 'static {
139        super::server_fns::get_module(uri, a, p, m)
140    }
141
142    fn get_solutions(
143        &self,
144        uri: ftml_uris::DocumentElementUri,
145    ) -> impl Future<
146        Output = Result<
147            ftml_ontology::narrative::elements::problems::Solutions,
148            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
149        >,
150    > + Send
151    + 'static {
152        let uriclone = uri.clone();
153        let r = super::server_fns::solution(
154            Some(uri.into()),
155            None,
156            None,
157            None,
158            None,
159            None,
160            None,
161            None,
162            None,
163        );
164        async move {
165            let r = r
166                .await
167                .map_err(|e| ftml_backend::BackendError::ToDo(e.to_string()))?;
168            ftml_ontology::narrative::elements::problems::Solutions::from_jstring(&r)
169                .ok_or_else(|| ftml_backend::BackendError::NotFound(uriclone.into()))
170        }
171    }
172
173    fn get_document(
174        &self,
175        uri: Option<ftml_uris::DocumentUri>,
176        rp: Option<String>,
177        a: Option<ftml_uris::ArchiveId>,
178        p: Option<String>,
179        d: Option<String>,
180        l: Option<ftml_uris::Language>,
181    ) -> impl Future<
182        Output = Result<
183            ftml_ontology::narrative::documents::Document,
184            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
185        >,
186    > + 'static {
187        super::server_fns::get_document(uri, rp, a, p, d, l)
188    }
189
190    fn get_notations(
191        &self,
192        uri: Option<Uri>,
193        rp: Option<String>,
194        a: Option<ftml_uris::ArchiveId>,
195        p: Option<String>,
196        d: Option<String>,
197        m: Option<String>,
198        l: Option<ftml_uris::Language>,
199        e: Option<String>,
200        s: Option<String>,
201    ) -> impl Future<
202        Output = Result<
203            Vec<(
204                ftml_uris::DocumentElementUri,
205                ftml_ontology::narrative::elements::Notation,
206            )>,
207            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
208        >,
209    > + Send
210    + 'static {
211        super::server_fns::notations(uri, rp, a, p, d, m, l, e, s)
212    }
213    fn get_logical_paragraphs(
214        &self,
215        uri: Option<ftml_uris::SymbolUri>,
216        a: Option<ftml_uris::ArchiveId>,
217        p: Option<String>,
218        m: Option<String>,
219        s: Option<String>,
220        problems: bool,
221    ) -> impl Future<
222        Output = Result<
223            Vec<(
224                ftml_uris::DocumentElementUri,
225                ftml_ontology::narrative::elements::ParagraphOrProblemKind,
226            )>,
227            ftml_backend::BackendError<leptos::server_fn::error::ServerFnErrorErr>,
228        >,
229    > + Send
230    + 'static {
231        super::server_fns::los(uri, a, p, m, s, problems)
232    }
233}