|
| 1 | +//! Writing Cabrillo contest log format |
| 2 | +
|
| 3 | +use crate::{Error, Record}; |
| 4 | +use bytes::{BufMut, BytesMut}; |
| 5 | +use futures::sink::Sink; |
| 6 | +use std::pin::Pin; |
| 7 | +use std::task::{Context, Poll}; |
| 8 | +use tokio::io::AsyncWrite; |
| 9 | +use tokio_util::codec::{Encoder, FramedWrite}; |
| 10 | + |
| 11 | +#[cfg(test)] |
| 12 | +mod test; |
| 13 | + |
| 14 | +enum Item { |
| 15 | + Record(Record), |
| 16 | + Eof, |
| 17 | +} |
| 18 | + |
| 19 | +struct CabrilloEncoder { |
| 20 | + fields: Vec<String>, |
| 21 | + started: bool, |
| 22 | +} |
| 23 | + |
| 24 | +impl CabrilloEncoder { |
| 25 | + fn new(fields: Vec<String>) -> Self { |
| 26 | + Self { |
| 27 | + fields, |
| 28 | + started: false, |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + fn encode_header( |
| 33 | + &mut self, r: Record, dst: &mut BytesMut, |
| 34 | + ) -> Result<(), Error> { |
| 35 | + if self.started { |
| 36 | + return Err(Error::DuplicateHeader); |
| 37 | + } |
| 38 | + |
| 39 | + self.started = true; |
| 40 | + dst.put_slice(b"START-OF-LOG: 3.0\n"); |
| 41 | + |
| 42 | + for (name, value) in r.fields() { |
| 43 | + let key = name.to_uppercase(); |
| 44 | + let val = value.to_cabrillo(); |
| 45 | + |
| 46 | + dst.put_slice(key.as_bytes()); |
| 47 | + dst.put_slice(b": "); |
| 48 | + dst.put_slice(val.as_bytes()); |
| 49 | + dst.put_slice(b"\n"); |
| 50 | + } |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + fn encode_qso(&self, r: Record, dst: &mut BytesMut) -> Result<(), Error> { |
| 55 | + dst.put_slice(b"QSO: "); |
| 56 | + |
| 57 | + for (i, f) in self.fields.iter().enumerate() { |
| 58 | + let d = r.get(f).ok_or_else(|| Error::MissingField { |
| 59 | + field: f.clone(), |
| 60 | + record: r.clone(), |
| 61 | + })?; |
| 62 | + |
| 63 | + if i > 0 { |
| 64 | + dst.put_slice(b" "); |
| 65 | + } |
| 66 | + let v = d.to_cabrillo(); |
| 67 | + dst.put_slice(v.as_bytes()); |
| 68 | + } |
| 69 | + |
| 70 | + dst.put_slice(b"\n"); |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Encoder<Item> for CabrilloEncoder { |
| 76 | + type Error = Error; |
| 77 | + |
| 78 | + fn encode( |
| 79 | + &mut self, item: Item, dst: &mut BytesMut, |
| 80 | + ) -> Result<(), Self::Error> { |
| 81 | + match item { |
| 82 | + Item::Record(r) => { |
| 83 | + if r.is_header() { |
| 84 | + self.encode_header(r, dst) |
| 85 | + } else if !self.started { |
| 86 | + Err(Error::MissingHeader) |
| 87 | + } else { |
| 88 | + self.encode_qso(r, dst) |
| 89 | + } |
| 90 | + } |
| 91 | + Item::Eof => { |
| 92 | + if !self.started { |
| 93 | + return Err(Error::MissingHeader); |
| 94 | + } |
| 95 | + dst.put_slice(b"END-OF-LOG:\n"); |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Sink for writing Cabrillo format records |
| 103 | +pub struct CabrilloSink<W> { |
| 104 | + inner: FramedWrite<W, CabrilloEncoder>, |
| 105 | +} |
| 106 | + |
| 107 | +impl<W> CabrilloSink<W> |
| 108 | +where |
| 109 | + W: AsyncWrite, |
| 110 | +{ |
| 111 | + /// Create a new CabrilloSink that writes the specified fields from |
| 112 | + /// each record in Cabrillo format. |
| 113 | + /// |
| 114 | + /// Header field names are output in uppercase. All values are output |
| 115 | + /// verbatim with no transformation, although the encoder does output |
| 116 | + /// typed data according to the format. |
| 117 | + /// |
| 118 | + /// ``` |
| 119 | + /// use adif::{CabrilloSink, Record}; |
| 120 | + /// use futures::SinkExt; |
| 121 | + /// |
| 122 | + /// # #[tokio::main(flavor = "current_thread")] |
| 123 | + /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 124 | + /// let mut buf = Vec::new(); |
| 125 | + /// let fields = vec!["freq", "mode", "time_on", "call"]; |
| 126 | + /// let mut sink = CabrilloSink::new(&mut buf, fields); |
| 127 | + /// |
| 128 | + /// let mut header = Record::new_header(); |
| 129 | + /// header.insert("contest", "ARRL-SS-CW")?; |
| 130 | + /// sink.send(header).await?; |
| 131 | + /// |
| 132 | + /// let mut qso = Record::new(); |
| 133 | + /// qso.insert("freq", "14000")?; |
| 134 | + /// qso.insert("mode", "CW")?; |
| 135 | + /// qso.insert("time_on", "CW")?; |
| 136 | + /// qso.insert("call", "W1AW")?; |
| 137 | + /// sink.send(qso).await?; |
| 138 | + /// |
| 139 | + /// sink.close().await?; |
| 140 | + /// # Ok(()) |
| 141 | + /// # } |
| 142 | + /// ``` |
| 143 | + pub fn new(w: W, fields: Vec<&str>) -> Self { |
| 144 | + let fields = fields.into_iter().map(|s| s.to_string()).collect(); |
| 145 | + Self { |
| 146 | + inner: FramedWrite::new(w, CabrilloEncoder::new(fields)), |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl<W> Sink<Record> for CabrilloSink<W> |
| 152 | +where |
| 153 | + W: AsyncWrite + Unpin, |
| 154 | +{ |
| 155 | + type Error = Error; |
| 156 | + |
| 157 | + fn poll_ready( |
| 158 | + mut self: Pin<&mut Self>, cx: &mut Context<'_>, |
| 159 | + ) -> Poll<Result<(), Error>> { |
| 160 | + Pin::new(&mut self.inner) |
| 161 | + .poll_ready(cx) |
| 162 | + .map_err(Error::from) |
| 163 | + } |
| 164 | + |
| 165 | + fn start_send(mut self: Pin<&mut Self>, r: Record) -> Result<(), Error> { |
| 166 | + Pin::new(&mut self.inner).start_send(Item::Record(r)) |
| 167 | + } |
| 168 | + |
| 169 | + fn poll_flush( |
| 170 | + mut self: Pin<&mut Self>, cx: &mut Context<'_>, |
| 171 | + ) -> Poll<Result<(), Error>> { |
| 172 | + Pin::new(&mut self.inner) |
| 173 | + .poll_flush(cx) |
| 174 | + .map_err(Error::from) |
| 175 | + } |
| 176 | + |
| 177 | + fn poll_close( |
| 178 | + mut self: Pin<&mut Self>, cx: &mut Context<'_>, |
| 179 | + ) -> Poll<Result<(), Error>> { |
| 180 | + Pin::new(&mut self.inner).start_send(Item::Eof)?; |
| 181 | + Pin::new(&mut self.inner) |
| 182 | + .poll_close(cx) |
| 183 | + .map_err(Error::from) |
| 184 | + } |
| 185 | +} |
0 commit comments