Preserve spendable metadata - #9
Conversation
Add a parsing path that reports whether output labels included an explicit spendable field, so callers can distinguish omitted values from explicit booleans. Preserve output field presence and representation when parsing labels so callers can distinguish omitted values from explicit booleans while keeping default parsing behavior unchanged.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughAdds ChangesSpendable Metadata API
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
9e7c45c to
0df0f9f
Compare
|
@greptileai review |
There was a problem hiding this comment.
Pull request overview
Adds an API for importing BIP329 JSONL while preserving the original spendable field presence and JSON representation for output records (omitted vs boolean vs string-boolean), which is otherwise normalized away by OutputRecord.
Changes:
- Introduce
ParsedLabels,OutputSpendableField, andSpendableFieldValueto expose outputspendablemetadata. - Add
Labels::try_from_str_with_metadata()to return normalized labels plus captured outputspendablemetadata, along with tests. - Reuse
SpendableFieldValueparsing in thespendablefield deserializer for consistent handling of string/boolean forms.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/lib.rs | Adds new public metadata-carrying types (ParsedLabels, OutputSpendableField, SpendableFieldValue) and parsing helpers. |
| src/label.rs | Adds try_from_str_with_metadata() and tests to preserve output spendable field metadata during import. |
| src/serde_util.rs | Refactors string-or-bool parsing to use the new SpendableFieldValue deserialization logic. |
| CHANGELOG.md | Documents the new metadata-preserving import API. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // keep normal output parsing aligned with metadata-aware parsing | ||
| let value = <SpendableFieldValue as serde::Deserialize>::deserialize(deserializer)?; | ||
|
|
||
| match StringOrBool::deserialize(deserializer)? { | ||
| StringOrBool::Bool(b) => Ok(b), | ||
| StringOrBool::String(s) => match s.to_ascii_lowercase().as_str() { | ||
| "true" => Ok(true), | ||
| "false" => Ok(false), | ||
| string => { | ||
| let msg = format!("Invalid boolean string: {string}"); | ||
| Err(serde::de::Error::custom(msg)) | ||
| } | ||
| }, | ||
| } | ||
| value | ||
| .explicit_value() | ||
| .ok_or_else(|| serde::de::Error::custom("missing spendable value")) |
Greptile SummaryThis PR adds Confidence Score: 4/5Safe to merge — the new parsing path correctly handles all three spendable field representations, and the serde_util refactor preserves the existing deserialization behaviour for OutputRecord. The double-parse approach in try_from_str_with_metadata is sound and the three new tests cover the intended cases. The unreachable error branch in serde_util and the absence of a file-based metadata variant are the only rough edges. src/serde_util.rs (dead ok_or_else arm) and src/label.rs (no try_from_file_with_metadata counterpart) deserve a quick second look before merging. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[JSONL input string] --> B{try_from_str_with_metadata}
B --> C[Trim & iterate lines]
C --> D[serde_json::from_str as Label]
D -->|parse error| E[Return ParseError]
D --> F{Label::Output?}
F -->|No| G[Append to labels vec]
F -->|Yes| H[serde_json::from_str as OutputSpendableMetadata]
H -->|parse error| E
H --> I{spendable field in JSON?}
I -->|Absent| J[SpendableFieldValue::Omitted]
I -->|JSON bool| K[SpendableFieldValue::Boolean]
I -->|JSON string| L{valid 'true'/'false'?}
L -->|Yes| M[SpendableFieldValue::String]
L -->|No| E
J & K & M --> N[Push OutputSpendableField ref_+value]
N --> G
G --> O[Return ParsedLabels labels + output_spendable vec]
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A[JSONL input string] --> B{try_from_str_with_metadata}
B --> C[Trim & iterate lines]
C --> D[serde_json::from_str as Label]
D -->|parse error| E[Return ParseError]
D --> F{Label::Output?}
F -->|No| G[Append to labels vec]
F -->|Yes| H[serde_json::from_str as OutputSpendableMetadata]
H -->|parse error| E
H --> I{spendable field in JSON?}
I -->|Absent| J[SpendableFieldValue::Omitted]
I -->|JSON bool| K[SpendableFieldValue::Boolean]
I -->|JSON string| L{valid 'true'/'false'?}
L -->|Yes| M[SpendableFieldValue::String]
L -->|No| E
J & K & M --> N[Push OutputSpendableField ref_+value]
N --> G
G --> O[Return ParsedLabels labels + output_spendable vec]
|
Refactor label parsing to preserve output-level spendable metadata while normalizing Label records. Introduces a serde-tagged ParsedLabelLine enum and ParsedOutputRecord to deserialize each JSONL line by type, extract Label instances, and separately collect OutputSpendableField entries for outputs. Updates try_from_str_with_metadata to iterate lines, convert parsed variants into labels and optional spendable metadata, and accumulate both. Adjusts serde_util deserializer to return the explicit boolean value for present spendable fields (and mark omitted as unreachable). Adds a unit test verifying mixed label types and output spendable metadata handling. Also updates imports to support the new types.
Summary by CodeRabbit
New Features
spendablefield format, distinguishing between omitted, boolean, and string representations.Documentation