Implement the KiCad expression evaluator (for real this time)#14
Conversation
| // Collect all attributes for all components | ||
| let mut paths = Vec::new(); | ||
| for (k, v) in index.map.iter() { | ||
| if let Node::Component(idx) = v { |
There was a problem hiding this comment.
maybe use comp or something similar here? that'd be clearer
There was a problem hiding this comment.
I chose idx for index to specifically distinguish that what's stored there is a lookup table, not any sort of Component object. But I've spelt it (and some nearby fields as well) out now for better clarity.
| pub struct SheetIndex<'a> { | ||
| pub(crate) map: HashMap<String, Node<'a>>, | ||
| } |
There was a problem hiding this comment.
Why is SheetIndex a struct by ComponentIndex a type re-definition?
Is that the only way to add functions to the type (I guess so)?
There was a problem hiding this comment.
That's exactly right, in Rust you can only impl for types that are defined in the same crate. With a type alias I would be impling for HashMap, which wouldn't be possible since it is defined externally. ComponentIndex just didn't need any additional functionality other than the type safety it provides, so it's simpler to have it as an alias. For the SheetIndex I'm also using the pub(crate) visibility modifier for map to restrict external accessors accidentally modifying it.
|
|
||
| pub fn resolve_entry<'b>( | ||
| &self, | ||
| mut path: impl ExactSizeIterator<Item = &'b String>, |
There was a problem hiding this comment.
This seems fancy, haven't seen this kind of impl statement before
There was a problem hiding this comment.
It's essentially a simpler variant of a generic with a trait bound in Rust 2018 (1.26 and above). There are some restrictions around the turbofish operator with it compared to explicit generics, but in this simple case it makes no meaningful difference other than this syntax being likely a bit more readable.
|
Thanks |
This took much longer than expected, but the KiCad
evaluatoris finally here, fully functional and ready to roll. What a Rust adventure and learning experience this has been 😅 This initial implementation of theevaluatorsupports the following features (out of all currently planned features):<schematic>.<component>.<attribute>, e.g.DCDC.R3.Tolerance. (Parent schematics cannot be accessed by design to preserve the capability of evaluating every schematic independently.)voltage_divider(...)(either baked-in or loaded externally).Schematicformat back into thekicad_parse_gen::schematic::Schematic(Implement updating and exportingkicad_parse_genSchematics for the evaluator #18).As evidenced by the rather large commits, this has gone through many revisions until I finally figured out how to implement this properly in Rust. Thus, I suggest squashing the commits when merging. I'm also aware that the code is a bit lightly commented right now due to lack of time, but I will improve that front with upcoming PRs. I can however also add the comments to this PR if that's preferred.
cc @luxas @chiplet