From 7c6ad4cfe434b06a60e2ae33a54a1cfc714181ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20R=C3=A5gstad?= Date: Wed, 13 Sep 2023 14:04:50 +0200 Subject: [PATCH] Improve code body --- src/file/parser.rs | 8 ++++---- src/file/webx.rs | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/file/parser.rs b/src/file/parser.rs index f1a5618..5d162cc 100644 --- a/src/file/parser.rs +++ b/src/file/parser.rs @@ -1,7 +1,7 @@ use std::{path::PathBuf, io::{BufReader, Read}}; use crate::{file::webx::WebXFile, reporting::error::{exit_error, ERROR_PARSE_IO, ERROR_SYNTAX, exit_error_unexpected_char, exit_error_unexpected, exit_error_expected_any_of_but_found, exit_error_expected_but_found}}; -use super::webx::{WebXScope, WebXModel, WebXRouteMethod, WebXRoute, WebXHandler}; +use super::webx::{WebXScope, WebXModel, WebXRouteMethod, WebXRoute, WebXHandler, WebXBody, WebXBodyType}; struct WebXFileParser<'a> { file: &'a PathBuf, @@ -290,16 +290,16 @@ impl<'a> WebXFileParser<'a> { WebXModel { name, fields } } - fn parse_code_body(&mut self) -> Option { + fn parse_code_body(&mut self) -> Option { self.skip_whitespace(true); match self.peek() { Some('{') => { self.next(); - Some(self.parse_block('{', '}')) + Some(WebXBody { body_type: WebXBodyType::TS, body: self.parse_block('{', '}').trim().to_string() }) }, Some('(') => { self.next(); - Some(self.parse_block('(', ')')) + Some(WebXBody { body_type: WebXBodyType::TSX, body: self.parse_block('(', ')').trim().to_string() }) }, _ => None, } diff --git a/src/file/webx.rs b/src/file/webx.rs index db6a33e..8919917 100644 --- a/src/file/webx.rs +++ b/src/file/webx.rs @@ -73,7 +73,7 @@ pub struct WebXHandler { /// The parameters of the handler. pub params: Vec, /// The typescript body of the handler. - pub body: String, + pub body: WebXBody, } #[derive(Debug)] @@ -89,6 +89,20 @@ pub enum WebXRouteMethod { TRACE, } +#[derive(Debug)] +pub enum WebXBodyType { + TS, + TSX, + JSON, + TEXT, +} + +#[derive(Debug)] +pub struct WebXBody { + pub body_type: WebXBodyType, + pub body: String, +} + #[derive(Debug)] pub struct WebXRoute { /// HTTP method of the route. @@ -100,7 +114,7 @@ pub struct WebXRoute { /// The pre-handler functions of the route. pub pre_handlers: Vec, /// The code block of the route. - pub body: Option, + pub body: Option, /// The post-handler functions of the route. pub post_handlers: Vec, }