syncweb-core 0.1.1-alpha.7

delay-tolerant web surfing
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::{
    collections::BTreeMap,
    fmt::{Display, Formatter},
    time::{SystemTime, UNIX_EPOCH},
};

use serde::{Deserialize, Serialize};

use crate::{Result, SyncwebError};

/// TOML representation of the global and per-folder synchronization schedule.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ScheduleConfig {
    #[serde(default)]
    pub active_hours: String,
    #[serde(default)]
    pub bandwidth: Vec<BandwidthWindowConfig>,
    #[serde(default)]
    pub folders: BTreeMap<String, ScheduleFolderConfig>,
}

/// A bandwidth window as represented in the configuration file.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct BandwidthWindowConfig {
    pub hours: String,
    #[serde(default = "unlimited_rate")]
    pub max_upload: String,
    #[serde(default = "unlimited_rate")]
    pub max_download: String,
}

impl Default for BandwidthWindowConfig {
    fn default() -> Self {
        Self {
            hours: "00:00-24:00".to_owned(),
            max_upload: unlimited_rate(),
            max_download: unlimited_rate(),
        }
    }
}

impl BandwidthWindowConfig {
    #[must_use]
    pub fn new(hours: impl Into<String>, max_upload: impl Into<String>, max_download: impl Into<String>) -> Self {
        Self {
            hours: hours.into(),
            max_upload: max_upload.into(),
            max_download: max_download.into(),
        }
    }
}

/// Per-folder schedule settings. Missing fields inherit the global schedule.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ScheduleFolderConfig {
    #[serde(default)]
    pub active_hours: Option<String>,
    #[serde(default)]
    pub max_upload: Option<String>,
    #[serde(default)]
    pub max_download: Option<String>,
}

fn unlimited_rate() -> String {
    "0".to_owned()
}

/// A half-open time interval in minutes after midnight.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct TimeWindow {
    start: u16,
    end: u16,
}

impl TimeWindow {
    /// Parse a `HH:MM-HH:MM` interval.
    ///
    /// An empty interval means that the schedule is active all day.
    ///
    /// # Errors
    ///
    /// Returns an error if the interval is not in `HH:MM-HH:MM` form.
    pub fn parse(value: &str) -> Result<Option<Self>> {
        let trimmed = value.trim();
        if trimmed.is_empty() {
            return Ok(None);
        }
        let (start, end) = trimmed.split_once('-').ok_or_else(|| {
            SyncwebError::InvalidConfig(format!("invalid schedule hours {trimmed:?}; expected HH:MM-HH:MM"))
        })?;
        Ok(Some(Self {
            start: parse_clock(start)?,
            end: parse_clock(end)?,
        }))
    }

    #[must_use]
    pub const fn contains(self, minute: u16) -> bool {
        if self.start == self.end {
            return false;
        }
        if self.start < self.end {
            minute >= self.start && minute < self.end
        } else {
            minute >= self.start || minute < self.end
        }
    }

    #[must_use]
    pub const fn start(self) -> u16 {
        self.start
    }

    #[must_use]
    pub const fn end(self) -> u16 {
        self.end
    }
}

impl Display for TimeWindow {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "{:02}:{:02}-{:02}:{:02}",
            self.start.div_euclid(60),
            self.start.rem_euclid(60),
            self.end.div_euclid(60),
            self.end.rem_euclid(60)
        )
    }
}

fn parse_clock(value: &str) -> Result<u16> {
    let (hour_text, minute_text) = value
        .trim()
        .split_once(':')
        .ok_or_else(|| SyncwebError::InvalidConfig(format!("invalid schedule time {value:?}; expected HH:MM")))?;
    let hour_value = hour_text
        .parse::<u16>()
        .map_err(|error| SyncwebError::InvalidConfig(format!("invalid schedule hour {value:?}: {error}")))?;
    let minute_value = minute_text
        .parse::<u16>()
        .map_err(|error| SyncwebError::InvalidConfig(format!("invalid schedule minute {value:?}: {error}")))?;
    if hour_value > 24 || minute_value > 59 || (hour_value == 24 && minute_value != 0) {
        return Err(SyncwebError::InvalidConfig(format!(
            "schedule time {value:?} is outside the 24-hour clock"
        )));
    }
    Ok(hour_value.saturating_mul(60).saturating_add(minute_value))
}

/// Parse a byte-per-second rate such as `5MB/s`, `500KB/s`, `2M`, `500K`, or `0`.
///
/// The scale letter (`k`/`m`/`g`/`t`) is case-insensitive; the unit letter
/// distinguishes bytes (`B`) from bits (`b`). So `5KB` is 5000 bytes while
/// `5Kb` is 5000 bits (625 bytes), and bare scales such as `2M` mean bytes.
///
/// # Errors
///
/// Returns an error if the rate is not a whole number of bytes per second
/// with a supported suffix.
pub fn parse_rate(value: &str) -> Result<Option<u64>> {
    let normalized = value.trim();
    if normalized.is_empty() || normalized == "0" || normalized.eq_ignore_ascii_case("unlimited") {
        return Ok(None);
    }
    let rate_number = normalized
        .strip_suffix("/s")
        .or_else(|| normalized.strip_suffix("/S"))
        .unwrap_or(normalized)
        .trim();
    let (numeric_part, multiplier, is_bytes) = split_rate_suffix(rate_number);
    let amount = numeric_part.trim().parse::<u64>().map_err(|error| {
        SyncwebError::InvalidConfig(format!(
            "invalid bandwidth rate {value:?}; expected a whole number with an optional B/s, KB/s, or Kb/s suffix: {error}"
        ))
    })?;
    let scaled = amount.checked_mul(multiplier);
    if is_bytes {
        scaled
            .map(Some)
            .ok_or_else(|| SyncwebError::InvalidConfig(format!("bandwidth rate {value:?} is too large")))
    } else {
        let bits =
            scaled.ok_or_else(|| SyncwebError::InvalidConfig(format!("bandwidth rate {value:?} is too large")))?;
        if bits.rem_euclid(8) != 0 {
            return Err(SyncwebError::InvalidConfig(format!(
                "bandwidth rate {value:?} is not a whole number of bytes per second"
            )));
        }
        Ok(Some(bits.checked_div(8).unwrap_or_default()))
    }
}

/// Split a rate into its numeric prefix, scale multiplier, and whether the
/// unit is bytes (true) or bits (false).
#[must_use]
fn split_rate_suffix(value: &str) -> (&str, u64, bool) {
    let lower = value.to_ascii_lowercase();
    for (suffix, multiplier) in [
        ("tibit", 1_u64 << 40),
        ("gibit", 1_u64 << 30),
        ("mibit", 1_u64 << 20),
        ("kibit", 1_u64 << 10),
        ("tib", 1_u64 << 40),
        ("gib", 1_u64 << 30),
        ("mib", 1_u64 << 20),
        ("kib", 1_u64 << 10),
        ("tbit", 1_000_000_000_000_u64),
        ("gbit", 1_000_000_000_u64),
        ("mbit", 1_000_000_u64),
        ("kbit", 1_000_u64),
        ("tb", 1_000_000_000_000_u64),
        ("gb", 1_000_000_000_u64),
        ("mb", 1_000_000_u64),
        ("kb", 1_000_u64),
        ("bit", 1_u64),
    ] {
        if let Some(rest) = lower.strip_suffix(suffix) {
            let is_bytes = if suffix.ends_with('t') {
                false
            } else {
                matches!(value.as_bytes().last(), Some(b'B'))
            };
            return (&value[..rest.len()], multiplier, is_bytes);
        }
    }
    let is_bytes = !matches!(value.as_bytes().last(), Some(b'b'));
    let (cut, multiplier) = if lower.ends_with('t') {
        (value.len().saturating_sub(1), 1_000_000_000_000_u64)
    } else if lower.ends_with('g') {
        (value.len().saturating_sub(1), 1_000_000_000_u64)
    } else if lower.ends_with('m') {
        (value.len().saturating_sub(1), 1_000_000_u64)
    } else if lower.ends_with('k') {
        (value.len().saturating_sub(1), 1_000_u64)
    } else if lower.ends_with('b') {
        (value.len().saturating_sub(1), 1_u64)
    } else {
        (value.len(), 1_u64)
    };
    (&value[..cut], multiplier, is_bytes)
}

/// A parsed bandwidth window.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct BandwidthWindow {
    pub hours: TimeWindow,
    pub max_upload: Option<u64>,
    pub max_download: Option<u64>,
}

/// Parsed schedule settings for one scope.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct Schedule {
    pub active_hours: Option<TimeWindow>,
    pub bandwidth: Vec<BandwidthWindow>,
}

/// Effective limits at a point in time.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct BandwidthLimits {
    pub max_upload: Option<u64>,
    pub max_download: Option<u64>,
}

#[derive(Clone, Debug)]
struct FolderSchedule {
    active_hours: Option<TimeWindow>,
    active_hours_override: bool,
    max_upload: Option<u64>,
    max_upload_override: bool,
    max_download: Option<u64>,
    max_download_override: bool,
}

/// Evaluates global and per-folder schedules.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ScheduleManager {
    global: Schedule,
    folders: BTreeMap<String, FolderSchedule>,
}

impl ScheduleManager {
    /// Parse a schedule configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if a schedule interval or bandwidth rate is invalid.
    pub fn from_config(config: &ScheduleConfig) -> Result<Self> {
        let global = parse_schedule(&config.active_hours, &config.bandwidth)?;
        let folders = config
            .folders
            .iter()
            .map(|(name, folder)| {
                let (active_hours_override, active_hours) = folder
                    .active_hours
                    .as_deref()
                    .map(TimeWindow::parse)
                    .transpose()?
                    .map_or((false, None), |hours| (true, hours));
                let (max_upload_override, max_upload) = folder
                    .max_upload
                    .as_deref()
                    .map(parse_rate)
                    .transpose()?
                    .map_or((false, None), |rate| (true, rate));
                let (max_download_override, max_download) = folder
                    .max_download
                    .as_deref()
                    .map(parse_rate)
                    .transpose()?
                    .map_or((false, None), |rate| (true, rate));
                Ok((
                    name.clone(),
                    FolderSchedule {
                        active_hours,
                        active_hours_override,
                        max_upload,
                        max_upload_override,
                        max_download,
                        max_download_override,
                    },
                ))
            })
            .collect::<Result<BTreeMap<_, _>>>()?;
        Ok(Self { global, folders })
    }

    /// Return whether a folder is active at the current local wall-clock time.
    #[must_use]
    pub fn is_active(&self, folder: Option<&str>) -> bool {
        self.is_active_at(folder, current_minute())
    }

    /// Return whether a folder is active at a supplied minute after midnight.
    #[must_use]
    pub fn is_active_at(&self, folder: Option<&str>, minute: u16) -> bool {
        let active_hours = folder
            .and_then(|name| self.folders.get(name))
            .filter(|schedule| schedule.active_hours_override)
            .and_then(|schedule| schedule.active_hours)
            .or(self.global.active_hours);
        active_hours.is_none_or(|window| window.contains(minute))
    }

    /// Return the limits for a folder at the current local wall-clock time.
    #[must_use]
    pub fn current_limits(&self, folder: Option<&str>) -> BandwidthLimits {
        self.current_limits_at(folder, current_minute())
    }

    /// Return the limits for a folder at a supplied minute after midnight.
    #[must_use]
    pub fn current_limits_at(&self, folder: Option<&str>, minute: u16) -> BandwidthLimits {
        let mut limits = self
            .global
            .bandwidth
            .iter()
            .find(|window| window.hours.contains(minute))
            .map_or_else(BandwidthLimits::default, |window| BandwidthLimits {
                max_upload: window.max_upload,
                max_download: window.max_download,
            });
        if let Some(folder_schedule) = folder.and_then(|name| self.folders.get(name)) {
            if folder_schedule.max_upload_override {
                limits.max_upload = folder_schedule.max_upload;
            }
            if folder_schedule.max_download_override {
                limits.max_download = folder_schedule.max_download;
            }
        }
        limits
    }

    /// Return the next minute at which the selected schedule becomes active.
    #[must_use]
    pub fn next_active_window_start_at(&self, folder: Option<&str>, minute: u16) -> Option<u16> {
        let window = folder
            .and_then(|name| self.folders.get(name))
            .filter(|schedule| schedule.active_hours_override)
            .and_then(|schedule| schedule.active_hours)
            .or(self.global.active_hours)?;
        if window.contains(minute) {
            return Some(minute);
        }
        for offset in 1_u16..=1_440 {
            let candidate = minute.saturating_add(offset) % 1_440;
            if window.contains(candidate) {
                return Some(candidate);
            }
        }
        None
    }

    #[must_use]
    pub const fn global(&self) -> &Schedule {
        &self.global
    }
}

fn parse_schedule(active_hours: &str, bandwidth_configs: &[BandwidthWindowConfig]) -> Result<Schedule> {
    let bandwidth = bandwidth_configs
        .iter()
        .map(|window| {
            let hours = TimeWindow::parse(&window.hours)?.ok_or_else(|| {
                SyncwebError::InvalidConfig("bandwidth windows must specify a non-empty hours interval".to_owned())
            })?;
            Ok(BandwidthWindow {
                hours,
                max_upload: parse_rate(&window.max_upload)?,
                max_download: parse_rate(&window.max_download)?,
            })
        })
        .collect::<Result<Vec<_>>>()?;
    Ok(Schedule {
        active_hours: TimeWindow::parse(active_hours)?,
        bandwidth,
    })
}

pub(crate) fn current_minute() -> u16 {
    let seconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_secs());
    let day_seconds = seconds % 86_400;
    u16::try_from(day_seconds.div_euclid(60)).unwrap_or(0)
}

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

    #[test]
    fn parse_rate_accepts_bare_scale_suffixes() -> anyhow::Result<()> {
        ensure!(parse_rate("500K")? == Some(500_000), "500K should be 500_000");
        ensure!(parse_rate("2M")? == Some(2_000_000), "2M should be 2_000_000");
        ensure!(parse_rate("1G")? == Some(1_000_000_000), "1G should be 1_000_000_000");
        ensure!(parse_rate("2m/s")? == Some(2_000_000), "2m/s should be 2_000_000");
        ensure!(
            parse_rate("2T")? == Some(2_000_000_000_000),
            "2T should be 2_000_000_000_000"
        );
        ensure!(parse_rate("0")?.is_none(), "0 should be unlimited");
        ensure!(parse_rate("unlimited")?.is_none(), "unlimited should be unlimited");
        Ok(())
    }

    #[test]
    fn parse_rate_preserves_existing_suffixes() -> anyhow::Result<()> {
        ensure!(parse_rate("5MB/s")? == Some(5_000_000), "5MB/s should be 5_000_000");
        ensure!(parse_rate("2MiB")? == Some(2_097_152), "2MiB should be 2_097_152");
        ensure!(parse_rate("3KiB")? == Some(3_072), "3KiB should be 3_072");
        ensure!(parse_rate("250B")? == Some(250), "250B should be 250");
        ensure!(parse_rate("4096")? == Some(4096), "plain number should be bytes");
        Ok(())
    }

    #[test]
    fn parse_rate_distinguishes_bytes_from_bits() -> anyhow::Result<()> {
        ensure!(parse_rate("5KB")? == Some(5000), "5KB should be 5000 bytes");
        ensure!(parse_rate("5Kb")? == Some(625), "5Kb should be 5000 bits = 625 bytes");
        ensure!(parse_rate("1MB")? == Some(1_000_000), "1MB should be 1_000_000 bytes");
        ensure!(
            parse_rate("1Mb")? == Some(125_000),
            "1Mb should be 1_000_000 bits = 125_000 bytes"
        );
        ensure!(parse_rate("2Mbit")? == Some(250_000), "2Mbit should be 250_000 bytes");
        ensure!(parse_rate("5kb/s")? == Some(625), "5kb/s should be 625 bytes");
        ensure!(parse_rate("8b")? == Some(1), "8b should be 1 byte");
        ensure!(parse_rate("1b").is_err(), "1 bit should not be a whole byte");
        ensure!(
            parse_rate("2TB")? == Some(2_000_000_000_000),
            "2TB should be 2 TB bytes"
        );
        ensure!(
            parse_rate("2Tb")? == Some(250_000_000_000),
            "2Tb should be 2 TB bits = 250 GB bytes"
        );
        ensure!(
            parse_rate("1TiB")? == Some(1_099_511_627_776),
            "1TiB should be 2^40 bytes"
        );
        Ok(())
    }

    #[test]
    fn parse_rate_rejects_invalid_values() -> anyhow::Result<()> {
        ensure!(parse_rate("banana").is_err(), "non-numeric rate should fail");
        ensure!(parse_rate("-2M").is_err(), "negative rate should fail");
        ensure!(parse_rate("M").is_err(), "bare suffix should fail");
        Ok(())
    }
}