Rust Domain Boundaries
Use this skill to keep invalid states out of Rust service internals. Parse raw
input once at the boundary, store validated values in narrow domain types, and
make unchecked construction difficult.
Core Workflow
- Find raw input boundaries: HTTP payloads, path/query parameters, config
files, environment variables, database rows, queues, and CLI flags.
- Separate transport DTOs from domain types. Let
serde deserialize incoming
shapes, then convert DTO fields into validated domain values.
- Replace primitive strings with small types for ruled values: email addresses,
usernames, passwords, subscriber names, slugs, tenant IDs, idempotency keys,
and money-like or duration-like values.
- Give domain types private fields and smart constructors. Avoid unchecked
pub fields or impl From<String> for fallible conversions.
- Return typed validation errors that map to useful HTTP responses without
leaking internal details.
- Keep database and external API mapping explicit. Convert to raw strings at
the final persistence or serialization edge.
- Test invariants directly. Cover valid examples, malformed inputs, boundary
lengths, normalization rules, and round trips.
Type Design Rules
- Prefer
TryFrom<String>, TryFrom<&str>, or FromStr for fallible parsing.
- Keep stored values owned unless profiling proves borrowing is necessary.
- Implement
AsRef<str> or a named accessor for read-only exposure.
- Implement
Display only when the formatted value is safe to show in logs,
errors, and UI.
- Avoid deriving
Debug for secret-bearing values unless the debug output is
redacted.
- Make normalization visible in tests: trim, lowercase, Unicode handling, and
canonicalization.
Request Boundary Pattern
Deserialize into a request shape, then construct a command:
#[derive(serde::Deserialize)]
pub struct SubscribeRequest {
email: String,
name: String,
}
pub struct SubscribeCommand {
pub email: EmailAddress,
pub name: SubscriberName,
}
impl TryFrom<SubscribeRequest> for SubscribeCommand {
type Error = SubscribeValidationError;
fn try_from(value: SubscribeRequest) -> Result<Self, Self::Error> {
Ok(Self {
email: EmailAddress::parse(value.email)?,
name: SubscriberName::parse(value.name)?,
})
}
}
Handlers should reject invalid input before business logic or database code. If
validation needs database state, keep pure parsing separate from uniqueness or
authorization checks.
Tests
Read references/property-testing.md when invariants have many edge cases or
when an AI agent is likely to miss invalid inputs with example-only tests.
Minimum tests for a new domain type:
- Accept a realistic valid value.
- Reject empty input and whitespace-only input.
- Reject too-long input when storage or product rules impose limits.
- Reject format violations.
- Preserve or normalize exactly as documented by tests.
- Round-trip through
serde or SQL mapping when that type crosses those
boundaries.
Reference Files
references/newtype-patterns.md: constructor, trait, serde, and persistence
patterns for Rust newtypes.
references/property-testing.md: property-testing strategy for parsers and
domain constructors.
Source: hashgraph-online/awesome-codex-plugins → plugins/LVTD-LLC/skills/skills/rust-domain-boundaries/SKILL.md
1---2name: rust-domain-boundaries3description: Use when modeling, validating, refactoring, or reviewing Rust service domain boundaries, especially when replacing primitive String fields with newtypes, parse-don't-validate constructors, private invariants, TryFrom/FromStr parsers, request DTO boundaries, or property tests.4---567# Rust Domain Boundaries89Use this skill to keep invalid states out of Rust service internals. Parse raw10input once at the boundary, store validated values in narrow domain types, and11make unchecked construction difficult.1213## Core Workflow14151. Find raw input boundaries: HTTP payloads, path/query parameters, config16 files, environment variables, database rows, queues, and CLI flags.172. Separate transport DTOs from domain types. Let `serde` deserialize incoming18 shapes, then convert DTO fields into validated domain values.193. Replace primitive strings with small types for ruled values: email addresses,20 usernames, passwords, subscriber names, slugs, tenant IDs, idempotency keys,21 and money-like or duration-like values.224. Give domain types private fields and smart constructors. Avoid unchecked23 `pub` fields or `impl From<String>` for fallible conversions.245. Return typed validation errors that map to useful HTTP responses without25 leaking internal details.266. Keep database and external API mapping explicit. Convert to raw strings at27 the final persistence or serialization edge.287. Test invariants directly. Cover valid examples, malformed inputs, boundary29 lengths, normalization rules, and round trips.3031## Type Design Rules3233- Prefer `TryFrom<String>`, `TryFrom<&str>`, or `FromStr` for fallible parsing.34- Keep stored values owned unless profiling proves borrowing is necessary.35- Implement `AsRef<str>` or a named accessor for read-only exposure.36- Implement `Display` only when the formatted value is safe to show in logs,37 errors, and UI.38- Avoid deriving `Debug` for secret-bearing values unless the debug output is39 redacted.40- Make normalization visible in tests: trim, lowercase, Unicode handling, and41 canonicalization.4243## Request Boundary Pattern4445Deserialize into a request shape, then construct a command:4647```rust48#[derive(serde::Deserialize)]49pub struct SubscribeRequest {50 email: String,51 name: String,52}5354pub struct SubscribeCommand {55 pub email: EmailAddress,56 pub name: SubscriberName,57}5859impl TryFrom<SubscribeRequest> for SubscribeCommand {60 type Error = SubscribeValidationError;6162 fn try_from(value: SubscribeRequest) -> Result<Self, Self::Error> {63 Ok(Self {64 email: EmailAddress::parse(value.email)?,65 name: SubscriberName::parse(value.name)?,66 })67 }68}69```7071Handlers should reject invalid input before business logic or database code. If72validation needs database state, keep pure parsing separate from uniqueness or73authorization checks.7475## Tests7677Read `references/property-testing.md` when invariants have many edge cases or78when an AI agent is likely to miss invalid inputs with example-only tests.7980Minimum tests for a new domain type:8182- Accept a realistic valid value.83- Reject empty input and whitespace-only input.84- Reject too-long input when storage or product rules impose limits.85- Reject format violations.86- Preserve or normalize exactly as documented by tests.87- Round-trip through `serde` or SQL mapping when that type crosses those88 boundaries.8990## Reference Files9192- `references/newtype-patterns.md`: constructor, trait, serde, and persistence93 patterns for Rust newtypes.94- `references/property-testing.md`: property-testing strategy for parsers and95 domain constructors.9697---9899**Source:** [`hashgraph-online/awesome-codex-plugins`](https://github.com/hashgraph-online/awesome-codex-plugins) → `plugins/LVTD-LLC/skills/skills/rust-domain-boundaries/SKILL.md`