String URL parsing vs Types?
Created by: james-darkfox
URLs everywhere! Strings? Not ideal for performance. Has anyone considered using builders? If the usage is the concern, then builder macros can also be used.
If various schemes need to be accepted, then a trait may be used and implemented over the respective types. (This can also be used to restrict inputs with marker traits. I.e. Only accept a context [file, remote, hardware, etc])
One limitation of the sample macro I'm providing here is that we cannot use //
due to comments.
let server: Tcp = url!(Tcp:"10.0.0.1"/80);
takes_url(server); // Tcp implements Url
takes_tcp(server);
This sample code does not implement full URL parsing but it is a start demonstrating the idea. https://play.rust-lang.org/?gist=af1d8867e0a10feddaaa&version=nightly
Enums could be used to accept different types. I.e. 10.0.0.1 = Ipv4([0u8;4])
, "hostname" = Host(&'a str)
, aa:bb:cc:dd:ee:ff = Mac([u8; 6])
, etc.
Files COULD be parsed by identifiers; namely. url!(File:file)
url!(File:/foo/bar)
but this would limit the dirs/files to having names that are valid identifiers. Alternatively an expression "/foo/bar-baz"
could be an option too (if not the only option).