cli: make command file argument named and optional

This commit is contained in:
Mikolaj Wielgus 2024-07-16 15:45:42 +02:00
parent 2ebd9b411b
commit 87fd5d5d05
5 changed files with 46 additions and 18 deletions

View File

@ -47,7 +47,7 @@ simple THT diode bridge rectifier:
``` ```
cd tests/single_layer/data/tht_diode_bridge_rectifier/ cd tests/single_layer/data/tht_diode_bridge_rectifier/
topola tht_diode_bridge_rectifier.dsn tht_diode_bridge_rectifier.ses autoroute_all.cmd topola tht_diode_bridge_rectifier.dsn tht_diode_bridge_rectifier.ses
``` ```
##### Viewing the results ##### Viewing the results
@ -72,7 +72,7 @@ then as follows:
``` ```
cd tests/single_layer/data/tht_diode_bridge_rectifier/ cd tests/single_layer/data/tht_diode_bridge_rectifier/
cargo run --features cli -- tht_diode_bridge_rectifier.dsn tht_diode_bridge_rectifier.ses autoroute_all.cmd cargo run --features cli -- tht_diode_bridge_rectifier.dsn tht_diode_bridge_rectifier.ses
``` ```
Viewing the results is obviously the same. Viewing the results is obviously the same.

View File

@ -25,7 +25,7 @@ impl History {
} }
} }
pub fn destructure(self) -> (Vec<Command>, Vec<Command>) { pub fn destruct(self) -> (Vec<Command>, Vec<Command>) {
(self.done, self.undone) (self.done, self.undone)
} }

View File

@ -243,7 +243,7 @@ impl<M: AccessMesadata> Invoker<M> {
#[debug_requires(self.ongoing_command.is_none())] #[debug_requires(self.ongoing_command.is_none())]
pub fn replay(&mut self, history: History) { pub fn replay(&mut self, history: History) {
let (done, undone) = history.destructure(); let (done, undone) = history.destruct();
for command in done { for command in done {
self.execute(command); self.execute(command);

View File

@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
board::{mesadata::AccessMesadata, Board}, board::{mesadata::AccessMesadata, Board},
drawing::graph::{GetLayer, MakePrimitive}, drawing::graph::{GetLayer, MakePrimitive},
geometry::compound::ManageCompounds, geometry::{compound::ManageCompounds, GenericNode},
graph::{GenericIndex, GetPetgraphIndex}, graph::{GenericIndex, GetPetgraphIndex},
layout::{poly::PolyWeight, CompoundWeight, NodeIndex}, layout::{poly::PolyWeight, CompoundWeight, NodeIndex},
}; };
@ -22,12 +22,26 @@ pub struct Selection {
} }
impl Selection { impl Selection {
pub fn new() -> Selection { pub fn new() -> Self {
Self { Self {
selectors: HashSet::new(), selectors: HashSet::new(),
} }
} }
pub fn new_select_all(board: &Board<impl AccessMesadata>) -> Self {
let mut this = Self::new();
for node in board.layout().drawing().primitive_nodes() {
if let Some(selector) = this.node_selector(board, GenericNode::Primitive(node)) {
if !this.contains_node(board, GenericNode::Primitive(node)) {
this.select(board, selector);
}
}
}
this
}
pub fn toggle_at_node(&mut self, board: &Board<impl AccessMesadata>, node: NodeIndex) { pub fn toggle_at_node(&mut self, board: &Board<impl AccessMesadata>, node: NodeIndex) {
let Some(selector) = self.node_selector(board, node) else { let Some(selector) = self.node_selector(board, node) else {
return; return;
@ -40,11 +54,11 @@ impl Selection {
} }
} }
fn select(&mut self, board: &Board<impl AccessMesadata>, selector: Selector) { fn select(&mut self, _board: &Board<impl AccessMesadata>, selector: Selector) {
self.selectors.insert(selector); self.selectors.insert(selector);
} }
fn deselect(&mut self, board: &Board<impl AccessMesadata>, selector: &Selector) { fn deselect(&mut self, _board: &Board<impl AccessMesadata>, selector: &Selector) {
self.selectors.remove(selector); self.selectors.remove(selector);
} }

View File

@ -2,13 +2,21 @@ use clap::{Error, Parser};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::io::BufReader; use std::io::BufReader;
use std::path::PathBuf;
use topola::autorouter::history::History;
use topola::autorouter::invoker::Command;
use topola::autorouter::invoker::Invoker;
use topola::autorouter::selection::Selection;
use topola::autorouter::Autorouter;
use topola::specctra::design::SpecctraDesign;
#[derive(Parser, Debug, Default)] #[derive(Parser, Debug, Default)]
#[command(about, version)] #[command(about, version)]
struct Cli { struct Cli {
input: std::path::PathBuf, input: PathBuf,
output: std::path::PathBuf, output: PathBuf,
history: std::path::PathBuf, #[arg(short, long, value_name = "FILE")]
commands: Option<PathBuf>,
} }
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
@ -17,15 +25,21 @@ fn main() -> Result<(), std::io::Error> {
let design_file = File::open(args.input)?; let design_file = File::open(args.input)?;
let mut design_bufread = BufReader::new(design_file); let mut design_bufread = BufReader::new(design_file);
let design = topola::specctra::design::SpecctraDesign::load(design_bufread).unwrap(); let design = SpecctraDesign::load(design_bufread).unwrap();
let board = design.make_board(); let board = design.make_board();
let history_file = File::open(args.history)?; let history = if let Some(commands_filename) = args.commands {
let mut history_bufread = BufReader::new(history_file); let command_file = File::open(commands_filename)?;
let mut invoker = topola::autorouter::invoker::Invoker::new( let commands_bufread = BufReader::new(command_file);
topola::autorouter::Autorouter::new(board).unwrap(), serde_json::from_reader(commands_bufread)?
); } else {
invoker.replay(serde_json::from_reader(history_bufread).unwrap()); let mut history = History::new();
history.do_(Command::Autoroute(Selection::new_select_all(&board)));
history
};
let mut invoker = Invoker::new(Autorouter::new(board).unwrap());
invoker.replay(history);
let mut file = File::create(args.output).unwrap(); let mut file = File::create(args.output).unwrap();
design.write_ses(invoker.autorouter().board(), &mut file); design.write_ses(invoker.autorouter().board(), &mut file);