Skip to content

Refactor add_record and edit_record to avoid expect() calls #2

Description

@orveth

Problem

The add_record and edit_record methods in src/client.rs (lines 189 and 236) currently use #[allow(clippy::missing_panics_doc)] annotations because they call .expect("json object") on a serde_json::Value.

#[allow(clippy::missing_panics_doc)]
pub fn add_record(&self, params: &AddRecordParams) -> Result<Record> {
    let mut json_params = serde_json::json!({
        "domain": params.domain,
        "type": params.record_type,
        "name": params.name,
    });

    // Safe: json! macro always creates an object when given object syntax
    let obj = json_params.as_object_mut().expect("json object");
    // ...
}

While the comment explains that the expect() is safe because json! with object syntax always creates an object, this pattern:

  1. Requires the clippy allow annotation
  2. Has a theoretical panic point even if unreachable

Proposed Solution

Refactor to build the JSON object directly using serde_json::Map:

pub fn add_record(&self, params: &AddRecordParams) -> Result<Record> {
    let mut obj = serde_json::Map::new();
    obj.insert("domain".to_string(), serde_json::json!(params.domain));
    obj.insert("type".to_string(), serde_json::json!(params.record_type));
    obj.insert("name".to_string(), serde_json::json!(params.name));

    if let Some(content) = &params.content {
        obj.insert("content".to_string(), serde_json::json!(content));
    }
    // ... rest of optional fields ...

    self.request("add-record", serde_json::Value::Object(obj))
}

This approach:

  • Eliminates the expect() call entirely
  • Removes the need for #[allow(clippy::missing_panics_doc)]
  • Makes the code slightly more explicit about what's happening

Files Affected

  • src/client.rs: add_record (line 189) and edit_record (line 236)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions