julianandrews / sgf-parse

SGF parsing library for Rust.
MIT License
13 stars 3 forks source link

Add an `SgfNodeBuilder` struct #6

Closed julianandrews closed 3 years ago

julianandrews commented 3 years ago

Currently SgfNode is immutable, and validates that all SGF specification requirements are met. This has a lot to recommend it, but it does mean that editing an SGF requires either lots of cloning, or destructuring and rebuilding.

Adding the functionality to modify an SgfNode would be work intensive and complicate the interface substantially. I want to keep that simple and immutable. Instead, I can add an SgfNodeBuilder struct with public fields that allows direct and easy access to the internals. This struct can then have a build method that validates the entire tree and returns the root immutable node. An SgfNode would also have a to_builder method which consumes the SgfNode and its children, and returns an SgfNodeBuilder.

julianandrews commented 3 years ago

Importantly - SgfNodeBuilder wouldn't make any effort to force the intermediate stages to be valid SGF. Validation would happen only on calls to build. It would also make sense to add an is_valid method to the builder, to allow checking the state in the middle of the process.

julianandrews commented 3 years ago

All done!