kenwood_thd75/protocol/
user.rs

1//! User/extra commands: US, TY, 0E.
2//!
3//! Provides parsing of responses for user settings and extra commands.
4//! Serialization is handled inline by the main dispatcher.
5
6use crate::error::ProtocolError;
7
8use super::Response;
9
10/// Parse a `u8` from a string field.
11fn parse_u8_field(s: &str, cmd: &str, field: &str) -> Result<u8, ProtocolError> {
12    s.parse::<u8>().map_err(|_| ProtocolError::FieldParse {
13        command: cmd.to_owned(),
14        field: field.to_owned(),
15        detail: format!("invalid u8: {s:?}"),
16    })
17}
18
19/// Parse a user/extra command response from mnemonic and payload.
20///
21/// Handles US (user settings), TY (radio type/region), and 0E (MCP status).
22/// Returns `None` if the mnemonic is not handled by this module.
23pub(crate) fn parse_user(mnemonic: &str, payload: &str) -> Option<Result<Response, ProtocolError>> {
24    match mnemonic {
25        "US" => Some(
26            parse_u8_field(payload, "US", "value").map(|value| Response::UserSettings { value }),
27        ),
28        "TY" => Some(parse_ty(payload)),
29        "0E" => Some(Ok(Response::McpStatus {
30            value: payload.to_owned(),
31        })),
32        _ => None,
33    }
34}
35
36/// Parse a TY (radio type/region) response.
37///
38/// Format: `region,variant` (e.g., `K,2`).
39fn parse_ty(payload: &str) -> Result<Response, ProtocolError> {
40    let (region_str, variant_str) =
41        payload
42            .split_once(',')
43            .ok_or_else(|| ProtocolError::FieldParse {
44                command: "TY".to_owned(),
45                field: "all".to_owned(),
46                detail: format!("expected region,variant, got {payload:?}"),
47            })?;
48
49    let variant = parse_u8_field(variant_str, "TY", "variant")?;
50
51    Ok(Response::RadioType {
52        region: region_str.to_owned(),
53        variant,
54    })
55}