Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Sep 13, 2023
1 parent 97c42aa commit 2bfddf1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
49 changes: 17 additions & 32 deletions src/file/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use crate::{
};
use std::{
io::{BufReader, Read},
path::PathBuf, fmt::format,
path::PathBuf,
};

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

struct WebXFileParser<'a> {
Expand Down Expand Up @@ -354,12 +355,18 @@ impl<'a> WebXFileParser<'a> {
let mut pairs = vec![];
loop {
let pair = self.parse_type_pair();
if pair.name.is_empty() { break; } // Empty name means end of type pairs.
if pair.name.is_empty() {
break;
} // Empty name means end of type pairs.
pairs.push(pair);
let nc = self.peek();
if nc.is_none() { break; }
if nc.is_none() {
break;
}
let nc = nc.unwrap();
if nc != ',' { break; } // No comma means end of type pairs.
if nc != ',' {
break;
} // No comma means end of type pairs.
self.next(); // Consume the comma.
self.skip_whitespace(true);
if allow_stray_comma && !char::is_alphabetic(self.peek().unwrap()) {
Expand Down Expand Up @@ -457,22 +464,6 @@ impl<'a> WebXFileParser<'a> {
WXHandler { name, params, body }
}

/// Parse a URL path variable segment.
/// **Obs: This function does not parse the opening parenthesis.**
///
/// ## Example:
/// ```ignore
/// (arg: string)?
/// ```
fn parse_url_path_variable(&mut self) -> String {
let context = "parsing a URL path variable segment";
let name = self.read_until(':').trim().to_string();
self.expect_next_specific(':', context);
let type_ = self.read_until(')').trim().to_string();
self.expect_next_specific(')', context);
format!("({}:{})", name, type_)
}

/// Parse a URL path.
/// ## Supporting syntax:
/// - Static path segments
Expand All @@ -494,14 +485,14 @@ impl<'a> WebXFileParser<'a> {
'(' => {
segments.push(WXUrlPathSegment::Parameter(self.parse_type_pair()));
self.expect_next_specific(')', context);
},
}
'*' => segments.push(WXUrlPathSegment::Regex("*".to_string())),
'/' => {
let nc = self.peek();
if nc.is_some() && char::is_alphanumeric(nc.unwrap()) {
segments.push(WXUrlPathSegment::Literal(self.parse_identifier()));
}
},
}
c if c.is_alphabetic() => {
let mut name = c.to_string();
name.push_str(&self.parse_identifier());
Expand Down Expand Up @@ -739,21 +730,15 @@ impl<'a> WebXFileParser<'a> {
},
'd' => {
self.expect_specific_str("delete", 1, context);
scope
.routes
.push(self.parse_route(WXRouteMethod::DELETE)?);
scope.routes.push(self.parse_route(WXRouteMethod::DELETE)?);
}
'c' => {
self.expect_specific_str("connect", 1, context);
scope
.routes
.push(self.parse_route(WXRouteMethod::CONNECT)?);
scope.routes.push(self.parse_route(WXRouteMethod::CONNECT)?);
}
'o' => {
self.expect_specific_str("options", 1, context);
scope
.routes
.push(self.parse_route(WXRouteMethod::OPTIONS)?);
scope.routes.push(self.parse_route(WXRouteMethod::OPTIONS)?);
}
't' => {
self.expect_specific_str("trace", 1, context);
Expand Down
25 changes: 13 additions & 12 deletions src/file/webx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub type WXType = String;
#[derive(Clone)]
pub struct WXTypedIdentifier {
pub name: String,
pub type_: WXType
pub type_: WXType,
}

impl fmt::Debug for WXTypedIdentifier {
Expand All @@ -31,17 +31,19 @@ impl fmt::Debug for WXUrlPath {
let c = self.0.clone();
let ss = c
.into_iter()
.map(|segment| {
match segment {
WXUrlPathSegment::Literal(literal) => literal,
WXUrlPathSegment::Parameter(WXTypedIdentifier { name, type_ }) => format!("({}: {})", name, type_),
WXUrlPathSegment::Regex(regex) => format!("({})", regex)
.map(|segment| match segment {
WXUrlPathSegment::Literal(literal) => literal,
WXUrlPathSegment::Parameter(WXTypedIdentifier { name, type_ }) => {
format!("({}: {})", name, type_)
}
WXUrlPathSegment::Regex(regex) => format!("({})", regex),
})
.collect::<Vec<_>>();
write!(f, "/")?;
for (i, s) in ss.iter().enumerate() {
if i > 0 { write!(f, "/")?; }
if i > 0 {
write!(f, "/")?;
}
write!(f, "{}", s)?;
}
Ok(())
Expand Down Expand Up @@ -148,17 +150,14 @@ pub enum WXRouteMethod {
pub enum WXBodyType {
TS,
TSX,
JSON,
TEXT,
// TODO: JSON and TEXT
}

impl ToString for WXBodyType {
fn to_string(&self) -> String {
match self {
WXBodyType::TS => "ts".to_string(),
WXBodyType::TSX => "tsx".to_string(),
WXBodyType::JSON => "json".to_string(),
WXBodyType::TEXT => "text".to_string(),
}
}
}
Expand Down Expand Up @@ -186,7 +185,9 @@ impl fmt::Debug for WXRouteReqBody {
WXRouteReqBody::Definition(name, fields) => {
write!(f, "{}(", name)?;
for (i, field) in fields.iter().enumerate() {
if i > 0 { write!(f, ", ")?; }
if i > 0 {
write!(f, ", ")?;
}
field.fmt(f)?;
}
write!(f, ")")
Expand Down

0 comments on commit 2bfddf1

Please sign in to comment.