Skip to content

Commit

Permalink
Implement support for shutdown and exit
Browse files Browse the repository at this point in the history
Does not yet fully adhear to the spec.
  • Loading branch information
ExcaliburZero committed Nov 4, 2023
1 parent 5fd240c commit 6a0a0c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ use std::io::{stdin, stdout, BufReader};
use quickbms_lsp::server::server;

fn main() {
server::run(BufReader::new(stdin()), stdout());
let exit_code = server::run(BufReader::new(stdin()), stdout());
std::process::exit(exit_code);
}
23 changes: 21 additions & 2 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use serde_json::{self, from_value, to_value, Value};

use crate::server::state::ServerState;

pub fn run<R, W>(mut input: R, mut output: W)
const EXIT_STRING: &str = "quickbms-lsp EXIT (-.-) zzz";

pub fn run<R, W>(mut input: R, mut output: W) -> i32
where
R: BufRead,
W: Write,
Expand All @@ -30,6 +32,11 @@ where

let response = io.handle_request_sync(&message.content);
if let Some(response) = response {
// TODO: Do this by using state instead
if response.contains(EXIT_STRING) {
return 0;
}

let response_message = Message::from_content(&response);
write!(output, "{}", response_message).unwrap();
output.flush().unwrap();
Expand Down Expand Up @@ -147,6 +154,18 @@ fn setup_handler() -> IoHandler {
}
});

// let state_c = state.clone();
io.add_sync_method("shutdown", move |_| {
eprintln!("Shutting down...");
Ok(Value::Null)
});

// let state_c = state.clone();
io.add_sync_method("exit", move |_| {
eprintln!("Exiting...");
Ok(Value::String(EXIT_STRING.to_string()))
});

io
}

Expand Down Expand Up @@ -182,7 +201,7 @@ impl Header {
}

fn parse_content_length(line: &str) -> u64 {
let re = Regex::new(r"^Content-Length: (\d+)\r\n").unwrap();
let re = Regex::new(r"^Content-Length: (\d+)").unwrap();
let length_string = re.captures(line).unwrap()[1].to_string();

length_string.parse::<u64>().unwrap()
Expand Down

0 comments on commit 6a0a0c6

Please sign in to comment.