taubyte_sdk/http/event/
body.rs1use super::{imports, Event, EventBody};
2use crate::errno::Errno;
3
4impl Event {
5 pub fn body(&self) -> EventBody {
6 EventBody {
7 consumed: false,
8 event: (self.event),
9 }
10 }
11}
12
13impl std::io::Read for EventBody {
14 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
15 if self.consumed {
16 return Ok(0);
17 }
18
19 let mut n = 0;
20
21 let err0 =
22 unsafe { imports::readHttpEventBody(self.event, buf.as_mut_ptr(), buf.len(), &mut n) };
23 if err0.is_errno(Errno::ErrorEOF) {
24 self.consumed = true;
27 Ok(n as usize)
28 } else if err0.ok() {
29 Ok(n as usize)
30 } else {
31 Err(std::io::Error::new(
32 std::io::ErrorKind::Other,
33 format!("reading body failed with: {}", err0),
34 ))
35 }
36 }
37}
38
39#[cfg(test)]
40pub mod test {
41 use crate::http::Event;
42 use std::io::Read;
43 pub static EXPECTED_BODY: &str = "Hello, world!";
44 pub static EXPECTED_ID: u32 = 0;
45
46 #[test]
47 fn test_body() {
48 let http: Event = Event {
49 event: (EXPECTED_ID),
50 };
51
52 let mut body = http.body();
53 let mut buffer = String::new();
54 body.read_to_string(&mut buffer).unwrap();
55
56 assert_eq!(buffer, EXPECTED_BODY);
57 }
58}