arti-rpcserver: Be careful about saying "result".

Even though json-rpc uses "result" to mean "a successful return value
from a method", we can't: Rust's `Result` type is so pervasive
that confusion would be inevitable.
This commit is contained in:
Nick Mathewson 2023-04-12 12:08:53 -04:00
parent 4d82bf4b98
commit 6bdfc5740f
3 changed files with 12 additions and 7 deletions

View File

@ -73,17 +73,22 @@ pub(crate) struct BoxedResponse {
/// The body of a response for an RPC client.
#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum ResponseBody {
/// The request has failed; no more responses will be sent in reply to it.
//
// TODO RPC: This should be a more specific type.
#[serde(rename = "error")]
Error(Box<dyn erased_serde::Serialize + Send>),
/// The request has succeeded; no more responses will be sent in reply to
/// it.
Result(Box<dyn erased_serde::Serialize + Send>),
///
/// Note that in the spec, this is called a "result": we don't propagate
/// that terminology into Rust, where `Result` has a different meaning.
#[serde(rename = "result")]
Success(Box<dyn erased_serde::Serialize + Send>),
/// The request included the `updates` flag to increment that incremental
/// progress information is acceptable.
#[serde(rename = "update")]
Update(Box<dyn erased_serde::Serialize + Send>),
}
@ -92,7 +97,7 @@ impl ResponseBody {
/// sent for this request.
pub(crate) fn is_final(&self) -> bool {
match self {
ResponseBody::Error(_) | ResponseBody::Result(_) => true,
ResponseBody::Error(_) | ResponseBody::Success(_) => true,
ResponseBody::Update(_) => false,
}
}
@ -114,7 +119,7 @@ impl std::fmt::Debug for ResponseBody {
match self {
Self::Error(arg0) => f.debug_tuple("Error").field(&json(arg0)).finish(),
Self::Update(arg0) => f.debug_tuple("Update").field(&json(arg0)).finish(),
Self::Result(arg0) => f.debug_tuple("Result").field(&json(arg0)).finish(),
Self::Success(arg0) => f.debug_tuple("Success").field(&json(arg0)).finish(),
}
}
}
@ -184,7 +189,7 @@ mod test {
fn fmt_replies() {
let resp = BoxedResponse {
id: RequestId::Int(7),
body: ResponseBody::Result(Box::new(DummyResponse {
body: ResponseBody::Success(Box::new(DummyResponse {
hello: 99,
world: "foo".into(),
})),

View File

@ -196,7 +196,7 @@ impl Session {
let (handle, fut) = Cancel::new(fut);
self.register_request(id.clone(), handle);
let fut = fut.map(|r| match r {
Ok(Ok(v)) => BoxedResponse { id, body: ResponseBody::Result(v) },
Ok(Ok(v)) => BoxedResponse { id, body: ResponseBody::Success(v) },
Ok(Err(e)) => BoxedResponse { id, body: e.into() },
// TODO RPC: This is not the correct error type.
Err(_cancelled) => BoxedResponse{ id, body: ResponseBody::Error(Box::new("hey i got cancelled")) }

View File

@ -88,7 +88,7 @@ mod test {
};
let r3 = BoxedResponse {
id: RequestId::Int(9),
body: ResponseBody::Result(Box::new(Empty {})),
body: ResponseBody::Success(Box::new(Empty {})),
};
// These should get serialized as follows.