regy 0.1.0

Private-by-default desktop agent for the Regy web interface
use crate::domain::errors::{AgentError, AgentResult, ErrorCode};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TerminalSize {
    pub cols: u16,
    pub rows: u16,
}

impl TerminalSize {
    pub fn new(cols: u16, rows: u16) -> AgentResult<Self> {
        if !(20..=300).contains(&cols) || !(5..=100).contains(&rows) {
            return Err(AgentError::new(
                ErrorCode::ResizeFailed,
                "invalid terminal size",
            ));
        }
        Ok(Self { cols, rows })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn accepts_bounds() {
        let size = TerminalSize::new(20, 5).unwrap();
        assert_eq!(size.cols, 20);
        assert_eq!(size.rows, 5);
    }

    #[test]
    fn rejects_too_small_or_large_values() {
        assert_eq!(
            TerminalSize::new(19, 5).unwrap_err().code(),
            ErrorCode::ResizeFailed
        );
        assert_eq!(
            TerminalSize::new(20, 4).unwrap_err().code(),
            ErrorCode::ResizeFailed
        );
        assert_eq!(
            TerminalSize::new(301, 5).unwrap_err().code(),
            ErrorCode::ResizeFailed
        );
        assert_eq!(
            TerminalSize::new(20, 101).unwrap_err().code(),
            ErrorCode::ResizeFailed
        );
    }
}