ferrite_config/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Core types used throughout the configuration system.
//!
//! This module provides type-safe wrappers around primitive types to ensure
//! configuration values are valid and consistent.

use crate::error::{ConfigError, Result};
use serde::{Deserialize, Serialize};

/// Represents an RGBA color with validation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ColorRGBA {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}

impl ColorRGBA {
    /// Creates a new color from RGBA components
    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self {
            r,
            g,
            b,
            a,
        }
    }

    /// Creates a color from a hexadecimal string (e.g., "#FF0000FF")
    pub fn from_hex(hex: &str) -> Result<Self> {
        if !hex.starts_with('#') || hex.len() != 9 {
            return Err(ConfigError::ColorError(
                "Invalid hex color format. Expected '#RRGGBBAA'".to_string(),
            ));
        }

        let r = u8::from_str_radix(&hex[1..3], 16).map_err(|_| {
            ConfigError::ColorError("Invalid red component".to_string())
        })?;
        let g = u8::from_str_radix(&hex[3..5], 16).map_err(|_| {
            ConfigError::ColorError("Invalid green component".to_string())
        })?;
        let b = u8::from_str_radix(&hex[5..7], 16).map_err(|_| {
            ConfigError::ColorError("Invalid blue component".to_string())
        })?;
        let a = u8::from_str_radix(&hex[7..9], 16).map_err(|_| {
            ConfigError::ColorError("Invalid alpha component".to_string())
        })?;

        Ok(Self::new(r, g, b, a))
    }

    /// Converts the color to a hexadecimal string
    pub fn to_hex(&self) -> String {
        format!("#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
    }
}

/// Represents a 2D vector with validation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Vector2D {
    x: f64,
    y: f64,
}

impl Vector2D {
    /// Creates a new vector with validation
    pub fn new(x: f64, y: f64) -> Result<Self> {
        if x.is_finite() && y.is_finite() {
            Ok(Self {
                x,
                y,
            })
        } else {
            Err(ConfigError::ValidationError(
                "Vector components must be finite numbers".to_string(),
            ))
        }
    }

    /// Gets the x component
    pub fn x(&self) -> f64 {
        self.x
    }

    /// Gets the y component
    pub fn y(&self) -> f64 {
        self.y
    }
}

/// Represents the corner of a window or display area
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Corner {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

impl Default for Corner {
    fn default() -> Self {
        Corner::TopRight
    }
}

/// Represents a mouse button
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
}

impl Default for MouseButton {
    fn default() -> Self {
        MouseButton::Left
    }
}

/// Re-export eframe types for consistency
pub use eframe::egui::{Color32, Key};

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

    #[test]
    fn test_color_conversion() {
        let color = ColorRGBA::new(255, 128, 0, 255);
        assert_eq!(color.to_hex(), "#FF8000FF");

        let parsed = ColorRGBA::from_hex("#FF8000FF").unwrap();
        assert_eq!(color, parsed);
    }

    #[test]
    fn test_vector_validation() {
        assert!(Vector2D::new(1.0, 2.0).is_ok());
        assert!(Vector2D::new(f64::INFINITY, 2.0).is_err());
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(from = "&str", into = "String")]
pub struct SerializableKey(Key);

impl From<&str> for SerializableKey {
    fn from(s: &str) -> Self {
        // Implement key string parsing
        Self(match s {
            "Equal" => Key::Equals,
            "Plus" => Key::Plus,
            "Minus" => Key::Minus,
            "W" => Key::W,
            "S" => Key::S,
            "F" => Key::F,
            "Num0" => Key::Num0,
            _ => Key::Equals, // Default
        })
    }
}

impl From<SerializableKey> for String {
    fn from(key: SerializableKey) -> Self {
        match key.0 {
            Key::Equals => "Equal",
            Key::Plus => "Plus",
            Key::Minus => "Minus",
            Key::W => "W",
            Key::S => "S",
            Key::F => "F",
            Key::Num0 => "Num0",
            _ => "Equal",
        }
        .to_string()
    }
}