Skip to main content

nmea_kit/ais/messages/
static_b.rs

1//! AIS static data report — Type 24 (Class B).
2
3use crate::ais::armor::{extract_string, extract_u32};
4
5/// AIS Static Data Report — Type 24 (Class B).
6///
7/// Type 24 comes in two parts:
8/// - Part A: vessel name
9/// - Part B: callsign + ship type
10#[non_exhaustive]
11#[derive(Debug, Clone, PartialEq)]
12pub enum StaticDataReport {
13    PartA {
14        mmsi: u32,
15        vessel_name: String,
16    },
17    PartB {
18        mmsi: u32,
19        callsign: String,
20        ship_type: u8,
21    },
22}
23
24impl StaticDataReport {
25    /// Decode a Type 24 static data report.
26    pub fn decode(bits: &[u8]) -> Option<Self> {
27        if bits.len() < 160 {
28            return None;
29        }
30
31        let mmsi = extract_u32(bits, 8, 30)?;
32        let part_number = extract_u32(bits, 38, 2)?;
33
34        match part_number {
35            0 => {
36                // Part A: vessel name
37                let vessel_name = extract_string(bits, 40, 20)?;
38                Some(Self::PartA { mmsi, vessel_name })
39            }
40            1 => {
41                // Part B: callsign + ship type
42                if bits.len() < 168 {
43                    return None;
44                }
45                let callsign = extract_string(bits, 40, 7)?;
46                let ship_type = extract_u32(bits, 82, 8)? as u8;
47                Some(Self::PartB {
48                    mmsi,
49                    callsign,
50                    ship_type,
51                })
52            }
53            _ => None,
54        }
55    }
56}