privmsg.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. use std::io;
  2. use darkfi::serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};
  3. #[derive(SerialEncodable, SerialDecodable, Clone)]
  4. pub struct PrivMsgEvent {
  5. pub nick: String,
  6. pub msg: String,
  7. pub target: String,
  8. }
  9. #[derive(Clone)]
  10. pub enum EventAction {
  11. PrivMsg(PrivMsgEvent),
  12. }
  13. impl std::string::ToString for PrivMsgEvent {
  14. fn to_string(&self) -> String {
  15. format!(":{}!anon@dark.fi PRIVMSG {} :{}\r\n", self.nick, self.target, self.msg)
  16. }
  17. }
  18. impl Encodable for EventAction {
  19. fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
  20. match self {
  21. Self::PrivMsg(event) => {
  22. let mut len = 0;
  23. len += 0u8.encode(&mut s)?;
  24. len += event.encode(s)?;
  25. Ok(len)
  26. }
  27. }
  28. }
  29. }
  30. impl Decodable for EventAction {
  31. fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
  32. let type_id = d.read_u8()?;
  33. match type_id {
  34. 0 => Ok(Self::PrivMsg(PrivMsgEvent::decode(d)?)),
  35. _ => Err(io::Error::new(io::ErrorKind::Other, "Bad type ID byte for Event")),
  36. }
  37. }
  38. }