pub struct Server { /* private fields */ }Expand description
An RCP/1 server.
Implementations§
Source§impl Server
impl Server
Sourcepub fn new() -> Server
pub fn new() -> Server
Examples found in repository?
examples/example_server.rs (line 57)
56fn build() -> Server {
57 let mut s = Server::new();
58 s.set_info("rcp-example", "1.0.0");
59 s.advertise(
60 Capability::Embed,
61 obj(&[("dimensions", (DIM as i64).into()), ("modes", vec![Json::from("dense")].into())]),
62 );
63 s.advertise(
64 Capability::Retrieve,
65 obj(&[
66 ("maxK", 100.into()),
67 ("modes", vec![Json::from("dense")].into()),
68 ("citations", true.into()),
69 ]),
70 );
71 s.advertise(
72 Capability::Graph,
73 obj(&[("ops", vec![Json::from("local"), Json::from("global")].into())]),
74 );
75
76 s.on(Method::EMBED, |params| {
77 // Accept `inputs` (preferred, spec §7.3) or the legacy `texts` synonym.
78 let items = params
79 .get("inputs")
80 .or_else(|| params.get("texts"))
81 .and_then(|v| v.as_array())
82 .cloned()
83 .unwrap_or_default();
84 let vectors: Vec<Json> = items
85 .iter()
86 .filter_map(|x| x.as_str())
87 .map(|t| Json::from(embed_one(t)))
88 .collect();
89 Ok(obj(&[("vectors", Json::Array(vectors)), ("dimensions", (DIM as i64).into())]))
90 });
91
92 s.on(Method::RETRIEVE, |params| {
93 let q = params.get_str("query").unwrap_or("");
94 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(10).max(0) as usize;
95 Ok(obj(&[("hits", search(q, k))]))
96 });
97
98 s.on(Method::GRAPH, |params| {
99 let op = params.get_str("op").unwrap_or("local");
100 match op {
101 "local" => {
102 let q = params.get_str("query").unwrap_or("");
103 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(5).max(0) as usize;
104 Ok(obj(&[("hits", search(q, k))]))
105 }
106 "global" => Ok(obj(&[
107 ("summary", "a global community summary would go here".into()),
108 ("communities", 1.into()),
109 ])),
110 other => Ok(obj(&[("op", other.into()), ("note", "unimplemented".into())])),
111 }
112 });
113
114 s
115}Sourcepub fn set_info(&mut self, name: &str, version: &str) -> &mut Self
pub fn set_info(&mut self, name: &str, version: &str) -> &mut Self
Examples found in repository?
examples/example_server.rs (line 58)
56fn build() -> Server {
57 let mut s = Server::new();
58 s.set_info("rcp-example", "1.0.0");
59 s.advertise(
60 Capability::Embed,
61 obj(&[("dimensions", (DIM as i64).into()), ("modes", vec![Json::from("dense")].into())]),
62 );
63 s.advertise(
64 Capability::Retrieve,
65 obj(&[
66 ("maxK", 100.into()),
67 ("modes", vec![Json::from("dense")].into()),
68 ("citations", true.into()),
69 ]),
70 );
71 s.advertise(
72 Capability::Graph,
73 obj(&[("ops", vec![Json::from("local"), Json::from("global")].into())]),
74 );
75
76 s.on(Method::EMBED, |params| {
77 // Accept `inputs` (preferred, spec §7.3) or the legacy `texts` synonym.
78 let items = params
79 .get("inputs")
80 .or_else(|| params.get("texts"))
81 .and_then(|v| v.as_array())
82 .cloned()
83 .unwrap_or_default();
84 let vectors: Vec<Json> = items
85 .iter()
86 .filter_map(|x| x.as_str())
87 .map(|t| Json::from(embed_one(t)))
88 .collect();
89 Ok(obj(&[("vectors", Json::Array(vectors)), ("dimensions", (DIM as i64).into())]))
90 });
91
92 s.on(Method::RETRIEVE, |params| {
93 let q = params.get_str("query").unwrap_or("");
94 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(10).max(0) as usize;
95 Ok(obj(&[("hits", search(q, k))]))
96 });
97
98 s.on(Method::GRAPH, |params| {
99 let op = params.get_str("op").unwrap_or("local");
100 match op {
101 "local" => {
102 let q = params.get_str("query").unwrap_or("");
103 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(5).max(0) as usize;
104 Ok(obj(&[("hits", search(q, k))]))
105 }
106 "global" => Ok(obj(&[
107 ("summary", "a global community summary would go here".into()),
108 ("communities", 1.into()),
109 ])),
110 other => Ok(obj(&[("op", other.into()), ("note", "unimplemented".into())])),
111 }
112 });
113
114 s
115}Sourcepub fn advertise(&mut self, cap: Capability, meta: Json) -> &mut Self
pub fn advertise(&mut self, cap: Capability, meta: Json) -> &mut Self
Advertise a capability with optional metadata (pass Json::object() for
none). Advertising is independent of registering a handler; a gated call
needs both or it is CapabilityMissing.
Examples found in repository?
examples/example_server.rs (lines 59-62)
56fn build() -> Server {
57 let mut s = Server::new();
58 s.set_info("rcp-example", "1.0.0");
59 s.advertise(
60 Capability::Embed,
61 obj(&[("dimensions", (DIM as i64).into()), ("modes", vec![Json::from("dense")].into())]),
62 );
63 s.advertise(
64 Capability::Retrieve,
65 obj(&[
66 ("maxK", 100.into()),
67 ("modes", vec![Json::from("dense")].into()),
68 ("citations", true.into()),
69 ]),
70 );
71 s.advertise(
72 Capability::Graph,
73 obj(&[("ops", vec![Json::from("local"), Json::from("global")].into())]),
74 );
75
76 s.on(Method::EMBED, |params| {
77 // Accept `inputs` (preferred, spec §7.3) or the legacy `texts` synonym.
78 let items = params
79 .get("inputs")
80 .or_else(|| params.get("texts"))
81 .and_then(|v| v.as_array())
82 .cloned()
83 .unwrap_or_default();
84 let vectors: Vec<Json> = items
85 .iter()
86 .filter_map(|x| x.as_str())
87 .map(|t| Json::from(embed_one(t)))
88 .collect();
89 Ok(obj(&[("vectors", Json::Array(vectors)), ("dimensions", (DIM as i64).into())]))
90 });
91
92 s.on(Method::RETRIEVE, |params| {
93 let q = params.get_str("query").unwrap_or("");
94 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(10).max(0) as usize;
95 Ok(obj(&[("hits", search(q, k))]))
96 });
97
98 s.on(Method::GRAPH, |params| {
99 let op = params.get_str("op").unwrap_or("local");
100 match op {
101 "local" => {
102 let q = params.get_str("query").unwrap_or("");
103 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(5).max(0) as usize;
104 Ok(obj(&[("hits", search(q, k))]))
105 }
106 "global" => Ok(obj(&[
107 ("summary", "a global community summary would go here".into()),
108 ("communities", 1.into()),
109 ])),
110 other => Ok(obj(&[("op", other.into()), ("note", "unimplemented".into())])),
111 }
112 });
113
114 s
115}Sourcepub fn on<F>(&mut self, method: &str, handler: F) -> &mut Self
pub fn on<F>(&mut self, method: &str, handler: F) -> &mut Self
Register a handler for method (a Method::* constant or its string).
Examples found in repository?
examples/example_server.rs (lines 76-90)
56fn build() -> Server {
57 let mut s = Server::new();
58 s.set_info("rcp-example", "1.0.0");
59 s.advertise(
60 Capability::Embed,
61 obj(&[("dimensions", (DIM as i64).into()), ("modes", vec![Json::from("dense")].into())]),
62 );
63 s.advertise(
64 Capability::Retrieve,
65 obj(&[
66 ("maxK", 100.into()),
67 ("modes", vec![Json::from("dense")].into()),
68 ("citations", true.into()),
69 ]),
70 );
71 s.advertise(
72 Capability::Graph,
73 obj(&[("ops", vec![Json::from("local"), Json::from("global")].into())]),
74 );
75
76 s.on(Method::EMBED, |params| {
77 // Accept `inputs` (preferred, spec §7.3) or the legacy `texts` synonym.
78 let items = params
79 .get("inputs")
80 .or_else(|| params.get("texts"))
81 .and_then(|v| v.as_array())
82 .cloned()
83 .unwrap_or_default();
84 let vectors: Vec<Json> = items
85 .iter()
86 .filter_map(|x| x.as_str())
87 .map(|t| Json::from(embed_one(t)))
88 .collect();
89 Ok(obj(&[("vectors", Json::Array(vectors)), ("dimensions", (DIM as i64).into())]))
90 });
91
92 s.on(Method::RETRIEVE, |params| {
93 let q = params.get_str("query").unwrap_or("");
94 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(10).max(0) as usize;
95 Ok(obj(&[("hits", search(q, k))]))
96 });
97
98 s.on(Method::GRAPH, |params| {
99 let op = params.get_str("op").unwrap_or("local");
100 match op {
101 "local" => {
102 let q = params.get_str("query").unwrap_or("");
103 let k = params.get("k").and_then(|v| v.as_i64()).unwrap_or(5).max(0) as usize;
104 Ok(obj(&[("hits", search(q, k))]))
105 }
106 "global" => Ok(obj(&[
107 ("summary", "a global community summary would go here".into()),
108 ("communities", 1.into()),
109 ])),
110 other => Ok(obj(&[("op", other.into()), ("note", "unimplemented".into())])),
111 }
112 });
113
114 s
115}Sourcepub fn handle(&mut self, request: &Json) -> Option<Json>
pub fn handle(&mut self, request: &Json) -> Option<Json>
Handle one JSON-RPC request → reply, or None for a notification that
warrants no response. Never panics.
Sourcepub fn handle_line(&mut self, line: &str) -> String
pub fn handle_line(&mut self, line: &str) -> String
Parse a JSON line, dispatch (single or batch), and return the reply string (“” when there is no response, e.g. an all-notification batch).
Sourcepub fn serve_stdio(&mut self)
pub fn serve_stdio(&mut self)
Serve newline-delimited JSON-RPC over stdin/stdout until shutdown/EOF.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Server
impl !Send for Server
impl !Sync for Server
impl !UnwindSafe for Server
impl Freeze for Server
impl Unpin for Server
impl UnsafeUnpin for Server
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more