use crate::{raw, share}; pub enum Element { VSource { label: String, voltage: String, rotation: share::Direction, }, ASource { label: String, current: String, rotation: share::Direction, }, Resistor { label: String, ohm: String, rotation: share::Direction, }, Terminal { label: String, }, Split { content: Vec>, }, Addon { offset: (isize, isize), content: Vec, }, Move(share::Direction), } pub fn desugar(raw_elements: Vec) -> Vec { let mut elements = vec![]; let handle_raw_move = |raw_direction: raw::Move, elements: &mut Vec| { let (count, direction) = match raw_direction { raw::Move::Up { value } => (value, share::Direction::Up), raw::Move::Down { value } => (value, share::Direction::Down), raw::Move::Left { value } => (value, share::Direction::Left), raw::Move::Right { value } => (value, share::Direction::Right), }; for _ in 0..count { elements.push(Element::Move(direction)); } }; for ele in raw_elements { match ele { raw::Element::Move(raw_direction) => { handle_raw_move(raw_direction, &mut elements); } raw::Element::Component(component) => match component { raw::Component::VSource { label, voltage, direction, rotation, } => { if let Some(direction) = direction { elements.push(Element::Move(direction)); } elements.push(Element::VSource { label, voltage, rotation: rotation.unwrap_or_else(|| direction.unwrap()), }); } raw::Component::ASource { label, current, direction, rotation, } => { if let Some(direction) = direction { elements.push(Element::Move(direction)); } elements.push(Element::ASource { label, current, rotation: rotation.unwrap_or_else(|| direction.unwrap()), }); } raw::Component::Resistor { label, ohm, direction, rotation, } => { if let Some(direction) = direction { elements.push(Element::Move(direction)); } elements.push(Element::Resistor { label, ohm, rotation: rotation.unwrap_or_else(|| direction.unwrap()), }); } raw::Component::Split { content } => { let desugerd_content = content.into_iter().map(desugar).collect(); elements.push(Element::Split { content: desugerd_content, }); } raw::Component::Addon { offset, content } => { elements.push(Element::Addon { offset: offset.unwrap_or((0, 0)), content: desugar(content), }); } raw::Component::Terminal { label, direction } => { if let Some(dir) = direction { elements.push(Element::Move(dir)); } elements.push(Element::Terminal { label }); } }, raw::Element::Wire(moves) => { for action in moves { handle_raw_move(action, &mut elements); } } } } elements }