Skip to content

refactor: extract generic field parsing helpers for Daml contract arguments #29

@gyorgybalazsi

Description

@gyorgybalazsi

Context

Across src/mint_redeem/models.rs and src/credentials.rs, there are 30+ instances of the same patterns for extracting fields from Daml createArgument JSON:

Required string fields:

let owner = args
    .get("owner")
    .and_then(|v| v.as_str())
    .ok_or("Missing 'owner' field")?
    .to_string();

Optional deserialized fields:

let limits = match args.get("limits") {
    None => None,
    Some(v) if v.is_null() => None,
    Some(v) => Some(
        serde_json::from_value::<Limits>(v.clone())
            .map_err(|e| format!("Invalid 'limits' field: {}", e))?,
    ),
};

Proposal

Extract two generic helpers:

  • required_field<T>(args, "field") — get field, error if missing/null, deserialize to T
  • optional_field<T>(args, "field") — get field, None if missing/null, deserialize to T or error

Optionally add a convenience required_str(args, "field") for the common string case.

Files affected

  • src/mint_redeem/models.rs (~20 instances)
  • src/credentials.rs (~10 instances)

Origin

Identified during PR #28 (credential and min/max limits support).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions