Skip to main content

celestial_pointing/commands/
fauto.rs

1use super::{Command, CommandOutput};
2use crate::error::{Error, Result};
3use crate::session::Session;
4
5pub struct Fauto;
6
7impl Command for Fauto {
8    fn name(&self) -> &str {
9        "FAUTO"
10    }
11    fn description(&self) -> &str {
12        "Auto-add harmonics up to Nth order"
13    }
14
15    fn execute(&self, session: &mut Session, args: &[&str]) -> Result<CommandOutput> {
16        if args.is_empty() {
17            return Err(Error::Parse("FAUTO requires a harmonic order".into()));
18        }
19        let order: usize = args[0]
20            .parse()
21            .map_err(|e| Error::Parse(format!("invalid order: {}", e)))?;
22        if order == 0 {
23            return Err(Error::Parse("harmonic order must be >= 1".into()));
24        }
25
26        let mut added = Vec::new();
27        for n in 1..=order {
28            let suffix = if n == 1 { String::new() } else { n.to_string() };
29            let names = [format!("HDSH{}", suffix), format!("HDCH{}", suffix)];
30            for name in &names {
31                if !session.model.term_names().contains(&name.as_str()) {
32                    session.model.add_term(name)?;
33                    added.push(name.clone());
34                }
35            }
36        }
37
38        if added.is_empty() {
39            Ok(CommandOutput::Text(format!(
40                "All harmonics up to order {} already in model",
41                order
42            )))
43        } else {
44            Ok(CommandOutput::Text(format!(
45                "Added {} harmonics: {}",
46                added.len(),
47                added.join(" ")
48            )))
49        }
50    }
51}