use serde_json::{json, Value};
pub mod error_code {
pub const PARSE_ERROR: i64 = -32700;
pub const INVALID_REQUEST: i64 = -32600;
pub const METHOD_NOT_FOUND: i64 = -32601;
pub const INVALID_PARAMS: i64 = -32602;
}
#[derive(Debug, Clone)]
pub struct RpcError {
pub code: i64,
pub message: String,
}
impl RpcError {
pub fn new(code: i64, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn invalid_params(message: impl Into<String>) -> Self {
Self::new(error_code::INVALID_PARAMS, message)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Incoming {
Request {
id: Value,
method: String,
params: Value,
},
Notification { method: String, params: Value },
Response,
}
pub fn response_result(id: Value, result: Value) -> String {
let mut line = serde_json::to_string(&json!({
"jsonrpc": "2.0",
"id": id,
"result": result,
}))
.expect("responses are always serializable");
line.push('\n');
line
}
pub fn response_error(id: Value, code: i64, message: impl Into<String>) -> String {
let mut line = serde_json::to_string(&json!({
"jsonrpc": "2.0",
"id": id,
"error": { "code": code, "message": message.into() },
}))
.expect("responses are always serializable");
line.push('\n');
line
}
pub fn parse_message(line: &str) -> Result<Incoming, (Value, RpcError)> {
let trimmed = line.trim();
if trimmed.is_empty() {
return Ok(Incoming::Notification {
method: String::new(),
params: Value::Null,
});
}
let value: Value = serde_json::from_str(trimmed).map_err(|e| {
(
Value::Null,
RpcError::new(error_code::PARSE_ERROR, e.to_string()),
)
})?;
let obj = match value.as_object() {
Some(o) => o,
None => {
return Err((
Value::Null,
RpcError::new(
error_code::INVALID_REQUEST,
"expected a single JSON-RPC object (batching is not supported)",
),
));
}
};
let method = obj
.get("method")
.and_then(|m| m.as_str())
.map(str::to_string);
let params = obj.get("params").cloned().unwrap_or(Value::Null);
match (obj.get("id").cloned(), method) {
(Some(id), Some(method)) => Ok(Incoming::Request { id, method, params }),
(None, Some(method)) => Ok(Incoming::Notification { method, params }),
(id, None) => {
if obj.get("result").is_some() || obj.get("error").is_some() {
Ok(Incoming::Response)
} else {
Err((
id.unwrap_or(Value::Null),
RpcError::new(error_code::INVALID_REQUEST, "message has no method"),
))
}
}
}
}
pub fn process_line<F>(line: &str, mut handle: F) -> Option<String>
where
F: FnMut(&str, &Value) -> Result<Value, RpcError>,
{
match parse_message(line) {
Err((id, e)) => Some(response_error(id, e.code, e.message)),
Ok(Incoming::Notification { method, params }) => {
let _ = handle(&method, ¶ms); None
}
Ok(Incoming::Response) => None, Ok(Incoming::Request { id, method, params }) => match handle(&method, ¶ms) {
Ok(result) => Some(response_result(id, result)),
Err(e) => Some(response_error(id, e.code, e.message)),
},
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ok_handler() -> impl FnMut(&str, &Value) -> Result<Value, RpcError> {
|method: &str, params: &Value| {
if method == "echo" {
Ok(params.clone())
} else if method == "fail" {
Err(RpcError::invalid_params("boom"))
} else if method == "notifications/anything" {
Ok(json!({"sneaky": true}))
} else {
Err(RpcError::new(
error_code::METHOD_NOT_FOUND,
format!("no {method}"),
))
}
}
}
#[test]
fn request_answered_with_result() {
let out = process_line(
r#"{"jsonrpc":"2.0","id":7,"method":"echo","params":{"a":1}}"#,
ok_handler(),
)
.unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["jsonrpc"], "2.0");
assert_eq!(v["id"], 7);
assert_eq!(v["result"]["a"], 1);
assert!(out.ends_with('\n'), "responses are newline-terminated");
}
#[test]
fn string_ids_round_trip() {
let out = process_line(
r#"{"jsonrpc":"2.0","id":"call-1","method":"echo"}"#,
ok_handler(),
)
.unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["id"], "call-1");
}
#[test]
fn handler_error_becomes_jsonrpc_error() {
let out =
process_line(r#"{"jsonrpc":"2.0","id":2,"method":"fail"}"#, ok_handler()).unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["error"]["code"], -32602);
assert_eq!(v["error"]["message"], "boom");
}
#[test]
fn unknown_method_is_method_not_found() {
let out =
process_line(r#"{"jsonrpc":"2.0","id":3,"method":"nope"}"#, ok_handler()).unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["error"]["code"], -32601);
}
#[test]
fn notifications_produce_no_response_even_if_handler_returns_one() {
assert!(process_line(
r#"{"jsonrpc":"2.0","method":"notifications/anything"}"#,
ok_handler()
)
.is_none());
}
#[test]
fn garbage_line_is_a_parse_error_with_null_id() {
let out = process_line("not json at all", ok_handler()).unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["id"], Value::Null);
assert_eq!(v["error"]["code"], -32700);
}
#[test]
fn batch_arrays_are_rejected_not_expanded() {
let out = process_line(
r#"[{"jsonrpc":"2.0","id":1,"method":"echo"}]"#,
ok_handler(),
)
.unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["error"]["code"], -32600);
assert!(v["error"]["message"].to_string().contains("batch"));
}
#[test]
fn id_without_method_is_ignored_without_response() {
assert!(
process_line(r#"{"jsonrpc":"2.0","id":9,"result":{"x":1}}"#, ok_handler()).is_none()
);
}
#[test]
fn blank_line_is_skipped() {
assert!(process_line(" ", ok_handler()).is_none());
}
#[test]
fn params_default_to_null() {
let out =
process_line(r#"{"jsonrpc":"2.0","id":4,"method":"echo"}"#, ok_handler()).unwrap();
let v: Value = serde_json::from_str(out.trim()).unwrap();
assert_eq!(v["result"], Value::Null);
}
}