Typestate encodes lifecycle state in the type system.
oauth2-rs uses it for a concrete pain point: OAuth2 clients need auth_url, token_url, introspect_url, and friends wired up before auth calls make sense. If token_url is missing, exchanging an authorization code for a token fails at runtime.
Forgot to call set_token_url? You find out when you fetch a token—not when you build the client.
Typestate pushes that mistake to compile time.
Typestate
State lives in the type. In a strongly typed language, the compiler can reject calls that do not match the current state.
Two ticket types illustrate the idea:
EmptyTicket can only be created or booked.
book moves you to BookedTicket, where you may use (consume) or cancel back to EmptyTicket.
auth_url and token_url each have an EndpointState.
Initially both are unset (EndpointNotSet). Calling a setter returns a Client with the corresponding type parameter flipped to EndointSet.
Methods that need a configured URL are only implemented on Client variants that carry EndointSet in the right slot—so you cannot call token helpers until the type says the token URL is set.
For example, Client<EndointSet, EndpointNotSet> may use auth-url APIs but not token-url APIs, because the type still says token_url is unset.