# manual-struct-conversion

Prefer conversions over field-by-field reconstruction

Reconstructing every field of one nominal value from an equally shaped value repeats a conversion wherever it is needed.
Instead, you SHOULD implement `From` for the destination type and call its `from` method.

- Category: style
- Level: warning
- Fix: none
- Scope: module

## Reported

```ds title="main.ds"
struct Input {
    x: int32;
    y: int32;
}
struct Point {
    x: int32;
    y: int32;
}

function convert(input: Input): Point {
    return Point { x: input.x, y: input.y };
}
```

## Accepted

```ds title="main.ds"
struct Input {
    x: int32;
    y: int32;
}
struct Point {
    x: int32;
    y: int32;
}

extension of Point implements From<Input> {
    static from(input: Input): Point {
        return Point { x: input.x, y: input.y };
    }
}

function convert(input: Input): Point {
    return Point.from(input);
}
```

[language/linter/src/rules/style/manual_struct_conversion.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/manual_struct_conversion.rs#L8)
