@@ -22,7 +22,7 @@ pub enum PageContext<W: std::io::Write> {
2222 /// Indicates that we should start rendering the body
2323 Body {
2424 http_response : HttpResponseBuilder ,
25- renderer : RenderContext < W > ,
25+ renderer : AnyRenderBodyContext < W > ,
2626 } ,
2727
2828 /// The response is ready, and should be sent as is. No further statements should be executed
@@ -232,9 +232,11 @@ impl<'a, W: std::io::Write> HeaderContext<W> {
232232 }
233233
234234 async fn start_body ( self , data : JsonValue ) -> anyhow:: Result < PageContext < W > > {
235- let renderer = RenderContext :: new ( self . app_state , self . request_context , self . writer , data)
236- . await
237- . with_context ( || "Failed to create a render context from the header context." ) ?;
235+ let html_renderer =
236+ HtmlRenderContext :: new ( self . app_state , self . request_context , self . writer , data)
237+ . await
238+ . with_context ( || "Failed to create a render context from the header context." ) ?;
239+ let renderer = AnyRenderBodyContext :: Html ( html_renderer) ;
238240 let http_response = self . response ;
239241 Ok ( PageContext :: Body {
240242 renderer,
@@ -283,8 +285,48 @@ fn take_object_str(json: &mut JsonValue, key: &str) -> Option<String> {
283285 }
284286}
285287
288+ /**
289+ * Can receive rows, and write them in a given format to an `io::Write`
290+ */
291+ pub enum AnyRenderBodyContext < W : std:: io:: Write > {
292+ Html ( HtmlRenderContext < W > ) ,
293+ }
294+
295+ /**
296+ * Dummy impl to dispatch method calls to the underlying renderer
297+ */
298+ impl < W : std:: io:: Write > AnyRenderBodyContext < W > {
299+ pub async fn handle_row ( & mut self , data : & JsonValue ) -> anyhow:: Result < ( ) > {
300+ match self {
301+ AnyRenderBodyContext :: Html ( render_context) => render_context. handle_row ( data) . await ,
302+ }
303+ }
304+ pub async fn handle_error ( & mut self , error : & anyhow:: Error ) -> anyhow:: Result < ( ) > {
305+ match self {
306+ AnyRenderBodyContext :: Html ( render_context) => render_context. handle_error ( error) . await ,
307+ }
308+ }
309+ pub async fn finish_query ( & mut self ) -> anyhow:: Result < ( ) > {
310+ match self {
311+ AnyRenderBodyContext :: Html ( render_context) => render_context. finish_query ( ) . await ,
312+ }
313+ }
314+
315+ pub fn writer_mut ( & mut self ) -> & mut W {
316+ match self {
317+ AnyRenderBodyContext :: Html ( HtmlRenderContext { writer, .. } ) => writer,
318+ }
319+ }
320+
321+ pub async fn close ( self ) -> W {
322+ match self {
323+ AnyRenderBodyContext :: Html ( render_context) => render_context. close ( ) . await ,
324+ }
325+ }
326+ }
327+
286328#[ allow( clippy:: module_name_repetitions) ]
287- pub struct RenderContext < W : std:: io:: Write > {
329+ pub struct HtmlRenderContext < W : std:: io:: Write > {
288330 app_state : Arc < AppState > ,
289331 pub writer : W ,
290332 current_component : Option < SplitTemplateRenderer > ,
@@ -297,13 +339,13 @@ const DEFAULT_COMPONENT: &str = "table";
297339const PAGE_SHELL_COMPONENT : & str = "shell" ;
298340const FRAGMENT_SHELL_COMPONENT : & str = "shell-empty" ;
299341
300- impl < W : std:: io:: Write > RenderContext < W > {
342+ impl < W : std:: io:: Write > HtmlRenderContext < W > {
301343 pub async fn new (
302344 app_state : Arc < AppState > ,
303345 request_context : RequestContext ,
304346 mut writer : W ,
305347 initial_row : JsonValue ,
306- ) -> anyhow:: Result < RenderContext < W > > {
348+ ) -> anyhow:: Result < HtmlRenderContext < W > > {
307349 log:: debug!( "Creating the shell component for the page" ) ;
308350
309351 let mut initial_rows = vec ! [ Cow :: Borrowed ( & initial_row) ] ;
@@ -340,7 +382,7 @@ impl<W: std::io::Write> RenderContext<W> {
340382 log:: debug!( "Rendering the shell with properties: {shell_row}" ) ;
341383 shell_renderer. render_start ( & mut writer, shell_row) ?;
342384
343- let mut initial_context = RenderContext {
385+ let mut initial_context = HtmlRenderContext {
344386 app_state,
345387 writer,
346388 current_component : None ,
0 commit comments