sausage-plugin part 1: run, #[rpc], #[schema], rpc.discover, calls, locks, Ctx, hooks
Checkpoint. Known issues, fixed in part 2: short writes on stdout, byte-wise stdin reads, Result suffix on all result schemas, lost enum variant docs, serde errors as invalid params, missing item docs.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
//! Shared access to OpenRPC documents and their Sausage extensions.
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// An OpenRPC document, including JSON Schema and extension fields.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Document(pub Value);
|
||||
|
||||
impl Document {
|
||||
/// Create an empty main interface.
|
||||
pub fn new(title: &str, version: &str) -> Self {
|
||||
Self(
|
||||
json!({"openrpc":"1.2.6", "info":{"title":title,"version":version},
|
||||
"methods":[], "components":{"schemas":{}},
|
||||
"x-sausage-endpoints":{}, "x-sausage-imports":{}}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Read the method descriptions.
|
||||
pub fn methods(&self) -> &[Value] {
|
||||
self.0["methods"]
|
||||
.as_array()
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Read a named schema.
|
||||
pub fn schema(&self, name: &str) -> Option<&Value> {
|
||||
self.0.get("components")?.get("schemas")?.get(name)
|
||||
}
|
||||
|
||||
/// Consume the document as a JSON value.
|
||||
pub fn into_value(self) -> Value {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user