Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Fix rebase and apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Jul 10, 2017
1 parent 9064964 commit 25085aa
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl ActionHandler {
}
_ => {
debug!("Error in Racer");
out.failure_simple(id, ErrorCode::InternalError, "GotoDef failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "GotoDef failed to complete successfully");
}
}
}
Expand Down Expand Up @@ -449,7 +449,7 @@ impl ActionHandler {
out.success(id, ResponseData::HoverSuccess(r));
}
Err(_) => {
out.failure_simple(id, ErrorCode::InternalError, "Hover failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Hover failed to complete successfully");
}
}
}
Expand All @@ -463,7 +463,7 @@ impl ActionHandler {
}
c => {
debug!("Unknown command: {}", c);
out.failure_simple(id, ErrorCode::MethodNotFound, "Unknown command");
out.failure_message(id, ErrorCode::MethodNotFound, "Unknown command");
}
}
}
Expand Down Expand Up @@ -520,17 +520,17 @@ impl ActionHandler {

// Start by checking that the user has selected a glob import.
if span.range.start() == span.range.end() {
out.failure_simple(id, ErrorCode::InvalidParams, "Empty selection");
out.failure_message(id, ErrorCode::InvalidParams, "Empty selection");
return;
}
match self.vfs.load_span(span.clone()) {
Ok(ref s) if s != "*" => {
out.failure_simple(id, ErrorCode::InvalidParams, "Not a glob");
out.failure_message(id, ErrorCode::InvalidParams, "Not a glob");
return;
}
Err(e) => {
debug!("Deglob failed: {:?}", e);
out.failure_simple(id, ErrorCode::InternalError, "Couldn't open file");
out.failure_message(id, ErrorCode::InternalError, "Couldn't open file");
return;
}
_ => {}
Expand All @@ -551,7 +551,7 @@ impl ActionHandler {
let mut deglob_str = match result {
Ok(Ok(s)) => s,
_ => {
out.failure_simple(id, ErrorCode::InternalError, "Couldn't get info from analysis");
out.failure_message(id, ErrorCode::InternalError, "Couldn't get info from analysis");
return;
}
};
Expand Down Expand Up @@ -582,12 +582,12 @@ impl ActionHandler {
Ok(FileContents::Text(s)) => FmtInput::Text(s),
Ok(_) => {
debug!("Reformat failed, found binary file");
out.failure_simple(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
return;
}
Err(e) => {
debug!("Reformat failed: {:?}", e);
out.failure_simple(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
return;
}
};
Expand Down Expand Up @@ -629,12 +629,12 @@ impl ActionHandler {
} else {
debug!("reformat: format_input failed: has errors, summary = {:?}", summary);

out.failure_simple(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
}
}
Err(e) => {
debug!("Reformat failed: {:?}", e);
out.failure_simple(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use analysis::{AnalysisHost, Target};
use config::Config;
use server::{self, ServerMessage, Request, Notification, Method, LsService, ParseError, ResponseData};
use server::{self, ServerMessage, Request, Notification, Method, LsService, ResponseData};
use vfs::Vfs;
use jsonrpc_core;

Expand Down
27 changes: 13 additions & 14 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use jsonrpc_core::{self as jsonrpc, Id, response, version};
pub fn server_failure(id: jsonrpc::Id, error: jsonrpc::Error) -> jsonrpc::Failure {
jsonrpc::Failure {
jsonrpc: Some(version::Version::V2),
id: id,
error: error,
id,
error,
}
}

Expand Down Expand Up @@ -117,6 +117,8 @@ macro_rules! messages {
$($method_name$(($method_arg))*,)*
}

// TODO: Change return type to Result<ServerMessage, Option<jsonrpc::Failure>>,
// since Notifications don't have id and only Requests can return a response
fn parse_message(input: &str) -> Result<ServerMessage, jsonrpc::Failure> {
trace!("parse_message `{}`", input);
let ls_command: serde_json::Value = serde_json::from_str(input).expect("Can't make value");
Expand All @@ -131,14 +133,11 @@ macro_rules! messages {
});
}

let id = ls_command.get("id").expect("no id").to_owned();
let id: jsonrpc::Id = serde_json::from_value(id).expect("Can't extract id");

// FIXME: Currently we only support numbers as strings
// TODO: Change used id from usize to u64 or ideally jsonrpc::Id
// Per JSON-RPC/LSP spec, Requests must have id, whereas Notifications can't
let id = ls_command.get("id").map(|id| serde_json::from_value(id.to_owned()).unwrap());
let parsed_numeric_id = match &id {
&Id::Num(ref num) => Some(num.clone() as usize),
&Id::Str(ref string) => usize::from_str_radix(string, 10).ok(),
&Some(Id::Num(n)) => Some(n as usize),
&Some(Id::Str(ref s)) => usize::from_str_radix(s, 10).ok(),
_ => None,
};

Expand All @@ -149,7 +148,7 @@ macro_rules! messages {
$method_str => {
match parsed_numeric_id {
Some(id) => Ok(ServerMessage::Request(Request{id, method: Method::$method_name$((params_as!($method_arg)))* })),
None => Err(server_failure(id, jsonrpc::Error::invalid_params("Id is not a number or numeric string"))),
None => Err(server_failure(id.unwrap_or(Id::Null), jsonrpc::Error::invalid_params("Id is not a number or numeric string"))),
}
}
)*
Expand All @@ -160,14 +159,14 @@ macro_rules! messages {
)*
$(
// If $other_expr is Err, then we need to pass id of actual message we're handling
$other_str => { ($other_expr).map_err(|err| server_failure(id, err)) }
$other_str => { ($other_expr).map_err(|err| server_failure(id.unwrap_or(Id::Null), err)) }
)*
}
} else {
Err(server_failure(id, jsonrpc::Error::invalid_params("Method is not a string")))
Err(server_failure(id.unwrap_or(Id::Null), jsonrpc::Error::invalid_params("Method is not a string")))
}
} else {
Err(server_failure(id, jsonrpc::Error::method_not_found()))
Err(server_failure(id.unwrap_or(Id::Null), jsonrpc::Error::method_not_found()))
}
}

Expand Down Expand Up @@ -566,7 +565,7 @@ pub trait Output: Sync + Send + Clone + 'static {
self.response(serde_json::to_string(&response).unwrap());
}

fn failure_simple<M: Into<String>>(&self, id: usize, code: jsonrpc::ErrorCode, msg: M) {
fn failure_message<M: Into<String>>(&self, id: usize, code: jsonrpc::ErrorCode, msg: M) {
let error = jsonrpc::Error {
code: code,
message: msg.into(),
Expand Down
3 changes: 1 addition & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,5 @@ fn test_parse_error_on_malformed_input() {
let failure: jsonrpc_core::Failure = serde_json::from_str(&error)
.expect("Couldn't parse json failure response");

const PARSE_ERROR: jsonrpc_core::ErrorCode = jsonrpc_core::ErrorCode::ParseError;
assert!(failure.error.code == PARSE_ERROR);
assert!(failure.error.code == jsonrpc_core::ErrorCode::ParseError);
}

0 comments on commit 25085aa

Please sign in to comment.