aboutsummaryrefslogtreecommitdiff
path: root/tools/router-rs/src/io.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/router-rs/src/io.rs')
-rw-r--r--tools/router-rs/src/io.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/tools/router-rs/src/io.rs b/tools/router-rs/src/io.rs
new file mode 100644
index 0000000..be51147
--- /dev/null
+++ b/tools/router-rs/src/io.rs
@@ -0,0 +1,145 @@
+//! JSON formats shared with the Python tools: board.json from tools/router/dump_board.py
+//! in, routes.json for tools/router/write_board.py and plot_routes.py out.
+
+use serde::{Deserialize, Serialize};
+use std::collections::{BTreeMap, HashMap};
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct PadJson {
+ #[serde(rename = "ref")]
+ pub refdes: String,
+ pub num: String,
+ pub net: String,
+ pub x: f64,
+ pub y: f64,
+ pub drill: f64,
+ #[serde(default)]
+ pub npth: bool,
+ #[serde(rename = "F")]
+ pub f: Option<Vec<[f64; 2]>>,
+ #[serde(rename = "B")]
+ pub b: Option<Vec<[f64; 2]>>,
+}
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct FpJson {
+ pub x: f64,
+ pub y: f64,
+ pub rot: f64,
+ #[serde(default)]
+ pub value: String,
+}
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct ClassJson {
+ pub track: f64,
+ pub via: f64,
+}
+
+/// Footprint copper that is not a pad, on "F" or "B".
+#[derive(Deserialize, Serialize, Clone)]
+pub struct FpCopperJson {
+ #[serde(rename = "ref")]
+ pub refdes: String,
+ pub layer: String,
+ pub poly: Vec<[f64; 2]>,
+}
+
+/// A copper pour: net, layer ("F.Cu" / "B.Cu") and outline.
+#[derive(Deserialize, Serialize, Clone)]
+pub struct ZoneJson {
+ pub net: String,
+ pub layer: String,
+ pub pts: Vec<[f64; 2]>,
+}
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct TrackJson {
+ pub net: String,
+ pub layer: String,
+ pub x1: f64,
+ pub y1: f64,
+ pub x2: f64,
+ pub y2: f64,
+ pub w: f64,
+}
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct ViaJson {
+ pub net: String,
+ pub x: f64,
+ pub y: f64,
+ pub d: f64,
+ #[serde(default)]
+ pub drill: f64,
+}
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct BoardJson {
+ pub bounds: [f64; 4],
+ pub pads: Vec<PadJson>,
+ #[serde(default)]
+ pub fps: BTreeMap<String, FpJson>,
+ #[serde(default)]
+ pub netclass: HashMap<String, String>,
+ #[serde(default)]
+ pub classes: HashMap<String, ClassJson>,
+ #[serde(default)]
+ pub fpcopper: Vec<FpCopperJson>,
+ /// copper pours (not rule areas): net, layer, outline
+ #[serde(default)]
+ pub zones: Vec<ZoneJson>,
+ #[serde(default)]
+ pub keepouts: Vec<Vec<[f64; 2]>>,
+ /// Owning footprint of each keepout ("" for board rule areas); absent in old dumps.
+ #[serde(default)]
+ pub keepout_refs: Vec<String>,
+ #[serde(default)]
+ pub tracks: Vec<TrackJson>,
+ #[serde(default)]
+ pub vias: Vec<ViaJson>,
+}
+
+impl BoardJson {
+ pub fn load(path: &str) -> anyhow::Result<BoardJson> {
+ use anyhow::Context;
+ let s = std::fs::read_to_string(path).with_context(|| path.to_string())?;
+ serde_json::from_str(&s).with_context(|| path.to_string())
+ }
+}
+
+#[derive(Serialize)]
+pub struct RoutesJson {
+ pub segs: Vec<TrackJson>,
+ pub vias: Vec<ViaJson>,
+ pub failed: Vec<Vec<String>>,
+ pub log: Vec<serde_json::Value>,
+}
+
+#[derive(Deserialize)]
+pub struct PadNet {
+ #[serde(rename = "ref")]
+ pub refdes: String,
+ pub net: String,
+}
+
+/// Fixed copper: a board dump (tracks, vias, fps, pads) or just tracks and vias.
+#[derive(Deserialize)]
+pub struct CopperJson {
+ #[serde(default)]
+ pub tracks: Vec<TrackJson>,
+ #[serde(default)]
+ pub vias: Vec<ViaJson>,
+ #[serde(default)]
+ pub fps: BTreeMap<String, FpJson>,
+ #[serde(default)]
+ pub pads: Vec<PadNet>,
+}
+
+impl CopperJson {
+ pub fn load(path: &str) -> anyhow::Result<CopperJson> {
+ use anyhow::Context;
+ let s = std::fs::read_to_string(path).with_context(|| path.to_string())?;
+ serde_json::from_str(&s).with_context(|| path.to_string())
+ }
+}