Facade
A facade is a narrow type or module that hides a noisy subsystem. Callers talk to one struct; the facade talks to the parser, the HTTP client, and the cache.
Detailed Explanation
A crate that exposes thirty types for a job that is "fetch a user" is hard to start. A facade is struct Users { http: Client, cache: Cache } with fn get(&self, id: UserId) -> Result<User>. Inside, it hits the cache, builds a request, parses JSON, maps errors to your enum. Outside, there is one method.
In Rust the facade is often a struct plus a small public module. The subsystem stays in crate::internal or in private modules. pub use re-exports the two types callers need. The rest of the crate is pub(crate). That is also how you keep a SemVer surface small: you can rewrite the HTTP crate behind the facade without breaking Users::get.
A facade is not a god object. If Users also sends email and writes billing, you have a pile, not a facade. One subsystem, one entry type. Several facades can share a Client via Arc.
Traits can be facades too: trait Store { async fn get(&self, id: Id) -> Result<User>; } implemented by a Postgres type and a fake. That is a facade at the boundary you test against. Do not make every internal helper a trait.
When you write a facade: a public crate, a subsystem with a messy setup, a place you want to swap implementations. When you do not: a 40-line module that is already clear. When the facade grows a dozen unrelated methods: split it.
Version the facade, not the internals. If Users::get stays stable, you can change the HTTP crate, the JSON shape you consume, and the cache without a SemVer bump. The moment you re-export an inner error type, that promise is gone. Map errors into an enum you own, even if the first version has one variant that only wraps reqwest::Error.
Code Examples
struct Http;
impl Http {
fn get(&self, path: &str) -> String {
format!("{{"path":"{path}"}}")
}
}
pub struct Users {
http: Http,
}
impl Users {
pub fn new() -> Self {
Self { http: Http }
}
pub fn get(&self, id: u64) -> String {
self.http.get(&format!("/users/{id}"))
}
}
fn main() {
println!("{}", Users::new().get(17)); // prints: {"path":"/users/17"}
}Common Gotchas
A facade that leaks every inner type is not a facade
If callers must name your HTTP crate's error, you have not hidden the subsystem. Map errors.
`pub use` is part of the pattern
Re-export `User` and `UserId`. Keep `hyper` types private.
God-object facades hide coupling, they do not remove it
Split along subsystems when the file becomes a catalogue of the whole program.
An `Arc<Inner>` facade is cheap to clone
Share the HTTP pool; do not rebuild a client per call.