ldpc_toolbox/cli/
ccsds.rs

1//! CCSDS CLI subcommand
2//!
3//! This subcommand can be used to generate the AR4JA LDPC codes described in
4//! the CCSDS TM Synchronization and Channel Coding Blue Book standard. It will
5//! print the alist of the parity check matrix to `stdout` and optionally
6//! compute and print the girth of the Tanner graph. See [`crate::codes::ccsds`]
7//! for more information about the CCSDS LDPC codes.
8//!
9//! # Examples
10//! The r=1/2, k=1024 parity check matrix can be generated with
11//! ```shell
12//! $ ldpc-toolbox ccsds --rate 1/2 --block-size 1024
13//! ```
14//! Its girth is computed with
15//! ```shell
16//! $ ldpc-toolbox ccsds --rate 1/2 --block-size 1024 --girth
17//! Code girth = 6
18//! ```
19
20use crate::cli::*;
21use crate::codes::ccsds::{AR4JACode, AR4JAInfoSize, AR4JARate};
22use clap::Parser;
23
24type Error = String;
25type Result<T> = std::result::Result<T, Error>;
26
27/// CCSDS CLI arguments.
28#[derive(Debug, Parser)]
29#[command(about = "Generates the alist of CCSDS LDPCs")]
30pub struct Args {
31    /// Coding rate
32    #[arg(short, long)]
33    pub rate: String,
34    /// Information block size (k)
35    #[arg(long)]
36    pub block_size: usize,
37    /// Performs girth calculation
38    #[arg(long)]
39    pub girth: bool,
40}
41
42impl Args {
43    fn code(&self) -> Result<AR4JACode> {
44        let rate = match &*self.rate {
45            "1/2" => AR4JARate::R1_2,
46            "2/3" => AR4JARate::R2_3,
47            "4/5" => AR4JARate::R4_5,
48            r => return Err(format!("Invalid code rate {}", r)),
49        };
50        let info_size = match self.block_size {
51            1024 => AR4JAInfoSize::K1024,
52            4096 => AR4JAInfoSize::K4096,
53            16384 => AR4JAInfoSize::K16384,
54            s => return Err(format!("Invalid information block size k = {}", s)),
55        };
56        Ok(AR4JACode::new(rate, info_size))
57    }
58}
59
60impl Run for Args {
61    fn run(&self) -> std::result::Result<(), Box<dyn std::error::Error>> {
62        let h = self.code()?.h();
63        if self.girth {
64            if let Some(g) = h.girth() {
65                println!("Code girth = {}", g);
66            } else {
67                println!("Code girth is infinite");
68            }
69        } else {
70            print!("{}", h.alist());
71        }
72        Ok(())
73    }
74}