raytracer/src/display.rs

45 lines
1.3 KiB
Rust

use core::fmt::Display;
use core::fmt::Formatter;
#[derive(Debug)]
pub struct DisplayInfo {
pub width: u32,
pub height: u32,
pub aspect_ratio: f64,
pub fps: u32,
}
impl Display for DisplayInfo {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
write!(
f,
"DisplayInfo: {{ width: {}, height: {}, aspect_ratio: {}, fps: {} }}",
self.width, self.height, self.aspect_ratio, self.fps
)
}
}
pub fn parse_display_string(display_string: &str) -> Result<DisplayInfo, String> {
let mut iter = display_string.split(&['x', '@']);
let width = iter.next().ok_or("Display string must contain width")?;
let height = iter.next().ok_or("Display string must contain height")?;
let fps = iter.next().ok_or("Display string must contain fps")?;
let width = width
.parse::<u32>()
.map_err(|e| format!("Could not parse width: {}", e))?;
let height = height
.parse::<u32>()
.map_err(|e| format!("Could not parse height: {}", e))?;
let fps = fps
.parse::<u32>()
.map_err(|e| format!("Could not parse fps: {}", e))?;
let dis_info = DisplayInfo {
width,
height,
aspect_ratio: (width / height) as f64,
fps,
};
Ok(dis_info)
}