我有恢复某种类型的<代码>的功能。 备选案文<Month>
pub fn month_from_index(index: u64) -> Option<Month>
它基本上从11-12岁恢复过来。
基本类型定义
<>Datetimeutils
/// Converts an integer in the range 1-12 into the corresponding `Month` enum.
/// Values outside the 1-12 range are converted to `None`.
pub fn month_from_index(index: u64) -> Option<Month> {
match index {
1 => Some(Month::January),
2 => Some(Month::February),
3 => Some(Month::March),
4 => Some(Month::April),
5 => Some(Month::May),
6 => Some(Month::June),
7 => Some(Month::July),
8 => Some(Month::August),
9 => Some(Month::September),
10 => Some(Month::October),
11 => Some(Month::November),
12 => Some(Month::December),
_ => None,
}
}
我本月的工作时间为u64,它将履行以下职能:<代码>月_从_index。
let month = Utc::now().month() as u64;
Now... i want to get the days of the month dynamically. This days_in_month
function only takes an enum, which makes it tricky.
<>main>。
use chrono::prelude::*;
use datetimeutils::{day_string, days_in_month, month_from_index, month_string, Month};
use slint::VecModel;
use std::rc::Rc;
slint::include_modules!();
fn generate_month(year: u64, month: Option<Month>) -> u64 {
days_in_month(year, month) /// if i replace month with Month::January it works. But i want this to be be accessed via index.
}
fn load_calendar(boxes: Rc<VecModel<NewBox>>, year: u64) {
let current_date = chrono::Utc::now();
let month = Utc::now().month() as u64;
let current_month = month_from_index(month);
let generate_days = generate_month(year, current_month);
for new_box in 0..generate_days {
boxes.insert(
new_box as usize,
NewBox {
visible: true,
day: new_box as i32 + 1,
},
)
}
}
fn main() -> Result<(), slint::PlatformError> {
let ui = AppWindow::new()?;
let boxes = Rc::new(slint::VecModel::<NewBox>::from(Vec::new()));
let new_boxes = boxes.clone();
load_calendar(new_boxes, 2024);
ui.set_boxes(boxes.into());
ui.run()
}
--> src/main.rs:8:25
|
8 | days_in_month(year, month)
| ------------- ^^^^^ expected `Month`, found `Option<Month>`
|
|
| arguments to this function are incorrect
| = note: expected enum `datetimeutils::Month` found enum `std::option::Option<datetimeutils::Month>`