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    rate: String,
34
35    /// Information block size (k)
36    #[arg(long)]
37    block_size: usize,
38
39    /// Performs girth calculation
40    #[arg(long)]
41    girth: bool,
42}
43
44impl Args {
45    fn code(&self) -> Result<AR4JACode> {
46        let rate = match &*self.rate {
47            "1/2" => AR4JARate::R1_2,
48            "2/3" => AR4JARate::R2_3,
49            "4/5" => AR4JARate::R4_5,
50            r => return Err(format!("Invalid code rate {}", r)),
51        };
52        let info_size = match self.block_size {
53            1024 => AR4JAInfoSize::K1024,
54            4096 => AR4JAInfoSize::K4096,
55            16384 => AR4JAInfoSize::K16384,
56            s => return Err(format!("Invalid information block size k = {}", s)),
57        };
58        Ok(AR4JACode::new(rate, info_size))
59    }
60}
61
62impl Run for Args {
63    fn run(&self) -> std::result::Result<(), Box<dyn std::error::Error>> {
64        let h = self.code()?.h();
65        if self.girth {
66            if let Some(g) = h.girth() {
67                println!("Code girth = {}", g);
68            } else {
69                println!("Code girth is infinite");
70            }
71        } else {
72            print!("{}", h.alist());
73        }
74        Ok(())
75    }
76}