File
Blob: src/rust/worker/ok.rs
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | use std::pin::Pin; |
| 6 | use std::time::SystemTime; |
| 7 | |
| 8 | use cxx::KjError; |
| 9 | use cxx::KjExceptionType; |
| 10 | use kj::http::ConnectResponse; |
| 11 | use kj::http::ConnectSettings; |
| 12 | use kj::http::HeadersRef; |
| 13 | use kj::http::Method; |
| 14 | use kj::http::ServiceResponse; |
| 15 | use kj::io::AsyncInputStream; |
| 16 | use kj::io::AsyncIoStream; |
| 17 | |
| 18 | use crate::AlarmResult; |
| 19 | use crate::Interface; |
| 20 | use crate::ScheduledResult; |
| 21 | use crate::ffi::Wrapper; |
| 22 | |
| 23 | #[cxx::bridge(namespace = "workerd::rust::worker")] |
| 24 | pub mod bridge { |
| 25 | extern "Rust" { |
| 26 | type Wrapper = crate::ffi::Wrapper; |
| 27 | |
| 28 | #[expect( |
| 29 | clippy::unnecessary_box_returns, |
| 30 | reason = "c++ expects heap-allocation" |
| 31 | )] |
| 32 | fn new_ok_worker() -> Box<Wrapper>; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// Test worker replying 200 "OK". |
| 37 | pub struct Worker; |
| 38 | |
| 39 | impl Worker { |
| 40 | fn not_implemented(name: &str) -> KjError { |
| 41 | KjError::new( |
| 42 | KjExceptionType::Unimplemented, |
| 43 | format!("{name} not implemented"), |
| 44 | ) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[async_trait::async_trait(?Send)] |
| 49 | impl kj::http::Service for Worker { |
| 50 | async fn request<'a>( |
| 51 | &'a mut self, |
| 52 | _method: Method, |
| 53 | _url: &'a [u8], |
| 54 | headers: HeadersRef<'a>, |
| 55 | _request_body: Pin<&'a mut AsyncInputStream>, |
| 56 | response: ServiceResponse<'a>, |
| 57 | ) -> crate::Result<()> { |
| 58 | let headers = headers.clone_shallow(); |
| 59 | let mut body = response.send(200, "OK", &headers, Some(2_u64))?; |
| 60 | body.write(b"OK").await?; |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | async fn connect<'a>( |
| 65 | &'a mut self, |
| 66 | _host: &'a [u8], |
| 67 | _headers: HeadersRef<'a>, |
| 68 | _connection: Pin<&'a mut AsyncIoStream>, |
| 69 | _response: ConnectResponse<'a>, |
| 70 | _settings: ConnectSettings<'a>, |
| 71 | ) -> crate::Result<()> { |
| 72 | Err(Self::not_implemented("connect")) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #[async_trait::async_trait(?Send)] |
| 77 | impl Interface for Worker { |
| 78 | async fn run_scheduled( |
| 79 | &mut self, |
| 80 | _scheduled_time: &SystemTime, |
| 81 | _cron: &str, |
| 82 | ) -> crate::Result<ScheduledResult> { |
| 83 | Err(Self::not_implemented("run_scheduled")) |
| 84 | } |
| 85 | |
| 86 | async fn run_alarm( |
| 87 | &mut self, |
| 88 | _scheduled_time: &SystemTime, |
| 89 | _retry_count: u32, |
| 90 | ) -> crate::Result<AlarmResult> { |
| 91 | Err(Self::not_implemented("run_alarm")) |
| 92 | } |
| 93 | |
| 94 | async fn custom_event( |
| 95 | &mut self, |
| 96 | _event: Pin<&mut crate::CustomEvent>, |
| 97 | ) -> crate::Result<crate::CustomEventResult> { |
| 98 | Err(Self::not_implemented("custom_event")) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn new_ok_worker() -> Box<Wrapper> { |
| 103 | Worker.into_ffi() |
| 104 | } |