polished up a bit and got the index offset working
This commit is contained in:
+44
-12
@@ -7,7 +7,6 @@ use std::
|
||||
use log::
|
||||
{
|
||||
info,
|
||||
trace,
|
||||
warn,
|
||||
debug,
|
||||
};
|
||||
@@ -20,34 +19,67 @@ mod api;
|
||||
#[tokio::main]
|
||||
async fn main()
|
||||
{
|
||||
// For debugging
|
||||
env_logger::init();
|
||||
// Tx and Rx allow you to pass data between threads
|
||||
let (tx,rx) = mpsc::channel();
|
||||
// Initialise the data strcut that will be sent out during API GET requests
|
||||
let data_to_send = Arc::new(Mutex::new(api::DataToSend
|
||||
{
|
||||
action_type: "initialise".to_string(),
|
||||
action_type: "begin".to_string(),
|
||||
content: "".to_string(),
|
||||
character: "".to_string(),
|
||||
}));
|
||||
|
||||
let data = Arc::clone(&data_to_send);
|
||||
|
||||
// setup the api stuff //
|
||||
|
||||
// Make clones of the data Arc for the two processes
|
||||
let data_clone1 = Arc::clone(&data_to_send);
|
||||
let tx_clone = tx.clone();
|
||||
// Spawn a thread for warp
|
||||
// Spawn a thread for warp api server
|
||||
tokio::spawn(
|
||||
async move {
|
||||
api::api_process(data, tx_clone).await;
|
||||
api::api_process(data_clone1, tx_clone).await;
|
||||
});
|
||||
debug!("and continue");
|
||||
|
||||
|
||||
// setup the parsing stuff //
|
||||
// Setup the characters hashmap which will store each character in it as a Character struct
|
||||
let mut characters = HashMap::<String, character::Character>::new();
|
||||
let file_contents: String = fs::read_to_string("stories/story.ha").unwrap();
|
||||
// Split the file contents into tokens
|
||||
|
||||
// Read the file and split it into tokens
|
||||
let file_contents: String = fs::read_to_string("stories/story.ha") // TODO make this a command line argument
|
||||
.unwrap_or_else(|err|
|
||||
{
|
||||
eprintln!("Failed to read file: {}", err);
|
||||
std::process::exit(2);
|
||||
});
|
||||
let tokens: Vec<&str> = file_contents
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
|
||||
let data_clone2 = Arc::clone(&data_to_send);
|
||||
// Run the parsing process
|
||||
let data_clone = Arc::clone(&data_to_send);
|
||||
match parsing::token_parse(&tokens, &mut characters, data_clone, &rx).await
|
||||
match parsing::token_parse(&tokens, &mut characters, data_clone2, &rx).await
|
||||
{
|
||||
Ok(()) => info!("Program exited successfully"),
|
||||
Err(error) => eprintln!("{}", error),
|
||||
// Exit with error or success
|
||||
Ok(()) =>
|
||||
{
|
||||
info!("Program exited successfully");
|
||||
let mut data = data_to_send.lock().unwrap();
|
||||
data.action_type = String::from("end");
|
||||
data.content = String::new();
|
||||
data.character = String::new();
|
||||
// Manually unlock the mutex
|
||||
std::mem::drop(data);
|
||||
let _ = rx.recv(); // Wait for the client to respond
|
||||
std::process::exit(0);
|
||||
},
|
||||
Err(error) =>
|
||||
{
|
||||
eprintln!("{}", error);
|
||||
std::process::exit(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user