fix: crashed when no timebar was requested because we unwrapped for the creation of Ui Data #26
cargo devel CI / cargo CI (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-09-05 11:35:51 +02:00
parent 04c570243c
commit 140123c124
3 changed files with 5 additions and 5 deletions

View File

@ -222,7 +222,7 @@ impl Clock {
) -> anyhow::Result<()> {
let tick_rate = std::time::Duration::from_millis(100);
let mut last_tick = Instant::now();
let mut uidata: Data = Data::new(self.timebar_len().unwrap());
let mut uidata: Data = Data::new(self.timebar_len());
self.setup()?;
loop {
let raw_time = chrono::Local::now().round_subsecs(0);

View File

@ -19,14 +19,14 @@ pub struct Data {
ftime: [String; 2],
timebar_ratio: [Option<f64>; 2],
timebar_type: TimeBarLength,
timebar_type: Option<TimeBarLength>,
started_at: DateTime<Local>,
idx: usize,
}
impl Data {
pub fn new(timebar_type: TimeBarLength) -> Self {
pub fn new(timebar_type: Option<TimeBarLength>) -> Self {
let mut this = Self {
now: [DateTime::default(); 2],
fdate: [String::new(), String::new()],
@ -91,7 +91,7 @@ impl Data {
#[inline]
#[allow(clippy::missing_const_for_fn)] // no it's not const
pub fn timebar_ratio(&self) -> Option<f64> {
if self.timebar_type == TimeBarLength::Timer {
if self.timebar_type.is_some() && self.timebar_type.unwrap() == TimeBarLength::Timer {
return Some(0.0);
}
self.timebar_ratio[self.idx]

View File

@ -90,7 +90,7 @@ fn mock_tests() {
info!("0s=0.0");
}
{
let mut data = Data::new(clock::timebar::TimeBarLength::Day);
let mut data = Data::new(None);
let now = Local::now();
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
assert_eq!(data.timebar_ratio(), Some(0.1));