Skip to content

Commit

Permalink
Improve code body
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Sep 13, 2023
1 parent d1ac4a6 commit 7c6ad4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/file/parser.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -290,16 +290,16 @@ impl<'a> WebXFileParser<'a> {
WebXModel { name, fields }
}

fn parse_code_body(&mut self) -> Option<String> {
fn parse_code_body(&mut self) -> Option<WebXBody> {
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,
}
Expand Down
18 changes: 16 additions & 2 deletions src/file/webx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct WebXHandler {
/// The parameters of the handler.
pub params: Vec<TypedIdentifier>,
/// The typescript body of the handler.
pub body: String,
pub body: WebXBody,
}

#[derive(Debug)]
Expand All @@ -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.
Expand All @@ -100,7 +114,7 @@ pub struct WebXRoute {
/// The pre-handler functions of the route.
pub pre_handlers: Vec<String>,
/// The code block of the route.
pub body: Option<String>,
pub body: Option<WebXBody>,
/// The post-handler functions of the route.
pub post_handlers: Vec<String>,
}

0 comments on commit 7c6ad4c

Please sign in to comment.