-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on module structure and project analysis
- Loading branch information
1 parent
1f1024e
commit 943718a
Showing
7 changed files
with
211 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,44 @@ | ||
use std::path::Path; | ||
|
||
pub fn run(dir: &Path, prod: bool) { | ||
use crate::file::parser::parse_webx_file; | ||
use crate::project::{locate_webx_files, load_project_config, construct_dependency_tree, detect_circular_dependencies}; | ||
use crate::reporting::error::{exit_error, ERROR_READ_WEBX_FILES, ERROR_CIRCULAR_DEPENDENCY}; | ||
|
||
const PROJECT_CONFIG_FILE_NAME: &str = "webx.config.json"; | ||
|
||
pub fn run(root: &Path, prod: bool) { | ||
let config_file = root.join(PROJECT_CONFIG_FILE_NAME); | ||
let config = load_project_config(&config_file); | ||
let source_root = root.join(&config.src); | ||
let files = match locate_webx_files(&source_root) { | ||
Ok(files) => files, | ||
Err(err) => exit_error(format!("Failed to locate webx program files due to, {}", err), ERROR_READ_WEBX_FILES), | ||
}; | ||
|
||
let webx_modules = files.iter().map(|f| parse_webx_file(f)).collect::<Vec<_>>(); | ||
let errors = webx_modules.iter().filter(|m| m.is_err()).map(|m| m.as_ref().unwrap_err()).collect::<Vec<_>>(); | ||
if !errors.is_empty() { | ||
exit_error( | ||
format!( | ||
"Failed to parse webx files:\n{:?}", | ||
errors | ||
), | ||
ERROR_READ_WEBX_FILES, | ||
); | ||
} | ||
let webx_modules = webx_modules.iter().map(|m| m.as_ref().unwrap()).collect::<Vec<_>>(); | ||
let dependency_tree = construct_dependency_tree(&webx_modules); | ||
let circular_dependencies = detect_circular_dependencies(&dependency_tree); | ||
if !circular_dependencies.is_empty() { | ||
exit_error( | ||
format!( | ||
"Circular dependencies detected:\n{:?}", | ||
circular_dependencies | ||
), | ||
ERROR_CIRCULAR_DEPENDENCY, | ||
); | ||
} | ||
|
||
println!("Running web server in {} mode", if prod { "production" } else { "development" }); | ||
println!("Directory: {}", dir.display()); | ||
println!("Directory: {}", root.display()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod webx; | ||
pub mod parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::{path::PathBuf, io::BufReader}; | ||
use crate::file::webx::WebXFile; | ||
|
||
pub fn parse_webx_file(file: &PathBuf) -> Result<WebXFile, String> { | ||
let module = WebXFile { | ||
path: file.clone(), | ||
includes: vec![], | ||
scopes: vec![], | ||
}; | ||
let file_contents = std::fs::read_to_string(file).map_err(|e| e.to_string())?; | ||
|
||
let reader = BufReader::new(file_contents.as_bytes()); | ||
|
||
Ok(module) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::path::PathBuf; | ||
|
||
/// # WebX file format | ||
/// A module for working with WebX files. | ||
#[derive(Debug)] | ||
pub struct WebXFile { | ||
/// The path to the file. | ||
pub path: PathBuf, | ||
/// The dependencies of the file. | ||
pub includes: Vec<String>, | ||
/// Scopes defined in the file. | ||
/// Created by root and the `location` keyword. | ||
pub scopes: Vec<WebXScope>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WebXScope { | ||
/// Global TypeScript code block | ||
pub global_ts: String, | ||
/// ORM Model definitions | ||
pub models: Vec<WebXModel>, | ||
/// Handler functions | ||
pub handlers: Vec<WebXHandler>, | ||
/// Route endpoints | ||
pub routes: Vec<WebXRoute>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WebXModel { | ||
/// The name of the model. | ||
pub name: String, | ||
/// The fields of the model. | ||
pub fields: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WebXHandler { | ||
/// The name of the handler. | ||
pub name: String, | ||
/// The parameters of the handler. | ||
pub params: String, | ||
/// Return type of the handler. | ||
pub return_type: Option<String>, | ||
/// The typescript body of the handler. | ||
pub body: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum WebXRouteMethod { | ||
GET, | ||
POST, | ||
PUT, | ||
PATCH, | ||
DELETE, | ||
OPTIONS, | ||
HEAD, | ||
CONNECT, | ||
TRACE, | ||
ANY, | ||
} | ||
|
||
pub fn route_from_str(method: String) -> Result<WebXRouteMethod, String> { | ||
match method.to_uppercase().as_str() { | ||
"GET" => Ok(WebXRouteMethod::GET), | ||
"POST" => Ok(WebXRouteMethod::POST), | ||
"PUT" => Ok(WebXRouteMethod::PUT), | ||
"PATCH" => Ok(WebXRouteMethod::PATCH), | ||
"DELETE" => Ok(WebXRouteMethod::DELETE), | ||
"OPTIONS" => Ok(WebXRouteMethod::OPTIONS), | ||
"HEAD" => Ok(WebXRouteMethod::HEAD), | ||
"CONNECT" => Ok(WebXRouteMethod::CONNECT), | ||
"TRACE" => Ok(WebXRouteMethod::TRACE), | ||
"ANY" => Ok(WebXRouteMethod::ANY), | ||
_ => Err(format!("Invalid route method: {}", method)), | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct WebXRoute { | ||
/// HTTP method of the route. | ||
pub method: WebXRouteMethod, | ||
/// The path of the route. | ||
pub path: String, | ||
/// The pre-handler functions of the route. | ||
pub pre_handlers: Vec<String>, | ||
/// The code block of the route. | ||
pub code: Option<String>, | ||
/// The post-handler functions of the route. | ||
pub post_handlers: Vec<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters