use serde::{Deserialize, Serialize};
use std::array;
pub const COLUMNS: usize = 15;
pub const PERMUTS: usize = 7;
pub const WIRES: [usize; COLUMNS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
#[derive(PartialEq, Default, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[cfg_attr(feature = "wasm_types", wasm_bindgen::prelude::wasm_bindgen)]
pub struct Wire {
pub row: usize,
pub col: usize,
}
impl Wire {
pub fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
pub fn for_row(row: usize) -> [Self; PERMUTS] {
GateWires::new(row)
}
}
pub type GateWires = [Wire; PERMUTS];
pub trait Wirable: Sized {
fn new(row: usize) -> Self;
fn wire(self, col: usize, to: Wire) -> Self;
}
impl Wirable for GateWires {
fn new(row: usize) -> Self {
array::from_fn(|col| Wire { row, col })
}
fn wire(mut self, col: usize, to: Wire) -> Self {
assert!(col < PERMUTS);
self[col] = to;
self
}
}
#[cfg(feature = "ocaml_types")]
pub mod caml {
use super::*;
use std::convert::TryInto;
#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlWire {
pub row: ocaml::Int,
pub col: ocaml::Int,
}
impl From<Wire> for CamlWire {
fn from(w: Wire) -> Self {
Self {
row: w.row.try_into().expect("usize -> isize"),
col: w.col.try_into().expect("usize -> isize"),
}
}
}
impl From<CamlWire> for Wire {
fn from(w: CamlWire) -> Self {
Self {
row: w.row.try_into().expect("isize -> usize"),
col: w.col.try_into().expect("isize -> usize"),
}
}
}
}
#[cfg(feature = "wasm_types")]
pub mod wasm {
use super::*;
#[wasm_bindgen::prelude::wasm_bindgen]
impl Wire {
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn create(row: i32, col: i32) -> Self {
Self {
row: row as usize,
col: col as usize,
}
}
}
}