doc tweaks

This commit is contained in:
Rob Ede 2021-08-31 04:47:27 +01:00
parent 049b925886
commit 30edfb921c
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 18 additions and 14 deletions

View File

@ -31,13 +31,13 @@ const REGEX_FLAGS: &str = "(?s-m)";
/// # Pattern Format and Matching Behavior /// # Pattern Format and Matching Behavior
/// ///
/// Resource pattern is defined as a string of zero or more _segments_ where each segment is /// Resource pattern is defined as a string of zero or more _segments_ where each segment is
/// preceeded by a slash `/`. /// preceded by a slash `/`.
/// ///
/// This means that pattern string __must__ either be empty or begin with a slash (`/`). /// This means that pattern string __must__ either be empty or begin with a slash (`/`).
/// This also implies that a trailing slash in pattern defines an empty segment. /// This also implies that a trailing slash in pattern defines an empty segment.
/// For example, the pattern `"/user/"` has two segments: `["user", ""]` /// For example, the pattern `"/user/"` has two segments: `["user", ""]`
/// ///
/// A key point to undertand is that `ResourceDef` matches segments, not strings. /// A key point to underhand is that `ResourceDef` matches segments, not strings.
/// It matches segments individually. /// It matches segments individually.
/// For example, the pattern `/user/` is not considered a prefix for the path `/user/123/456`, /// For example, the pattern `/user/` is not considered a prefix for the path `/user/123/456`,
/// because the second segment doesn't match: `["user", ""]` vs `["user", "123", "456"]`. /// because the second segment doesn't match: `["user", ""]` vs `["user", "123", "456"]`.
@ -279,7 +279,6 @@ impl ResourceDef {
/// ``` /// ```
pub fn new<T: IntoPatterns>(paths: T) -> Self { pub fn new<T: IntoPatterns>(paths: T) -> Self {
profile_method!(new); profile_method!(new);
Self::new2(paths, false) Self::new2(paths, false)
} }
@ -308,7 +307,6 @@ impl ResourceDef {
/// ``` /// ```
pub fn prefix<T: IntoPatterns>(paths: T) -> Self { pub fn prefix<T: IntoPatterns>(paths: T) -> Self {
profile_method!(prefix); profile_method!(prefix);
ResourceDef::new2(paths, true) ResourceDef::new2(paths, true)
} }
@ -334,7 +332,6 @@ impl ResourceDef {
/// ``` /// ```
pub fn root_prefix(path: &str) -> Self { pub fn root_prefix(path: &str) -> Self {
profile_method!(root_prefix); profile_method!(root_prefix);
ResourceDef::prefix(insert_slash(path).into_owned()) ResourceDef::prefix(insert_slash(path).into_owned())
} }
@ -418,9 +415,9 @@ impl ResourceDef {
/// Returns the pattern string that generated the resource definition. /// Returns the pattern string that generated the resource definition.
/// ///
/// If definition is constructed with multiple patterns, first pattern is returned. /// If definition is constructed with multiple patterns, the first pattern is returned. To get
/// If it is zero-length then `None`. /// all patterns, use [`patterns_iter`][Self::pattern_iter]. If resource has 0 patterns,
/// See [`patterns_iter`][Self::pattern_iter]. /// returns `None`.
/// ///
/// # Examples /// # Examples
/// ``` /// ```
@ -813,7 +810,8 @@ impl ResourceDef {
/// ///
/// Returns `true` on success. /// Returns `true` on success.
/// ///
/// For multi-pattern resources, first pattern is used. /// For multi-pattern resources, the first pattern is used under the assumption that it would be
/// equivalent to any other choice.
/// ///
/// # Examples /// # Examples
/// ``` /// ```
@ -838,7 +836,8 @@ impl ResourceDef {
/// ///
/// Returns `true` on success. /// Returns `true` on success.
/// ///
/// For multi-pattern resources, first pattern is used. /// For multi-pattern resources, the first pattern is used under the assumption that it would be
/// equivalent to any other choice.
/// ///
/// # Examples /// # Examples
/// ``` /// ```
@ -873,14 +872,19 @@ impl ResourceDef {
let rem = path.strip_prefix(pattern)?; let rem = path.strip_prefix(pattern)?;
match self.is_prefix { match self.is_prefix {
// resource is not a prefix so an exact match is needed
false if rem.is_empty() => Some(pattern.len()), false if rem.is_empty() => Some(pattern.len()),
// resource is a prefix so rem should start with a path delimiter
true if rem.is_empty() || rem.starts_with('/') => Some(pattern.len()), true if rem.is_empty() || rem.starts_with('/') => Some(pattern.len()),
// otherwise, no match
_ => None, _ => None,
} }
} }
fn new2<T: IntoPatterns>(paths: T, is_prefix: bool) -> Self { fn new2<T: IntoPatterns>(paths: T, is_prefix: bool) -> Self {
profile_method!(new); profile_method!(new2);
let patterns = paths.patterns(); let patterns = paths.patterns();
let (pat_type, segments) = match &patterns { let (pat_type, segments) = match &patterns {
@ -899,7 +903,7 @@ impl ResourceDef {
let mut segments = None; let mut segments = None;
for pattern in patterns { for pattern in patterns {
match ResourceDef::parse(&pattern, is_prefix, true) { match ResourceDef::parse(pattern, is_prefix, true) {
(PatternType::Dynamic(re, names), segs) => { (PatternType::Dynamic(re, names), segs) => {
re_set.push(re.as_str().to_owned()); re_set.push(re.as_str().to_owned());
pattern_data.push((re, names)); pattern_data.push((re, names));
@ -910,7 +914,7 @@ impl ResourceDef {
} }
let pattern_re_set = RegexSet::new(re_set).unwrap(); let pattern_re_set = RegexSet::new(re_set).unwrap();
let segments = segments.unwrap_or_else(|| Vec::new()); let segments = segments.unwrap_or_else(Vec::new);
( (
PatternType::DynamicSet(pattern_re_set, pattern_data), PatternType::DynamicSet(pattern_re_set, pattern_data),
@ -1349,7 +1353,7 @@ mod tests {
assert_eq!(re.find_match("/u/abc"), Some(6)); assert_eq!(re.find_match("/u/abc"), Some(6));
assert_eq!(re.find_match("/u/abc/123"), Some(6)); assert_eq!(re.find_match("/u/abc/123"), Some(6));
assert_eq!(re.find_match("s/user/profile"), None); assert_eq!(re.find_match("/s/user/profile"), None);
assert_eq!(re.find_match("/123"), Some(4)); assert_eq!(re.find_match("/123"), Some(4));
assert_eq!(re.find_match("/123/456"), Some(4)); assert_eq!(re.find_match("/123/456"), Some(4));