whiskers.git / backend / worker / src / wire.rs
wire.rsannotatedwire.rssource49 lines · 1.9 KB · raw
1//! What crosses between the runtime's `Request` and `Response` and the service's, as pure functions.
2
3use whiskers_service::Content;
4
5/// The `Content-Type` a response of this kind carries, as `whiskersd` sends it (so every host answers
6/// the same bytes with the same type).
7pub fn content_type(content: Content) -> Option<&'static str> {
8    match content {
9        Content::None => None,
10        Content::Json => Some("application/json"),
11        Content::Text => Some("text/plain; charset=UTF-8"),
12        Content::Mpeg => Some("audio/mpeg"),
13    }
14}
15
16/// The request's class for the access line: a fixed word per route, never the path as sent.
17pub fn route_class(path: &str) -> &'static str {
18    match path {
19        "/memory/sync" => "memory-sync",
20        "/household/sync" => "household-sync",
21        "/chat/sync" => "chat-sync",
22        "/check" | "/rerank" | "/embed" | "/speak" | "/speak/v2" | "/usage" | "/v1/messages" => "other-known",
23        "/journal/push" | "/journal/pull" | "/picture/missing" | "/picture/put" | "/picture/get" => "other-known",
24        p if p.starts_with("/_hub/") => "test-hub",
25        _ => "unknown",
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn each_kind_of_reply_has_the_type_the_native_server_gives_it() {
35        assert_eq!(content_type(Content::None), None);
36        assert_eq!(content_type(Content::Json), Some("application/json"));
37        assert_eq!(content_type(Content::Text), Some("text/plain; charset=UTF-8"));
38        assert_eq!(content_type(Content::Mpeg), Some("audio/mpeg"));
39    }
40
41    #[test]
42    fn a_path_is_reduced_to_a_class_and_goes_no_further() {
43        assert_eq!(route_class("/memory/sync"), "memory-sync");
44        assert_eq!(route_class("/journal/push"), "other-known");
45        assert_eq!(route_class("/_hub/memory/merge"), "test-hub");
46        let c = route_class("/some/secret/path?x=1");
47        assert_eq!(c, "unknown");
48    }
49}