agentleFS
dawidpereiradawidpereira/rust-skills/skills/rust-serde/SKILL.md

rust-serde

Skill0 starsChanged 8 months agoMITView on GitHub

Serde derive macros, custom serializers/deserializers, enum representations (tagged, untagged, adjacently tagged), serde attributes (rename, default, flatten, skip, with), zero-copy deserialization, format selection (JSON, TOML, bincode), DTO patterns, and feature-gating serde in libraries. Use when working with serialization, deserialization, JSON APIs, config files, wire formats, or data conversion between Rust types and external representations.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every Serialize / Deserialize derive is a contract with the outside world.

    line 22
  2. Distinctive:

    Use adjacently tagged: when you need versioned payloads or the content can be any type.

    line 90
  3. Distinctive:

    Use untagged: only as a last resort — error messages are poor and it tries variants in order.

    line 93
  4. Distinctive:

    Use an optional feature:

    line 126
  5. Very specific:

    Never put serde derives on domain types directly.

    line 173

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Serde & Serialization

Core Question

How does this data cross the boundary between Rust types and external formats?

Every Serialize / Deserialize derive is a contract with the outside world. The shape of your Rust types becomes a wire format, a config schema, or a storage layout. Get the representation wrong and you break every consumer.


Error -> Design Question

SymptomDon't Just SayAsk Instead
"expected struct, found string""Fix the JSON"Is your enum representation correct for this format?
"missing field x""Add the field"Should this field have a default or be Optional?
"duplicate field x""Remove one"Is #[serde(flatten)] creating key collisions?
"unknown field x""Ignore it"Should you deny_unknown_fields to catch drift?
Custom deserializer panics"Fix the input"Is parse-don't-validate the right approach here?
"data did not match any variant""Add a variant"Do you need #[serde(other)] for forward compat?

Quick Decisions

SituationReach ForWhy
New struct for JSON API#[derive(Serialize, Deserialize)]Derive covers 90% of cases
Enum over the wire#[serde(tag = "type")]Internally tagged is cleanest for JSON APIs
Rust snake_case to JSON camelCase#[serde(rename_all = "camelCase")]One attribute on the container
Optional fields in inputOption<T> + #[serde(default)]Missing keys become None, not errors
Newtype wrapper (e.g., UserId(Uuid))#[serde(transparent)]Serializes as the inner type
Reject unexpected keys#[serde(deny_unknown_fields)]Catches typos and schema drift early
Feature-gate serde in a libraryserde feature + optional depConsumers opt in, no forced dependency
Flatten nested config#[serde(flatten)]Merges child fields into parent — watch for key collisions and perf cost
Zero-copy deserialization&'de str or Cow<'de, str>Borrows from input buffer, avoids allocation
Date/time custom format#[serde(with = "module")]Isolates format logic in a helper module
Sensitive fields (passwords, keys)#[serde(skip)]Never serialize secrets to the wire
Default values for missing fields#[serde(default = "path::to_fn")]Provides fallback without Option wrapping
Validate on deserializeCustom Deserialize impl or wrapperEnforces invariants at the boundary
JSON API serverserde_jsonStandard, well-tested, good errors
Config filestoml or serde_yamlHuman-friendly formats for configuration
High-throughput parsingsimd-json with serde compatSIMD-accelerated, drop-in replacement

Enum Representation Strategies

Serde supports four enum encodings. The default (externally tagged) is rarely what you want for JSON APIs.

#[derive(Serialize, Deserialize)]
enum Message {
    Text { body: String },
    Image { url: String, width: u32 },
}
StrategyAttributeJSON Output
Externally tagged(default){"Text": {"body": "hi"}}
Internally tagged#[serde(tag = "type")]{"type": "Text", "body": "hi"}
Adjacently tagged#[serde(tag = "t", content = "c")]{"t": "Text", "c": {"body": "hi"}}
Untagged#[serde(untagged)]{"body": "hi"}

Use internally tagged for JSON APIs — it matches how most languages model discriminated unions.

Use adjacently tagged when you need versioned payloads or the content can be any type.

Use untagged only as a last resort — error messages are poor and it tries variants in order.

See references/enums.md for decision matrix and forward-compatibility patterns.


The Attribute Quick Reference

AttributeLevelEffect
rename_allContainerRenames all fields/variants
deny_unknown_fieldsContainerRejects unexpected keys
defaultContainer/FieldUses Default::default for missing fields
tag / contentContainerControls enum representation
transparentContainerDelegates to single inner field
renameField/VariantRenames one field or variant
skipField/VariantExcludes from both ser and de
flattenFieldMerges nested struct fields inline
withFieldCustom ser/de via module path
aliasField/VariantAccepts alternative name on input
default = "path"FieldCustom default value function
otherVariantCatch-all for unknown variants

See references/attributes.md for full details and code examples.


Feature-Gating Serde in Libraries

Library crates should not force serde on consumers. Use an optional feature:

[features]
default = []
serde = ["dep:serde"]

[dependencies]
serde = { version = "1", features = ["derive"], optional = true }
#[cfg_attr(feature = "serde", derive(
    serde::Serialize, serde::Deserialize,
))]
pub struct Config {
    pub name: String,
    pub timeout_ms: u64,
}

This pattern keeps the dependency tree lean for consumers who don't need serialization.


Usage Scenarios

Scenario 1: "I'm building JSON API request/response types" -> Derive Serialize + Deserialize, use rename_all = "camelCase", internally tagged enums, and deny_unknown_fields on input types. Keep response types permissive, input types strict. See references/attributes.md.

Scenario 2: "I need to parse a TOML config file into typed structs" -> Derive Deserialize, use #[serde(default)] for optional settings, nested structs for sections. Validate invariants after deserialization or in a custom impl. See references/custom.md.

Scenario 3: "My domain types differ from my wire format" -> Create separate DTO structs with serde derives. Convert between domain and DTO using From/Into. Never put serde derives on domain types directly. See references/custom.md for DTO patterns.

Scenario 4: "I need to deserialize large JSON payloads fast" -> Use simd-json with serde compat, &'de str for zero-copy strings, and avoid #[serde(flatten)] which disables struct-level optimizations. See references/custom.md for zero-copy patterns.


Reference Files

FileRead When
references/attributes.mdContainer, field, and variant attributes with examples
references/enums.mdEnum representation strategies, decision matrix, forward compatibility
references/custom.mdCustom ser/de, zero-copy, visitors, serde_with, DTO mapping

Cross-References

WhenCheck
Newtype wrappers, PhantomData in typesrust-types -> Quick Decisions
Feature gating, public API namingrust-api -> Quick Decisions
DTO to domain model mappingrust-ddd -> Quick Decisions
Request/response type placementrust-architecture -> Quick Decisions

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.