Skip to content

Commit 0e05748

Browse files
committed
Size cleanups
1 parent 6be74cc commit 0e05748

3 files changed

Lines changed: 46 additions & 113 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ targets = ["i686-unknown-linux-musl"]
1313

1414
[dependencies]
1515
# Async runtime (poller, agent loop, cron, heartbeat)
16-
tokio = { version = "1.41", features = ["full"] }
16+
tokio = { version = "1.41", features = ["rt-multi-thread", "macros", "sync", "time", "fs", "io-util", "process", "net"] }
1717
# SQLite persistence (bundled C library; works on i686-musl without host toolchain issues)
1818
rusqlite = { version = "0.38", features = ["bundled"] }
1919
# Config and API types (config.toml)
@@ -24,7 +24,7 @@ reqwest = { version = "0.13", default-features = false, features = ["json", "rus
2424
regex = "1.11"
2525
serde_json = "1.0"
2626
# Date for today + recent daily notes (YYYYMMDD)
27-
time = { version = "0.3", default-features = false, features = ["formatting", "std"] }
27+
2828
# IANA timezone support for local-time system prompt line (DST-aware)
2929
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
3030
chrono-tz = { version = "0.10", default-features = false }
@@ -36,10 +36,11 @@ tokio = { version = "1.41", features = ["test-util"] }
3636
serde_json = "1.0"
3737

3838
[profile.release]
39+
opt-level = "z"
3940
lto = true
4041
codegen-units = 1
4142
strip = true
4243
panic = "abort"
4344

4445
[profile.release.package."*"]
45-
opt-level = 3
46+
opt-level = "z"

src/tools/cron.rs

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::{Arc, RwLock};
77

88
use serde::{Deserialize, Serialize};
99
use serde_json::Value;
10-
use time::{Date, Month, OffsetDateTime, Time, Weekday};
10+
use chrono::{DateTime, Datelike, NaiveDate, TimeZone, Timelike, Utc};
1111

1212
use crate::tools::context::ToolCtx;
1313
use crate::tools::registry::{BoxFuture, Tool};
@@ -172,72 +172,48 @@ pub fn parse_cron_expr(expr: &str) -> Result<CronExpr, CronError> {
172172
})
173173
}
174174

175-
fn month_to_u8(m: Month) -> u8 {
176-
use Month::*;
177-
match m {
178-
January => 1,
179-
February => 2,
180-
March => 3,
181-
April => 4,
182-
May => 5,
183-
June => 6,
184-
July => 7,
185-
August => 8,
186-
September => 9,
187-
October => 10,
188-
November => 11,
189-
December => 12,
190-
}
191-
}
192-
193-
fn weekday_to_cron(w: Weekday) -> u8 {
194-
use Weekday::*;
195-
match w {
196-
Monday => 1,
197-
Tuesday => 2,
198-
Wednesday => 3,
199-
Thursday => 4,
200-
Friday => 5,
201-
Saturday => 6,
202-
Sunday => 0,
203-
}
204-
}
205-
206175
const LIMIT_YEARS: i32 = 4;
207176

208177
pub fn next_match(expr: &CronExpr, after_unix: u64) -> Option<u64> {
209178
let start_secs = (after_unix / 60 + 1) * 60;
210179
let start_secs = start_secs.min(i64::MAX as u64) as i64;
211-
let mut dt = match OffsetDateTime::from_unix_timestamp(start_secs) {
212-
Ok(d) => d,
213-
Err(_) => return None,
180+
let mut dt = match DateTime::from_timestamp(start_secs, 0) {
181+
Some(d) => d,
182+
None => return None,
214183
};
215184
let limit = dt.year() + LIMIT_YEARS;
216185

217186
while dt.year() <= limit {
218-
let month_u8 = month_to_u8(dt.month());
187+
let month_u8 = dt.month() as u8;
219188
if !expr.months.contains(&month_u8) {
220189
dt = next_matching_month(dt, expr)?;
221190
continue;
222191
}
223192
let dom = dt.day() as u8;
224-
let dow = weekday_to_cron(dt.weekday());
193+
let dow = dt.weekday().num_days_from_sunday() as u8;
225194
if !expr.doms.contains(&dom) || !expr.dows.contains(&dow) {
226195
dt = dt
227-
.replace_date(dt.date().next_day()?)
228-
.replace_time(Time::MIDNIGHT);
196+
.date_naive()
197+
.succ_opt()?
198+
.and_hms_opt(0, 0, 0)?
199+
.and_utc();
229200
continue;
230201
}
231202
let hour = dt.hour() as u8;
232203
if !expr.hours.contains(&hour) {
233204
match expr.hours.iter().find(|&&h| h >= hour) {
234205
Some(&h) => {
235-
dt = dt.replace_time(Time::from_hms(h, 0, 0).unwrap_or(Time::MIDNIGHT));
206+
dt = dt
207+
.date_naive()
208+
.and_hms_opt(h as u32, 0, 0)?
209+
.and_utc();
236210
}
237211
None => {
238212
dt = dt
239-
.replace_date(dt.date().next_day()?)
240-
.replace_time(Time::MIDNIGHT);
213+
.date_naive()
214+
.succ_opt()?
215+
.and_hms_opt(0, 0, 0)?
216+
.and_utc();
241217
}
242218
}
243219
continue;
@@ -246,43 +222,32 @@ pub fn next_match(expr: &CronExpr, after_unix: u64) -> Option<u64> {
246222
if !expr.minutes.contains(&minute) {
247223
match expr.minutes.iter().find(|&&m| m >= minute) {
248224
Some(&m) => {
249-
dt = dt.replace_time(Time::from_hms(hour, m, 0).unwrap_or(Time::MIDNIGHT));
225+
dt = dt
226+
.date_naive()
227+
.and_hms_opt(hour as u32, m as u32, 0)?
228+
.and_utc();
250229
}
251230
None => {
252231
let (next_date, next_hour) = next_hour_in_expr(dt, expr);
253-
let t = Time::from_hms(next_hour, expr.minutes[0], 0).unwrap_or(Time::MIDNIGHT);
254-
dt = next_date.with_time(t).assume_utc();
232+
dt = next_date
233+
.and_hms_opt(next_hour as u32, expr.minutes[0] as u32, 0)?
234+
.and_utc();
255235
}
256236
}
257237
continue;
258238
}
259-
return Some(dt.unix_timestamp() as u64);
239+
return Some(dt.timestamp() as u64);
260240
}
261241
None
262242
}
263243

264-
fn next_matching_month(dt: OffsetDateTime, expr: &CronExpr) -> Option<OffsetDateTime> {
244+
fn next_matching_month(dt: DateTime<Utc>, expr: &CronExpr) -> Option<DateTime<Utc>> {
265245
let mut y = dt.year();
266-
let mut m = month_to_u8(dt.month());
246+
let mut m = dt.month() as u8;
267247
for _ in 0..24 {
268248
if expr.months.contains(&m) {
269-
let month = match m {
270-
1 => Month::January,
271-
2 => Month::February,
272-
3 => Month::March,
273-
4 => Month::April,
274-
5 => Month::May,
275-
6 => Month::June,
276-
7 => Month::July,
277-
8 => Month::August,
278-
9 => Month::September,
279-
10 => Month::October,
280-
11 => Month::November,
281-
12 => Month::December,
282-
_ => return None,
283-
};
284-
let date = Date::from_calendar_date(y, month, 1).ok()?;
285-
return Some(date.midnight().assume_utc());
249+
let date = NaiveDate::from_ymd_opt(y, m as u32, 1)?;
250+
return Some(date.and_hms_opt(0, 0, 0)?.and_utc());
286251
}
287252
m += 1;
288253
if m > 12 {
@@ -293,14 +258,14 @@ fn next_matching_month(dt: OffsetDateTime, expr: &CronExpr) -> Option<OffsetDate
293258
None
294259
}
295260

296-
fn next_hour_in_expr(dt: OffsetDateTime, expr: &CronExpr) -> (Date, u8) {
297-
let mut date = dt.date();
261+
fn next_hour_in_expr(dt: DateTime<Utc>, expr: &CronExpr) -> (NaiveDate, u8) {
262+
let mut date = dt.date_naive();
298263
let mut hour = dt.hour() as u8;
299264
loop {
300265
if let Some(&h) = expr.hours.iter().find(|&&h| h > hour) {
301266
return (date, h);
302267
}
303-
date = match date.next_day() {
268+
date = match date.succ_opt() {
304269
Some(d) => d,
305270
None => return (date, 23),
306271
};

src/workspace.rs

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fs;
44
use std::path::{Path, PathBuf};
55

6-
use time::{Date, Month, OffsetDateTime};
6+
use chrono::{Datelike, NaiveDate, Utc};
77

88
const MEMORY_SUMMARY_LEN: usize = 4000;
99
const DAILY_NOTE_SUMMARY_LEN: usize = 2000;
@@ -13,8 +13,8 @@ pub const RECENT_DAILY_DAYS: u32 = 3;
1313
/// Current date in UTC as "YYYYMMDD" for daily note paths and memory context.
1414
#[inline]
1515
pub fn today_yyyymmdd() -> String {
16-
let d = OffsetDateTime::now_utc().date();
17-
format!("{:04}{:02}{:02}", d.year(), month_n(d.month()), d.day())
16+
let d = Utc::now().date_naive();
17+
format!("{:04}{:02}{:02}", d.year(), d.month(), d.day())
1818
}
1919

2020
/// Path to the skills directory under the workspace: `workspace/skills`.
@@ -108,48 +108,15 @@ pub fn brain_db_path(workspace: &Path) -> PathBuf {
108108
icrab_dir(workspace).join("brain.db")
109109
}
110110

111-
fn month_n(m: Month) -> u8 {
112-
use Month::*;
113-
match m {
114-
January => 1,
115-
February => 2,
116-
March => 3,
117-
April => 4,
118-
May => 5,
119-
June => 6,
120-
July => 7,
121-
August => 8,
122-
September => 9,
123-
October => 10,
124-
November => 11,
125-
December => 12,
126-
}
127-
}
128-
129111
/// Parse "YYYYMMDD" into Date. Returns None if invalid.
130-
fn parse_yyyymmdd(s: &str) -> Option<Date> {
112+
fn parse_yyyymmdd(s: &str) -> Option<NaiveDate> {
131113
if s.len() != 8 {
132114
return None;
133115
}
134116
let y: i32 = s[0..4].parse().ok()?;
135-
let m: u8 = s[4..6].parse().ok()?;
136-
let d: u8 = s[6..8].parse().ok()?;
137-
let month = match m {
138-
1 => Month::January,
139-
2 => Month::February,
140-
3 => Month::March,
141-
4 => Month::April,
142-
5 => Month::May,
143-
6 => Month::June,
144-
7 => Month::July,
145-
8 => Month::August,
146-
9 => Month::September,
147-
10 => Month::October,
148-
11 => Month::November,
149-
12 => Month::December,
150-
_ => return None,
151-
};
152-
Date::from_calendar_date(y, month, d).ok()
117+
let m: u32 = s[4..6].parse().ok()?;
118+
let d: u32 = s[6..8].parse().ok()?;
119+
NaiveDate::from_ymd_opt(y, m, d)
153120
}
154121

155122
/// Dates from today going back (today_yyyymmdd, yesterday, …). At most `days` entries.
@@ -160,10 +127,10 @@ fn recent_daily_dates(today_yyyymmdd: &str, days: u32) -> Option<Vec<String>> {
160127
out.push(format!(
161128
"{:04}{:02}{:02}",
162129
date.year(),
163-
month_n(date.month()),
130+
date.month(),
164131
date.day()
165132
));
166-
date = date.previous_day()?;
133+
date = date.pred_opt()?;
167134
}
168135
Some(out)
169136
}

0 commit comments

Comments
 (0)