// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use serde::Serialize;
+use serde::ser::{Serialize, SerializeStruct, Serializer};
use std::collections::HashMap;
use std::net::UdpSocket;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const MESSAGE_SIZE: usize = 12;
const LOGFILENAME: &str = "log.csv";
-#[derive(Eq, Debug, Hash, PartialEq, Serialize)]
+#[derive(Eq, Debug, Hash, PartialEq)]
struct MessageKey {
epoch: u32,
device: u16,
key: MessageKey,
t: u64,
}
+impl Serialize for Message {
+ // https://github.com/BurntSushi/rust-csv/issues/155
+ // https://github.com/BurntSushi/rust-csv/issues/98
+ // https://github.com/BurntSushi/rust-csv/pull/223
+ // csv doesn't support #[serde(flatten)], so we implement this directly. :(
+ fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
+ let mut row = serializer.serialize_struct("Message", 4)?;
+ row.serialize_field("epoch", &self.key.epoch)?;
+ row.serialize_field("device", &self.key.device)?;
+ row.serialize_field("seq", &self.key.seq)?;
+ row.serialize_field("t", &self.t)?;
+ row.end()
+ }
+}
impl From<&[u8; MESSAGE_SIZE]> for Message {
fn from(value: &[u8; MESSAGE_SIZE]) -> Self {
continue;
}
let message = Message::try_from(filled_buf).expect("I can't count");
- log.serialize((&message.key, message.t))
- .expect("Couldn't write log");
+ log.serialize(&message).expect("Couldn't write log");
log.flush().expect("Couldn't flush log");
merge_message(&mut presses, message);
}