Migrate to rust-error and refactor