tmux-copyrat/src/textbuf/regexes.rs

49 lines
1.7 KiB
Rust
Raw Normal View History

use crate::error;
pub(super) const EXCLUDE_PATTERNS: [(&str, &str); 1] =
2020-05-31 00:17:51 +02:00
[("ansi_colors", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")];
2020-05-27 10:04:42 +02:00
2020-06-04 07:30:10 +02:00
/// Holds all the regex patterns that are currently supported.
///
/// The email address was obtained at https://www.regular-expressions.info/email.html.
/// Others were obtained from Ferran Basora.
pub(super) const PATTERNS: [(&str, &str); 16] = [
("markdown-url", r"\[[^]]*\]\(([^)]+)\)"),
2020-05-27 10:04:42 +02:00
(
"url",
r"((https?://|git@|git://|ssh://|ftp://|file:///)[^ \(\)\[\]\{\}]+)",
2020-05-27 10:04:42 +02:00
),
2020-06-04 07:16:07 +02:00
("email", r"\b[A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,}\b"),
("diff-a", r"--- a/([^ ]+)"),
("diff-b", r"\+\+\+ b/([^ ]+)"),
2020-05-27 10:04:42 +02:00
("docker", r"sha256:([0-9a-f]{64})"),
("path", r"(([.\w\-@~]+)?(/[.\w\-@]+)+)"),
("hexcolor", r"#[0-9a-fA-F]{6}"),
2020-05-27 10:04:42 +02:00
(
"uuid",
2020-05-27 10:04:42 +02:00
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
),
2021-03-17 10:12:57 +01:00
(
"version",
r"(v?\d{1,4}\.\d{1,4}(\.\d{1,4})?(-(alpha|beta|rc)(\.\d)?)?)[^.0-9s]",
),
2020-05-27 10:04:42 +02:00
("ipfs", r"Qm[0-9a-zA-Z]{44}"),
("sha", r"[0-9a-f]{7,40}"),
2020-06-04 07:30:10 +02:00
("ipv4", r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"),
2020-05-27 10:04:42 +02:00
("ipv6", r"[A-f0-9:]+:+[A-f0-9:]+[%\w\d]+"),
2021-03-23 07:25:02 +01:00
("pointer-address", r"0x[0-9a-fA-F]+"),
("digits", r"[0-9]{4,}"),
2020-05-27 10:04:42 +02:00
];
/// Type-safe string Pattern Name (newtype).
#[derive(Debug)]
pub struct NamedPattern(pub String, pub String);
/// Parse a name string into `NamedPattern`, used during CLI parsing.
pub(crate) fn parse_pattern_name(src: &str) -> Result<NamedPattern, error::ParseError> {
match PATTERNS.iter().find(|&(name, _pattern)| name == &src) {
Some((name, pattern)) => Ok(NamedPattern(name.to_string(), pattern.to_string())),
None => Err(error::ParseError::UnknownPatternName),
}
}