Parse an npm "person" string into its parts.
npm lets the author, contributors, and maintainers fields of a
package.json use a shorthand string of the form Name <email> (url). This
crate parses that shorthand into an Author with optional name, email, and
url. Every part is optional. The name, when present, is the leading text
before the first bracket. The email and url tokens may appear in either order.
[dependencies]
author-parser = "0.1"use author_parser::parse;
let a = parse("Jon Schlinkert <jon@example.com> (https://example.com)");
assert_eq!(a.name.as_deref(), Some("Jon Schlinkert"));
assert_eq!(a.email.as_deref(), Some("jon@example.com"));
assert_eq!(a.url.as_deref(), Some("https://example.com"));A field is Some only when the matching part is present and non-empty. An
input with no usable parts returns the default, where every field is None.
use author_parser::{parse, Author};
assert_eq!(parse(""), Author::default());
assert_eq!(parse(" "), Author::default());Any subset of the parts works. The name, when present, leads; email and url may appear in either order:
Name
Name <email> (url)
Name <email>(url)
Name<email> (url)
Name<email>(url)
Name (url) <email>
Name (url)<email>
Name(url) <email>
Name(url)<email>
Name (url)
Name(url)
Name <email>
Name<email>
<email> (url)
<email>(url)
(url) <email>
(url)<email>
<email>
(url)
- A bracket token is
<...>for email or(...)for url. The leading bracket decides the field, so mismatched pairs like<x)still read as email. - Empty bracket tokens (
<>or()) are dropped. - The grammar holds one optional first bracket token plus a repeated group that
consumes adjacent tokens with no whitespace between them. Only the first
bracket token and the final token in the adjacent trailing run are read.
Middle tokens in a mixed run can be dropped, so
<a>(u)<b>reads asemail = "b"andurl = None. Same-kind adjacent tokens still keep the final value, so(u1)(u2)(u3)reads asurl = "u3". A token set off by whitespace past the single boundary the grammar allows breaks the match, so(u1) (u2) (u3)returns the default. - When two tokens set the same field, the second wins.
- The presence gate uses an ASCII word character check. A string with no ASCII word character returns the default, even when it holds non-ASCII letters. A name that carries an ASCII word character keeps its full text.
Licensed under the MIT license.