Skip to content

Commit

Permalink
Improve req body format AST
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Sep 13, 2023
1 parent 59d5b10 commit 98c372c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/file/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

use super::webx::{
WXBody, WXBodyType, WXHandler, WXModel, WXRoute, WXRouteMethod, WXScope, WXUrlPath, WXROOT_PATH, WXUrlPathSegment,
WXBody, WXBodyType, WXHandler, WXModel, WXRoute, WXRouteMethod, WXScope, WXUrlPath, WXROOT_PATH, WXUrlPathSegment, WXRouteReqBody, WXRouteReqBodyDefinition,
};

struct WebXFileParser<'a> {
Expand Down Expand Up @@ -510,14 +510,26 @@ impl<'a> WebXFileParser<'a> {
/// form(name: string, age: number)
/// User
/// ```
fn parse_body_format(&mut self) -> Option<String> {
fn parse_body_format(&mut self) -> Option<WXRouteReqBody> {
let context = "parsing a request body format";
self.skip_whitespace(true);
let nc = self.peek();
if nc.is_some() && char::is_alphabetic(nc.unwrap()) {
let mut result = self.read_until(')');
result.push(self.expect_next_specific(')', context));
Some(result)
let name = self.parse_identifier();
let nc = self.peek();
if nc.is_some() && nc.unwrap() == '(' {
// Custom format with fields.
self.expect(context); // Consume the '('.
let fields = self.parse_type_pairs(true);
self.expect_next_specific(')', context);
Some(WXRouteReqBody::Definition(WXRouteReqBodyDefinition {
markup_type: name,
structure: fields,
}))
} else {
// User-defined model name reference.
Some(WXRouteReqBody::ModelReference(name))
}
} else {
None
}
Expand Down
14 changes: 13 additions & 1 deletion src/file/webx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,26 @@ impl fmt::Debug for WXBody {
}
}

#[derive(Debug)]
pub struct WXRouteReqBodyDefinition {
pub markup_type: String,
pub structure: Vec<WXTypedIdentifier>,
}

#[derive(Debug)]
pub enum WXRouteReqBody {
ModelReference(String),
Definition(WXRouteReqBodyDefinition),
}

#[derive(Debug)]
pub struct WXRoute {
/// HTTP method of the route.
pub method: WXRouteMethod,
/// The path of the route.
pub path: WXUrlPath,
/// Request body format.
pub body_format: Option<String>,
pub body_format: Option<WXRouteReqBody>,
/// The pre-handler functions of the route.
pub pre_handlers: Vec<String>,
/// The code block of the route.
Expand Down

0 comments on commit 98c372c

Please sign in to comment.