PrintAllControlScripts/
main.rs

1// -!- rust -!- //////////////////////////////////////////////////////////////
2//
3//  System        : 
4//  Module        : 
5//  Object Name   : $RCSfile$
6//  Revision      : $Revision$
7//  Date          : $Date$
8//  Author        : $Author$
9//  Created By    : Robert Heller
10//  Created       : 2025-09-28 13:09:45
11//  Last Modified : <250928.1415>
12//
13//  Description	
14//
15//  Notes
16//
17//  History
18//	
19/////////////////////////////////////////////////////////////////////////////
20//    Copyright (C) 2025  Robert Heller D/B/A Deepwoods Software
21//			51 Locke Hill Road
22//			Wendell, MA 01379-9728
23//
24//    This program is free software; you can redistribute it and/or modify
25//    it under the terms of the GNU General Public License as published by
26//    the Free Software Foundation; either version 2 of the License, or
27//    (at your option) any later version.
28//
29//    This program is distributed in the hope that it will be useful,
30//    but WITHOUT ANY WARRANTY; without even the implied warranty of
31//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32//    GNU General Public License for more details.
33//
34//    You should have received a copy of the GNU General Public License
35//    along with this program; if not, write to the Free Software
36//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37// 
38//
39//////////////////////////////////////////////////////////////////////////////
40
41//! This example program prints out all of the layout control elements (Blocks,
42//! SwitchMotors, Signals, Sensors, and Controls.
43//!
44//! ## Synopsis
45//!
46//! PrintAllControlScripts FILE \[options\]
47//!
48//! ## Options
49//! - -b, --blocks print Blocks
50//! - -m, --switchmotors print Switchmotors
51//! - -s, --signals print Signals
52//! - -c, --controls print Controls
53//! - -x, --sensors print Sensors
54//! - -a, --all print all (this is the default)
55//! - -h, --help print help
56//! ## Parameters
57//!
58//! The layout file to load.
59//!
60//! ## Description
61//!
62//! This program loads in a layout file and prints the selected control 
63//! elements.
64//!
65//! ## Author
66//! Robert Heller \<heller@deepsoft.com\>
67
68
69extern crate getopts;
70use getopts::Options;
71use std::env;
72use xtrakcad_parser::Layout;
73
74fn print_usage(program: &str, opts: Options) {
75    let brief = format!("Usage: {} FILE [options]", program);
76    print!("{}", opts.usage(&brief));
77}
78
79
80fn main() {
81    let args: Vec<String> = env::args().collect();
82    let program = args[0].clone();
83
84    let mut opts = Options::new();
85    opts.optflag("b", "blocks", "print Blocks");
86    opts.optflag("m", "switchmotors", "print Switchmotors");
87    opts.optflag("s", "signals", "print Signals ");
88    opts.optflag("c", "controls", "print Controls");
89    opts.optflag("x", "sensors", "print Sensors");
90    opts.optflag("a", "all", "print all (this is the default)");
91    opts.optflag("h", "help", "print this help menu");
92    let matches = match opts.parse(&args[1..]) {
93        Ok(m) => { m },
94        Err(f) => { panic!("{}", f.to_string());  },
95    };
96    if matches.opt_present("h") {
97        print_usage(&program, opts);
98        return ();
99    };
100    let mut blocks: bool = false;
101    let mut switchmotors: bool = false;
102    let mut signals: bool = false;
103    let mut controls: bool = false;
104    let mut sensors: bool = false;
105    let mut all: bool = true;
106    if matches.opt_present("b") {
107        blocks = true;
108        all = false;
109    }
110    if matches.opt_present("m") {
111        switchmotors = true;
112        all = false;
113    }
114    if matches.opt_present("s") {
115        signals = true; 
116        all = false;
117    }
118    if matches.opt_present("c") {
119        controls = true; 
120        all = false;
121    }
122    if matches.opt_present("x") {
123        sensors = true;
124        all = false;
125    }
126    if matches.opt_present("a") {
127        all = true;
128    }
129    let layoutfile = if !matches.free.is_empty() {
130        matches.free[0].clone()
131    } else {
132        print_usage(&program, opts);
133        panic!("Missing layout file!");
134    };
135    let layout = match Layout::new(layoutfile) {
136        Ok(l) => { l },
137        Err(message) => { panic!("{}",message.to_string()); },
138    };
139    println!("{}",layout);
140    if blocks || all {
141        for (index, block) in layout.BlockIter() {
142            println!("Block # {}:",*index);
143            println!("\tName: {}",block.Name());
144            println!("\tScript: {}",block.Script());
145            println!("\tTrack list: {}",block.Tracklist());
146        }
147    }
148    if switchmotors || all {
149        for (index, switchmotor) in layout.SwitchMotorIter() {
150            println!("SwitchMotor # {}:",*index);
151            println!("\tName: {}",switchmotor.Name());
152            let turnout = layout.Turnout(switchmotor.Turnout()).expect("Should not happen");
153            let tablist = turnout.Tablist();
154            let tabelts: Vec<&str> = tablist.split('\t').collect();
155            println!("\tTurnout #{} ({})",switchmotor.Turnout(),tabelts[1]);
156            println!("\tNormal: {}",switchmotor.Normal());
157            println!("\tReverse: {}",switchmotor.Reverse());
158            println!("\tPoint Sense: {}",switchmotor.Pointsense());
159        }
160    }
161    if signals || all {
162        for (index, signal) in layout.SignalIter() {
163            println!("Signal # {}:",*index);
164            println!("\tName: {}",signal.Name());
165            println!("\tHeads: {}",signal.Numheads());
166            let aspects = signal.Aspectlist();
167            if aspects.len() > 0 {
168                println!("\tAspects:");
169                for ia in 0..aspects.len() {
170                    let aspect = &aspects[ia];
171                    println!("\t\t{}: {}",aspect.Name(), aspect.Script());
172                }
173            }
174        }
175    }
176    if sensors || all {
177        for (index, sensor) in layout.SensorIter() {
178            println!("Sensor # {}",*index);
179            println!("\tName: {}",sensor.Name());
180            println!("\tScript: {}",sensor.Script());
181        }
182    }
183    if controls  || all {
184        for (index, control) in layout.ControlIter() {
185            println!("Control # {}",*index);
186            println!("\tName: {}",control.Name());
187            println!("\tOn Script: {}",control.OnScript());
188            println!("\tOff Script: {}",control.OffScript());
189        }
190    }
191}
192