Skip to content

change payment_method type to string array#128

Merged
grunch merged 2 commits into
mainfrom
change-payment-method-type
Jun 5, 2025
Merged

change payment_method type to string array#128
grunch merged 2 commits into
mainfrom
change-payment-method-type

Conversation

@grunch
Copy link
Copy Markdown
Member

@grunch grunch commented Jun 4, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved robustness and error handling when parsing order tags, reducing the risk of application crashes due to malformed input.
  • Refactor
    • Updated how payment methods are handled and displayed: now supports multiple payment methods, shown as a comma-separated list in order tables and previews.
  • Chores
    • Updated a dependency to the latest version for improved compatibility.

@grunch grunch requested a review from arkanoider June 4, 2025 21:01
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 4, 2025

Walkthrough

This update refactors payment method handling across several modules, changing it from a single string to a vector of strings. It updates the function signatures, parsing, serialization, and display logic to support multiple payment methods. Additionally, it improves error handling in tag parsing and updates the mostro-core dependency version.

Changes

File(s) Change Summary
Cargo.toml Updated mostro-core dependency from version 0.6.40 to 0.6.41.
src/cli/new_order.rs Changed payment_method parameter from &String to &str; now splits and trims into Vec<String>.
src/db.rs Serializes payment_method as a JSON string from a vector of strings when creating an Order.
src/nip33.rs Refactored order_from_tags for safer parsing; payment_method now supports multiple values.
src/pretty_table.rs Displays payment_method as a comma-separated list instead of a single string.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant DB
    participant PrettyTable

    User->>CLI: Provide payment methods as comma-separated string
    CLI->>CLI: Split and trim payment methods into Vec<String>
    CLI->>DB: Pass Vec<String> as payment_method
    DB->>DB: Serialize payment_method Vec<String> to JSON
    DB-->>CLI: Order created with serialized payment methods
    CLI->>PrettyTable: Request to display order(s)
    PrettyTable->>PrettyTable: Join payment methods as comma-separated string
    PrettyTable-->>User: Show order(s) with payment methods listed
Loading

Possibly related PRs

Poem

In fields of code where payments grow,
A string once stood, but now we sow
A garden full—each method new,
Split and trimmed, a vibrant view.
Orders bloom in rows so neat,
Comma-joined for all to greet!
🥕🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between adcab13 and 20a66b1.

📒 Files selected for processing (1)
  • src/nip33.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/nip33.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
src/nip33.rs (1)

62-91: 🛠️ Refactor suggestion

Inconsistent error handling: dispute_from_tags still uses unsafe patterns.

While order_from_tags was improved with robust error handling, dispute_from_tags still uses unwrap() calls that can panic. For consistency and reliability, apply similar safety improvements here.

Consider applying similar error handling patterns:

 pub fn dispute_from_tags(tags: Tags) -> Result<Dispute> {
     let mut dispute = Dispute::default();
     for tag in tags {
         let t = tag.to_vec();
-        let v = t.get(1).unwrap().as_str();
-        match t.first().unwrap().as_str() {
+        if t.is_empty() {
+            continue;
+        }
+        let v = t.get(1).map(|s| s.as_str()).unwrap_or_default();
+        match t.first().map(|s| s.as_str()).unwrap_or_default() {
             "d" => {
-                let id = t.get(1).unwrap().as_str().parse::<Uuid>();
+                let id_str = t.get(1).map(|s| s.as_str()).unwrap_or_default();
+                let id = Uuid::parse_str(id_str);
                 let id = match id {
                     core::result::Result::Ok(id) => id,
                     Err(_) => return Err(anyhow::anyhow!("Invalid dispute id")),
                 };
                 dispute.id = id;
             }
🧹 Nitpick comments (1)
src/nip33.rs (1)

36-36: Improved error handling with unwrap_or(0) but consider the appropriateness of zero defaults.

The change from unwrap() to unwrap_or(0) prevents panics, which is good. However, defaulting to 0 for amounts and premium might not always be semantically correct, especially for required fields like amount.

Consider whether 0 is an appropriate default for these financial fields, or if parsing failures should be handled differently (e.g., logging warnings or using Option types).

Also applies to: 44-44, 46-46, 53-53

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4af104f and adcab13.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • Cargo.toml (1 hunks)
  • src/cli/new_order.rs (2 hunks)
  • src/db.rs (2 hunks)
  • src/nip33.rs (1 hunks)
  • src/pretty_table.rs (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/pretty_table.rs (2)
src/db.rs (2)
  • new (84-105)
  • new (225-292)
src/cli/new_order.rs (1)
  • payment_method (60-63)
src/db.rs (1)
src/cli/new_order.rs (1)
  • payment_method (60-63)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (10)
Cargo.toml (1)

42-42: LGTM: Dependency update aligns with payment method refactor.

The mostro-core update to version 0.6.41 supports the changes from single payment method string to multiple payment methods as a vector.

src/pretty_table.rs (2)

71-71: LGTM: Proper display of multiple payment methods.

The change from .to_string() to .join(", ") correctly handles the new Vec<String> payment method format, displaying multiple payment methods as a readable comma-separated list.


176-177: LGTM: Consistent payment method display logic.

The join operation properly formats the payment method vector for table display, maintaining consistency with the preview function.

src/cli/new_order.rs (2)

22-22: LGTM: Improved parameter type.

The change from &String to &str follows Rust best practices and is more flexible for callers.


60-63: LGTM: Well-implemented multiple payment method parsing.

The logic properly splits comma-separated payment methods, trims whitespace, and collects into the expected Vec<String> format. This handles user input variations gracefully.

src/db.rs (1)

236-237: LGTM: Proper JSON serialization with error handling.

The serialization of the payment method vector to JSON string is the correct approach for database storage. The unwrap_or_default() provides reasonable fallback behavior if serialization fails.

src/nip33.rs (4)

2-2: LGTM: Added necessary import for Kind enum.

The import is correctly added to support the Kind::from_str usage in the updated parsing logic.


12-21: Excellent improvement in error handling and tag parsing robustness.

The refactored tag parsing logic significantly improves safety by:

  • Adding empty tag checks to prevent index out-of-bounds errors
  • Using safe array access with bounds checking
  • Eliminating potential panics from direct indexing

This is a substantial improvement over the previous implementation.


24-24: Good use of .ok() to prevent panics on parsing failures.

Converting the unwrap() calls to .ok() properly handles parsing failures without crashing, allowing the order to be created even if some fields fail to parse.

Also applies to: 27-27


50-50: LGTM: Successfully implements payment_method as vector of strings.

The change correctly collects all payment method values into a vector, supporting the new requirement for multiple payment methods per order.

Comment thread src/nip33.rs Outdated
@grunch grunch changed the title hange payment_method type to string array change payment_method type to string array Jun 5, 2025
@grunch grunch merged commit 0a67db1 into main Jun 5, 2025
2 checks passed
@grunch grunch deleted the change-payment-method-type branch June 5, 2025 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant