Krolyxon
Commit 90075f3

Branches

Refs →

budctlcratesbudctl-coresrcstatus.rs

80 lines · 2.5 KB · Rust

1//! What a session reports, in device-neutral terms. Front ends print these, and `--json`
2//! serializes them as they are.
3
4use serde::{Serialize, Serializer};
5
6/// Bumped whenever the JSON shape changes incompatibly.
7pub const JSON_SCHEMA: u32 = 1;
8
9#[derive(Debug, Default, Serialize)]
10pub struct Status {
11 pub battery: Battery,
12 pub noise_control: Option<NoiseControl>,
13 /// Name of the current EQ preset.
14 pub eq: Option<String>,
15 pub bass_level: Option<i8>,
16 /// On/off settings, serialized as `{"game": false, …}`.
17 #[serde(serialize_with = "switches_as_map")]
18 pub switches: Vec<Switch>,
19 /// Audio codec of the link the buds report.
20 pub codec: Option<String>,
21 pub firmware: Option<String>,
22 /// Reads that failed. The rest of the status is still valid.
23 pub warnings: Vec<String>,
24}
25
26#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize)]
27pub struct Battery {
28 pub left: Option<Charge>,
29 pub right: Option<Charge>,
30 pub case: Option<Charge>,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
34pub struct Charge {
35 /// Percent.
36 pub level: u8,
37 pub charging: bool,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
41pub struct NoiseControl {
42 /// `off`, `transparency` or `anc`, or `unknown (…)` for a value the device module cannot name.
43 pub mode: String,
44 /// The noise-cancelling level, when the device reports one.
45 pub level: Option<String>,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
49pub struct Switch {
50 /// Stable name used on the command line and in JSON (`game`, `dual`…).
51 pub key: &'static str,
52 /// Human name (`Game mode`).
53 pub label: &'static str,
54 pub on: bool,
55}
56
57#[derive(Debug, Default, Serialize)]
58pub struct TouchControls {
59 pub gestures: Vec<Gesture>,
60 /// Noise-control modes a hold steps through, when the device has that setting.
61 pub hold_cycle: Option<Vec<String>>,
62 pub warnings: Vec<String>,
63}
64
65#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
66pub struct Gesture {
67 /// `left`, `right` or `both`.
68 pub side: String,
69 /// `double-tap`, `triple-tap`, `hold`…
70 pub gesture: String,
71 /// `play-pause`, `next`, `none`…
72 pub action: String,
73 /// Raw button group, for entries outside the main gestures (calls). `None` for the main group.
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub group: Option<u8>,
76}
77
78fn switches_as_map<S: Serializer>(switches: &[Switch], serializer: S) -> Result<S::Ok, S::Error> {
79 serializer.collect_map(switches.iter().map(|s| (s.key, s.on)))
80}