I put pure helper functions in one IO interface's folder and called them from other interfaces
I recently refactored several backend services onto a layered architecture, and I made a structural mistake that the automated checker missed. The rules are clear. Standalone function modules belong in the data layer. Each folder under io/<interface>/ holds one IO interface and only implementations of it. An IO implementation must not depend on an IO of another interface. I broke all three in four services, and the result was 28 cross-interface edges.
What I did
When I ported each service, I carried small helpers along with the first module that needed them. Those helpers landed in that module’s new io/ folder:
- Error conversions such as “database driver error → service error” and “HTTP transport error → service error”
- A percent-encoder for URL path segments
- A “is this error a not-found?” predicate
- An I/O-error conversion
All of these are pure functions over data-layer types and library error types. None of them is an IO, but I filed them under one interface’s folder. Implementations of other interfaces then imported them from there. One interface folder ended up holding five free-function files next to its interface.
Two of the edges are worse than misplacement. In two services, one SQL IO called a free async fn that runs the insert for another interface’s table. That is a real dependency between IO implementations, exactly what the rule forbids. It also means one IO writes data whose primary type belongs to a different IO.
The checker passed all of it. It reads the rule as “an IO implementation type names another IO interface type,” and a free function in an io/ subfolder is neither. I treated “the checker is green” as if it meant “the rule holds,” and those are different claims.
Why it matters
- Discoverability: if you want to know how a database error becomes the service’s error, you have to know which interface happened to need it first.
- Hidden coupling: two IOs write tables they don’t own, and an import-based check can’t see it.
- Eroded boundaries: an interface folder that holds unrelated helpers stops being “one interface and its implementations,” and the next person will add more.
- Deferred judgment: when the edges were later found, the report listed them as “for you to rule on.” The rules already said where each piece belongs, so that call should have come with the report instead of being left to the operator.
What I should have done
- Ask what kind of construct each helper is. I should have done this before copying it next to its old caller. A pure function over data types is a data-layer function module. Error conversions go next to the error types they build, as a
Fromimpl or a function in that file. The encoder and the predicate go underdata/. Every layer may use the data layer, so then any IO can call them legitimately. - Keep each table’s writes in the IO that owns that table. If recording an event goes with another write, the service that calls both IOs should do both. Calling IO operations is the service layer’s job. If the two writes must share a transaction, that is a named invariant to design for, not a reason for one IO to run another’s SQL.
- Check what the rule says, not what the checker reads. A SQL string or helper that reaches another interface’s data is a dependency, just like an import of its type. I already knew this about crate boundaries and didn’t apply it between interfaces inside one crate.
How I’ll catch it next time
During review of any refactored crate I’ll look for:
- Files under
io/<x>/that aren’t the interface, an implementation, or the exploding test double, especially names like*_failure.rs,is_*.rs,insert_*.rs,send.rsorsegment.rs use crate::io::<a>::…inside a file underio/<b>/- Top-level
pub fn/pub(crate) fnin anio/subfolder async fns underio/that take a connection or pool and aren’t methods of an IO implementation
When I report a scan, I’ll state for each finding which rule it breaks and where the code belongs. I’ll only leave a ruling to someone else when two rules actually conflict.
I’ll also track the number of non-interface files under io/ and the number of cross-interface edges, per crate, after every refactor. This time it was 28 edges, nine misplaced helper files and two shared SQL functions. The target is zero.