Crates.io | uri-template-system |
lib.rs | uri-template-system |
version | 0.1.5 |
source | src |
created_at | 2023-08-18 23:40:55.471927 |
updated_at | 2023-08-24 14:18:42.79071 |
description | URI Template (RFC 6570) Parsing, Expansion and Matching |
homepage | https://github.com/kolektiv/uri-template-system |
repository | |
max_upload_size | |
id | 948401 |
size | 5,551 |
URI Templates (RFC6570) are an underrated tool for web programming. They regularise the construction of URIs and related forms, removing the need for ad-hoc string manipulation, and can help provide consistency across complex web applications.
use uri_template_system_core::{ Template, Value, Values };
let template = Template::parse("/hello/{name}/from{/library*}").unwrap();
let values = Values::default()
.add("name", Value::item("world"))
.add("library", Value::list(["uri", "template", "system"]));
assert_eq!(
template.expand(&values).unwrap(),
"/hello/world/from/uri/template/system"
);
Beyond the scope of the RFC itself, they can also be used (with some judgement) for matching URIs and related forms as well, effectively providing the basis for a routing mechanism which can be driven from the same data as the linking mechanism. No more mismatches between linking and routing.
This library provides an implementation of URI Templates which complies with the RFC and passes the standard test cases for parsing and expansion.
Future iterations will extend this to provide matching for both single and multiple URI templates, routing based on URI Template matching, and support for strongly-typed template data. See the milestones for the latest progress and for more detailed descriptions of planned features.
Existing implementations of URI Templates exist for Rust, notably rust-uritemplate
and the various forks of that implementation, and an implementation in the broader iri-string
library.
This implementation aims to be both more "correct" (the parser is intentionally strict with regards to the RFC, while the existing implementations allow some malformed templates through as valid) and more structured in terms of underlying representation (with the aim of making additional features such as matching more tractable). Performance is also important, and the implementation is roughly on-par with -- or slightly quicker than -- iri-string
(the rust-uritemplate
derivations have a slightly different programming model, but even allowing for this, they are generally significantly slower).
Benchmarks and RFC-compliance tests (and test harnesses) can be found in the tests
directory - as always, benchmarks are indicative but performance under real-world conditions will always vary.