Skip to content

feat(rust): add dt parameter to encryption response#11

Merged
calvinbrewer merged 4 commits into
mainfrom
feat/encryption-response-data-type-param
Jul 1, 2025
Merged

feat(rust): add dt parameter to encryption response#11
calvinbrewer merged 4 commits into
mainfrom
feat/encryption-response-data-type-param

Conversation

@coreyhn
Copy link
Copy Markdown
Collaborator

@coreyhn coreyhn commented Jul 1, 2025

This change makes it so the cast_as value from the encryption configuration is returned as the dt parameter in all encryption responses. The data type parameter makes it so encrypted data can be type casted without requiring access to the original configuration. This will allow the cipherstash/protectphp library to have automatic data type detection during encryption (with optional overrides) and data type casting during decryption when using this library, for example:

$encrypted = Protect::encrypt('users.balance', 42);

echo $encrypted;
// {"k":"ct","c":"mBbJ_{U+!@oO9P*hGG5rslSWF6d2...","dt":"small_int","hm":"89bd7c0deb763305bf26212aa96f...","ob":["9930303030303030c68b2822197a..."],"bf":null,"i":{"t":"users","c":"balance"},"v":2}

$decrypted = Protect::decrypt($encrypted);

echo $decrypted;
// 42

The strum crate was added for CastAs enum string serialization instead of relying on serde because it provides better performance for this use case. Rather than using serde to serialize the entire enum to JSON just to extract the string representation for a single parameter, strum's dedicated Display derive macro allows direct string conversion with snake_case formatting while maintaining separate control over JSON serialization.

coreyhn and others added 4 commits June 30, 2025 17:57
This change makes it so the `cast_as` value from the encryption
configuration is returned as the `dt` parameter in all encryption
responses. The data type parameter makes it so encrypted data can be
type casted without requiring access to the original configuration.
This will allow the `cipherstash/protectphp` library to have automatic
data type casting when decrypting data with this library.

The `strum` crate was added for `CastAs` enum string serialization
instead of relying on `serde` because it provides better performance
for this use case. Rather than using `serde` to serialize the entire
enum to JSON just to extract the string representation for a single
parameter, `strum`'s dedicated `Display` derive macro allows direct
string conversion with clean `snake_case` formatting while maintaining
separate control over JSON serialization.
This change removes unnecessary invariant violation checks for
`identifiers` and `cast_types` array access in `encrypt_bulk_inner`.
Since these vectors are constructed in parallel from the same input
using `fold`, they are guaranteed to have identical lengths. The loop
bounds use the same length value, making these error checks redundant.

The code now uses direct indexing (`&identifiers[index]` and
`&cast_types[index]`) instead of `.get().ok_or_else()` patterns,
improving readability and performance while maintaining the same safety
guarantees through the construction logic.
This change refactors the tests for the `Encrypted` enum to provide
more focused and valuable test coverage. Replaces the complex roundtrip
serialization test with two JSON format tests that validate the structure
and field mapping.

The new tests verify specific JSON field names, values, and structure for
both `Ciphertext` and `SteVec` variants. This approach tests the actual
FFI contract rather than just basic serde functionality, improving test
maintainability and relevance.
@coreyhn coreyhn requested a review from calvinbrewer July 1, 2025 01:21
@calvinbrewer calvinbrewer merged commit f97616c into main Jul 1, 2025
@coreyhn coreyhn deleted the feat/encryption-response-data-type-param branch July 1, 2025 23:01
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.

2 participants