chiark / gitweb /
response plumbing
[hippotat.git] / src / queue.rs
index f821eb0c36111b8b8a92069d222b92642c116a30..3a85f5c27b1886b3863ca22e033386a849ecc26b 100644 (file)
@@ -4,14 +4,48 @@
 
 use crate::prelude::*;
 
+// xxx are we using this at all ?
 #[derive(Default,Clone)]
-pub struct Queue<E> {
+pub struct PacketQueue<D> {
+  queue: VecDeque<D>,
+  content: usize,
+}
+
+impl<D> PacketQueue<D> where D: AsRef<[u8]> {
+  pub fn push_back(&mut self, data: D) {
+    self.content += data.as_ref().len();
+    self.queue.push_back(data);
+  }
+
+  pub fn pop_front(&mut self) -> Option<D> {
+    let data = self.queue.pop_front()?;
+    self.content -= data.as_ref().len();
+    Some(data)
+  }
+
+  pub fn content_count(&self) -> usize { self.queue.len() }
+  pub fn content_len(&self) -> usize { self.content }
+  pub fn total_len(&self) -> usize {
+    self.content_count() + self.content_len()
+  }
+
+  pub fn is_empty(&self) -> bool { self.queue.is_empty() }
+  pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
+}
+
+#[derive(Default,Clone)]
+pub struct QueueBuf<E> {
   content: usize,
   eaten1: usize, // 0 <= eaten1 < queue.front()...len()
   queue: VecDeque<E>,
 }
 
-impl<E> Debug for Queue<E> where E: AsRef<[u8]> {
+#[derive(Default,Debug,Clone)]
+pub struct FrameQueueBuf {
+  queue: QueueBuf<Cervine<'static, Box<[u8]>, [u8]>>,
+}
+
+impl<E> Debug for QueueBuf<E> where E: AsRef<[u8]> {
   #[throws(fmt::Error)]
   fn fmt(&self, f: &mut fmt::Formatter) {
     write!(f, "Queue{{content={},eaten1={},queue=[",
@@ -21,27 +55,39 @@ impl<E> Debug for Queue<E> where E: AsRef<[u8]> {
   }
 }
 
-impl<E> Queue<E> where E: AsRef<[u8]> {
+impl<E> QueueBuf<E> where E: AsRef<[u8]> {
   pub fn push<B: Into<E>>(&mut self, b: B) {
     self.push_(b.into());
   }
-  pub fn push_(&mut self, b: E) {
+  fn push_(&mut self, b: E) {
     let l = b.as_ref().len();
     self.queue.push_back(b);
     self.content += l;
   }
   pub fn is_empty(&self) -> bool { self.content == 0 }
+  pub fn len(&self) -> usize { self.content }
 }
 
-impl<E> Extend<E> for Queue<E> where E: AsRef<[u8]> {
-  fn extend<I>(&mut self, it: I)
-  where I: IntoIterator<Item=E>
-  {
-    for b in it { self.push(b) }
+impl FrameQueueBuf {
+  pub fn push_esc<B: Into<Box<[u8]>>>(&mut self, b: B) {
+    self.push_esc_(b.into());
+  }
+  fn push_esc_(&mut self, b: Box<[u8]>) {
+    self.queue.push_(Cervine::Owned(b));
+    self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
   }
+  pub fn esc_push(&mut self, b: Box<[u8]>) {
+    self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
+    self.queue.push_(Cervine::Owned(b));
+  }
+  pub fn push_raw(&mut self, b: Box<[u8]>) {
+    self.queue.push_(Cervine::Owned(b));
+  }
+  pub fn is_empty(&self) -> bool { self.queue.is_empty() }
+  pub fn len(&self) -> usize { self.queue.len() }
 }
 
-impl<E> hyper::body::Buf for Queue<E> where E: AsRef<[u8]> {
+impl<E> hyper::body::Buf for QueueBuf<E> where E: AsRef<[u8]> {
   fn remaining(&self) -> usize { self.content }
   fn chunk(&self) -> &[u8] {
     let front = if let Some(f) = self.queue.front() { f } else { return &[] };
@@ -59,3 +105,39 @@ impl<E> hyper::body::Buf for Queue<E> where E: AsRef<[u8]> {
     }
   }
 }
+
+impl hyper::body::Buf for FrameQueueBuf {
+  fn remaining(&self) -> usize { self.queue.remaining() }
+  fn chunk(&self) -> &[u8] { self.queue.chunk() }
+  fn advance(&mut self, cnt: usize) { self.queue.advance(cnt) }
+}
+
+pin_project!{
+  pub struct BufBody<B:Buf> {
+    body: Option<B>,
+  }
+}
+impl<B:Buf> BufBody<B> {
+  pub fn new(body: B) -> Self { Self { body: Some(body ) } }
+}
+impl BufBody<FrameQueueBuf> {
+  pub fn display<S:Display>(s: S) -> Self {
+    let s = s.to_string().into_bytes();
+    let mut buf: FrameQueueBuf = default();
+    buf.push_raw(s.into());
+    Self::new(buf)
+  }
+}
+
+impl<B:Buf> HttpBody for BufBody<B> {
+  type Error = Void;
+  type Data = B;
+  fn poll_data(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
+               -> Poll<Option<Result<B, Void>>> {
+    Poll::Ready(Ok(self.project().body.take()).transpose())
+  }
+  fn poll_trailers(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
+ -> Poll<Result<Option<hyper::HeaderMap<hyper::header::HeaderValue>>, Void>> {
+    Poll::Ready(Ok(None))
+  }
+}