Skip to main content

celestial_pointing/commands/
fix.rs

1use super::{Command, CommandOutput};
2use crate::error::{Error, Result};
3use crate::session::Session;
4
5pub struct Fix;
6pub struct Unfix;
7
8impl Command for Fix {
9    fn name(&self) -> &str {
10        "FIX"
11    }
12    fn description(&self) -> &str {
13        "Fix terms at current values during fit"
14    }
15
16    fn execute(&self, session: &mut Session, args: &[&str]) -> Result<CommandOutput> {
17        if args.is_empty() {
18            return Err(Error::Parse("FIX requires term names or ALL".into()));
19        }
20        if args[0].eq_ignore_ascii_case("ALL") {
21            session.model.fix_all();
22            return Ok(CommandOutput::Text(format!(
23                "Fixed all {} terms",
24                session.model.term_count()
25            )));
26        }
27        let mut fixed = Vec::new();
28        for name in args {
29            let upper = name.to_uppercase();
30            if session.model.fix_term(&upper) {
31                fixed.push(upper);
32            } else {
33                return Err(Error::Parse(format!("term {} not in model", name)));
34            }
35        }
36        Ok(CommandOutput::Text(format!("Fixed: {}", fixed.join(" "))))
37    }
38}
39
40impl Command for Unfix {
41    fn name(&self) -> &str {
42        "UNFIX"
43    }
44    fn description(&self) -> &str {
45        "Allow terms to be fitted"
46    }
47
48    fn execute(&self, session: &mut Session, args: &[&str]) -> Result<CommandOutput> {
49        if args.is_empty() {
50            return Err(Error::Parse("UNFIX requires term names or ALL".into()));
51        }
52        if args[0].eq_ignore_ascii_case("ALL") {
53            session.model.unfix_all();
54            return Ok(CommandOutput::Text(format!(
55                "Unfixed all {} terms",
56                session.model.term_count()
57            )));
58        }
59        let mut unfixed = Vec::new();
60        for name in args {
61            let upper = name.to_uppercase();
62            if session.model.unfix_term(&upper) {
63                unfixed.push(upper);
64            } else {
65                return Err(Error::Parse(format!("term {} not in model", name)));
66            }
67        }
68        Ok(CommandOutput::Text(format!(
69            "Unfixed: {}",
70            unfixed.join(" ")
71        )))
72    }
73}