Skip to content
Snippets Groups Projects
message.rs 1.93 KiB
Newer Older
use core::fmt::Display;

Stephen D's avatar
Stephen D committed
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum MessageDirection {
    From,
    To,
}

#[derive(Copy, Clone)]
pub struct Message {
Stephen D's avatar
Stephen D committed
    pub content: &'static str,
    pub direction: MessageDirection,
}

// A single column in the message list
Stephen D's avatar
Stephen D committed
// TODO rename this to MessageIdentity? May be more straightforward
pub struct MessageGroup {
    callsign: &'static str,
    ssid: u8,
}

impl MessageGroup {
    pub fn iter(&self) -> MessageGroupIter {
        MessageGroupIter::new(self)
    }
}

impl Display for MessageGroup {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Stephen D's avatar
Stephen D committed
        // TODO will need to somehow look up the actual contact name
        write!(f, "{}-{}", self.callsign, self.ssid)
    }
}

impl Default for MessageGroup {
    // Stubbing for now
    fn default() -> Self {
        Self {
            callsign: "VA3QAK",
            ssid: 3,
        }
    }
}

pub struct MessageGroupIter<'a> {
    group: &'a MessageGroup,
    i: usize,
}

impl<'a> MessageGroupIter<'a> {
    pub fn new(group: &'a MessageGroup) -> Self {
        Self { group, i: 0 }
    }
}

impl<'a> Iterator for MessageGroupIter<'a> {
    type Item = Message;

    fn next(&mut self) -> Option<Self::Item> {
        // Stubbing for now
        let stubbed = [
            Message {
                content: "Hello",
                direction: MessageDirection::From,
            },
            Message {
                content: "Hi",
                direction: MessageDirection::To,
            },
            Message {
                content: "How are you?",
                direction: MessageDirection::From,
            },
            Message {
                content: "I'm good",
                direction: MessageDirection::To,
            },
        ];

Stephen D's avatar
Stephen D committed
        if self.i >= stubbed.len() {
            None
        } else {
            let out = stubbed[self.i];

            self.i += 1;

            Some(out)
        }
    }
}