URLFormat banner

Open SourceSwift

URLFormat Overview

Type safe url pattern matching without regular expressions and arguments type mismatches based on parser combinators.

URLFormat is a URL builder that allows you to describe URL in a natural manner and allows you to pattern match it in a type safe way.

The conventional way of representing URL patterns, i.e. for web server API routes, is using some kind of string placeholder for parameters, i.e. /user/:name. This is then parsed, and path and query parameters are aggregated into a collection. The issue is that this approach is error-prone (what if : is missed) and access to the parameters is not type safe - it's possible to access parameters as a wrong type or conversion must be implemented by the client, and it's possible to access parameter by the wrong key or index.

Another approach that Swift allows is to use enums pattern matching, as described in this post and implemented in URLPatterns. While this approach allows type-safe access to parameters it's not very ergonomic and nice to read:

Stay on top of new tools for iOS developers

Subscribe to get a convenient weekly roundup of iOS Dev Tools' newest addition
of helpful and time-saving developer tools, services, and SDKs to get your job done faster.