busylib 0.0.10

BUSY Bar Rust HTTP client
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Asset schemas, covering the display elements and audio playback that use them

use serde::{Deserialize, Serialize};

use crate::types::app_name::AppName;
use crate::types::asset_path::AssetPath;
use crate::types::color::Color;
use crate::types::element_id::ElementId;
use crate::types::invalid_value::InvalidValue;
use crate::types::opacity::Opacity;
use crate::types::priority::Priority;
use crate::types::stock_path::StockPath;
use crate::types::text::Text;
use crate::types::try_into_value::TryIntoValue;

/// Draw request for one application
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DisplayElements {
    /// Application ID for organizing assets
    pub application_name: AppName,
    /// Draw priority in the range [1, 100] inclusive. A draw request is accepted when its
    /// priority is greater than or equal to (>=) the priority of the currently running system
    /// app. Equal-priority requests from a different application_name override whatever is on
    /// screen. System app priority levels: stub/poweroff apps = 0 (always preemptable), any
    /// standard built-in app = 10, active BUSY/CUSTOM work session = 90. The draw API only
    /// accepts values 1–100; 0 is reserved for internal use.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<Priority>,
    /// Color to blink the status LED, in #RRGGBBAA format.  If not specified, the LED will not
    /// blink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub led_notification_color: Option<Color>,
    /// Array of elements to display
    pub elements: Vec<DisplayElement>,
}

impl DisplayElements {
    pub fn new(application_name: impl TryIntoValue<AppName>) -> Result<Self, InvalidValue> {
        Ok(Self {
            application_name: application_name.try_into_value()?,
            priority: None,
            led_notification_color: None,
            elements: Vec::new(),
        })
    }

    pub fn priority(mut self, priority: Priority) -> Self {
        self.priority = Some(priority);
        self
    }

    pub fn led_notification_color(mut self, color: Color) -> Self {
        self.led_notification_color = Some(color);
        self
    }

    pub fn element(mut self, element: DisplayElement) -> Self {
        self.elements.push(element);
        self
    }

    pub fn elements(mut self, elements: impl IntoIterator<Item = DisplayElement>) -> Self {
        self.elements.extend(elements);
        self
    }
}

/// Single element of a draw request
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DisplayElement {
    /// Unique identifier for the element
    pub id: ElementId,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub lifetime: Option<Lifetime>,
    /// X coordinate of selected anchor point relative to top-left of display
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x: Option<i16>,
    /// Y coordinate of selected anchor point relative to top-left of display
    #[serde(skip_serializing_if = "Option::is_none")]
    pub y: Option<i16>,
    /// Which display to show the element on (for dual-display devices)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display: Option<Screen>,
    /// Anchor point of element. Also use `x` and `y` to position element.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub align: Option<Align>,
    #[serde(flatten)]
    pub kind: ElementKind,
}

impl DisplayElement {
    pub fn builder(
        id: impl TryIntoValue<ElementId>,
    ) -> Result<DisplayElementBuilder, InvalidValue> {
        Ok(DisplayElementBuilder {
            id: id.try_into_value()?,
            lifetime: None,
            x: None,
            y: None,
            display: None,
            align: None,
        })
    }
}

/// Builder for a [`DisplayElement`]
#[derive(Debug, Clone)]
pub struct DisplayElementBuilder {
    id: ElementId,
    lifetime: Option<Lifetime>,
    x: Option<i16>,
    y: Option<i16>,
    display: Option<Screen>,
    align: Option<Align>,
}

impl DisplayElementBuilder {
    pub fn at(mut self, x: i16, y: i16) -> Self {
        self.x = Some(x);
        self.y = Some(y);
        self
    }

    pub fn align(mut self, align: Align) -> Self {
        self.align = Some(align);
        self
    }

    pub fn screen(mut self, screen: Screen) -> Self {
        self.display = Some(screen);
        self
    }

    pub fn timeout_secs(mut self, seconds: u32) -> Self {
        self.lifetime = Some(Lifetime::timeout_secs(seconds));
        self
    }

    pub fn display_until(mut self, unix_seconds: u64) -> Self {
        self.lifetime = Some(Lifetime::display_until(unix_seconds));
        self
    }

    pub fn text(self, text: TextElement) -> DisplayElement {
        self.finish(ElementKind::Text(text))
    }

    pub fn image(self, image: ImageElement) -> DisplayElement {
        self.finish(ElementKind::Image(image))
    }

    pub fn animation(self, animation: AnimationElement) -> DisplayElement {
        self.finish(ElementKind::Animation(animation))
    }

    pub fn countdown(self, countdown: CountdownElement) -> DisplayElement {
        self.finish(ElementKind::Countdown(countdown))
    }

    pub fn rectangle(self, rectangle: RectangleElement) -> DisplayElement {
        self.finish(ElementKind::Rectangle(rectangle))
    }

    fn finish(self, kind: ElementKind) -> DisplayElement {
        DisplayElement {
            id: self.id,
            lifetime: self.lifetime,
            x: self.x,
            y: self.y,
            display: self.display,
            align: self.align,
            kind,
        }
    }
}

/// How long an element stays on screen
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Lifetime {
    Timeout {
        /// Time in seconds the element should be displayed (0 for no timeout). Mutually
        /// exclusive with display_until.
        timeout: u32,
    },
    DisplayUntil {
        /// The element will be hidden when system time reaches the specified Unix timestamp
        /// (in seconds). Mutually exclusive with timeout.
        #[serde(with = "crate::serde_util::string_u64")]
        display_until: u64,
    },
}

impl Lifetime {
    pub fn timeout_secs(seconds: u32) -> Self {
        Self::Timeout { timeout: seconds }
    }

    pub fn display_until(unix_seconds: u64) -> Self {
        Self::DisplayUntil {
            display_until: unix_seconds,
        }
    }
}

/// Type of display element
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ElementKind {
    Text(TextElement),
    Image(ImageElement),
    Animation(AnimationElement),
    Countdown(CountdownElement),
    Rectangle(RectangleElement),
}

/// Text to draw
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TextElement {
    /// Text content to display (printable ASCII only; fonts are bitmap ASCII)
    pub text: Text,
    /// One of the available fonts to display the text in
    pub font: Font,
    /// Color to display the text in, in #RRGGBBAA format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<Color>,
    /// Width of the label
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u16>,
    /// Scroll rate in pixels per minute
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scroll_rate: Option<u32>,
    /// Delay in milliseconds before the scroll animation begins
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scroll_start_delay: Option<u32>,
    /// Pause duration in milliseconds between successive scroll cycles
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scroll_repeat_delay: Option<u32>,
}

impl TextElement {
    pub fn new(text: impl TryIntoValue<Text>, font: Font) -> Result<Self, InvalidValue> {
        Ok(Self {
            text: text.try_into_value()?,
            font,
            color: None,
            width: None,
            scroll_rate: None,
            scroll_start_delay: None,
            scroll_repeat_delay: None,
        })
    }

    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    pub fn width(mut self, pixels: u16) -> Self {
        self.width = Some(pixels);
        self
    }

    pub fn scroll_rate(mut self, pixels_per_minute: u32) -> Self {
        self.scroll_rate = Some(pixels_per_minute);
        self
    }

    pub fn scroll_start_delay_ms(mut self, milliseconds: u32) -> Self {
        self.scroll_start_delay = Some(milliseconds);
        self
    }

    pub fn scroll_repeat_delay_ms(mut self, milliseconds: u32) -> Self {
        self.scroll_repeat_delay = Some(milliseconds);
        self
    }
}

/// Where an image or animation is loaded from
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ImageSource {
    Asset { path: AssetPath },
    Stock { stock_path: StockPath },
}

impl ImageSource {
    pub fn asset(path: impl TryIntoValue<AssetPath>) -> Result<Self, InvalidValue> {
        Ok(Self::Asset {
            path: path.try_into_value()?,
        })
    }

    pub fn stock(stock_path: impl TryIntoValue<StockPath>) -> Result<Self, InvalidValue> {
        Ok(Self::Stock {
            stock_path: stock_path.try_into_value()?,
        })
    }
}

/// Image to draw
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageElement {
    #[serde(flatten)]
    pub source: ImageSource,
    /// Opacity of the image in percentage (0-100)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub opacity: Option<Opacity>,
}

impl ImageElement {
    pub fn new(source: ImageSource) -> Self {
        Self {
            source,
            opacity: None,
        }
    }

    pub fn asset(path: impl TryIntoValue<AssetPath>) -> Result<Self, InvalidValue> {
        Ok(Self::new(ImageSource::asset(path)?))
    }

    pub fn stock(stock_path: impl TryIntoValue<StockPath>) -> Result<Self, InvalidValue> {
        Ok(Self::new(ImageSource::stock(stock_path)?))
    }

    pub fn opacity(mut self, opacity: Opacity) -> Self {
        self.opacity = Some(opacity);
        self
    }
}

/// Animation to play
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnimationElement {
    #[serde(flatten)]
    pub source: ImageSource,
    /// Whether to loop the requested part of the animation
    #[serde(rename = "loop", skip_serializing_if = "Option::is_none")]
    pub repeat: Option<bool>,
    /// If the element has been created before and this flag is true, the previous range will
    /// finish before the requested one starts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub await_previous_end: Option<bool>,
    /// Name of the section to play back. Specifying "default" selects the entire animation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub section: Option<String>,
    /// Opacity of the animated image in percentage (0-100)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub opacity: Option<Opacity>,
}

impl AnimationElement {
    pub fn new(source: ImageSource) -> Self {
        Self {
            source,
            repeat: None,
            await_previous_end: None,
            section: None,
            opacity: None,
        }
    }

    pub fn asset(path: impl TryIntoValue<AssetPath>) -> Result<Self, InvalidValue> {
        Ok(Self::new(ImageSource::asset(path)?))
    }

    pub fn stock(stock_path: impl TryIntoValue<StockPath>) -> Result<Self, InvalidValue> {
        Ok(Self::new(ImageSource::stock(stock_path)?))
    }

    pub fn repeat(mut self, repeat: bool) -> Self {
        self.repeat = Some(repeat);
        self
    }

    pub fn await_previous_end(mut self, await_previous_end: bool) -> Self {
        self.await_previous_end = Some(await_previous_end);
        self
    }

    pub fn section(mut self, section: impl Into<String>) -> Self {
        self.section = Some(section.into());
        self
    }

    pub fn opacity(mut self, opacity: Opacity) -> Self {
        self.opacity = Some(opacity);
        self
    }
}

/// Countdown to draw
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CountdownElement {
    /// Seconds-based Unix UTC timestamp to count down or up to. Note: it's a number in a
    /// string.
    #[serde(with = "crate::serde_util::string_u64")]
    pub timestamp: u64,
    /// Color to display the text in, in #RRGGBBAA format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<Color>,
    /// Whether to count up or down
    pub direction: CountdownDirection,
    /// When to show the hours position
    pub show_hours: ShowHours,
}

impl CountdownElement {
    pub fn new(unix_seconds: u64, direction: CountdownDirection, show_hours: ShowHours) -> Self {
        Self {
            timestamp: unix_seconds,
            color: None,
            direction,
            show_hours,
        }
    }

    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }
}

/// Rectangle to draw
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RectangleElement {
    /// Width of the rectangle in pixels
    pub width: u16,
    /// Height of the rectangle in pixels
    pub height: u16,
    /// Corner radius of the rectangle in pixels (0 for sharp corners)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub radius: Option<u16>,
    /// Fill style of the rectangle
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill: Option<Fill>,
    /// Colors used for filling the rectangle. For solid fill, provide one color. For gradient
    /// fill, provide two colors.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill_colors: Option<Vec<Color>>,
    /// Width of the rectangle border in pixels (0 for no border)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub border_width: Option<u16>,
    /// Color of the rectangle border in #RRGGBBAA format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub border_color: Option<Color>,
}

impl RectangleElement {
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            width,
            height,
            radius: None,
            fill: None,
            fill_colors: None,
            border_width: None,
            border_color: None,
        }
    }

    pub fn radius(mut self, pixels: u16) -> Self {
        self.radius = Some(pixels);
        self
    }

    pub fn solid(mut self, color: Color) -> Self {
        self.fill = Some(Fill::Solid);
        self.fill_colors = Some(vec![color]);
        self
    }

    pub fn horizontal_gradient(mut self, from: Color, to: Color) -> Self {
        self.fill = Some(Fill::GradientH);
        self.fill_colors = Some(vec![from, to]);
        self
    }

    pub fn vertical_gradient(mut self, from: Color, to: Color) -> Self {
        self.fill = Some(Fill::GradientV);
        self.fill_colors = Some(vec![from, to]);
        self
    }

    pub fn no_fill(mut self) -> Self {
        self.fill = Some(Fill::None);
        self.fill_colors = None;
        self
    }

    pub fn border(mut self, width: u16, color: Color) -> Self {
        self.border_width = Some(width);
        self.border_color = Some(color);
        self
    }
}

/// Fonts text can be drawn in
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Font {
    Tiny,
    Small,
    Normal,
    Condensed,
    Bold,
    Large,
    ExtraLarge,
    Global,
}

/// Anchor points an element can be positioned by
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Align {
    TopLeft,
    TopMid,
    TopRight,
    MidLeft,
    Center,
    MidRight,
    BottomLeft,
    BottomMid,
    BottomRight,
}

/// Screens an element can be drawn on
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Screen {
    Front,
    Back,
}

impl Screen {
    pub fn index(self) -> u8 {
        match self {
            Screen::Front => 0,
            Screen::Back => 1,
        }
    }
}

/// Fill styles of a rectangle
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Fill {
    None,
    Solid,
    GradientH,
    GradientV,
}

/// Direction a countdown runs in
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CountdownDirection {
    TimeLeft,
    TimeSince,
}

/// When a countdown shows hours
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ShowHours {
    WhenNonZero,
    Always,
}

/// Request to play an audio file
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlayAudio {
    /// Application ID for organizing assets
    pub application_name: AppName,
    #[serde(flatten)]
    pub source: AudioSource,
}

impl PlayAudio {
    pub fn asset(
        application_name: impl TryIntoValue<AppName>,
        path: impl TryIntoValue<AssetPath>,
    ) -> Result<Self, InvalidValue> {
        Ok(Self {
            application_name: application_name.try_into_value()?,
            source: AudioSource::Asset {
                path: path.try_into_value()?,
            },
        })
    }

    pub fn stock(
        application_name: impl TryIntoValue<AppName>,
        stock_path: impl TryIntoValue<StockPath>,
    ) -> Result<Self, InvalidValue> {
        Ok(Self {
            application_name: application_name.try_into_value()?,
            source: AudioSource::Stock {
                stock_path: stock_path.try_into_value()?,
            },
        })
    }
}

/// Where an audio file is loaded from
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AudioSource {
    Asset {
        /// Path to audio file within app's assets directory
        path: AssetPath,
    },
    Stock {
        /// Stock audio file name
        stock_path: StockPath,
    },
}