Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133use 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<Vec<Element>>, }, Addon { offset: (isize, isize), content: Vec<Element>, }, Move(share::Direction),}
pub fn desugar(raw_elements: Vec<raw::Element>) -> Vec<Element> { let mut elements = vec![];
let handle_raw_move = |raw_direction: raw::Move, elements: &mut Vec<Element>| { 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}