graphannis_core/util/
mod.rs

1use crate::errors::{GraphAnnisCoreError, Result};
2use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
3use std::borrow::Cow;
4
5pub mod disk_collections;
6
7#[cfg(test)]
8pub(crate) mod example_graphs;
9
10const QNAME_ENCODE_SET: &AsciiSet = &CONTROLS.add(b' ').add(b':').add(b'%');
11
12pub fn join_qname(ns: &str, name: &str) -> String {
13    let mut result = String::with_capacity(ns.len() + name.len() + 2);
14    if !ns.is_empty() {
15        let encoded_anno_ns: Cow<str> = utf8_percent_encode(ns, QNAME_ENCODE_SET).into();
16        result.push_str(&encoded_anno_ns);
17        result.push_str("::");
18    }
19    let encoded_anno_name: Cow<str> = utf8_percent_encode(name, QNAME_ENCODE_SET).into();
20    result.push_str(&encoded_anno_name);
21    result
22}
23
24pub fn split_qname(qname: &str) -> (Option<&str>, &str) {
25    let sep_pos = qname.find("::");
26    if let Some(sep_pos) = sep_pos {
27        (Some(&qname[..sep_pos]), &qname[sep_pos + 2..])
28    } else {
29        (None, qname)
30    }
31}
32
33pub fn regex_full_match(pattern: &str) -> String {
34    let mut full_match_pattern = String::new();
35    full_match_pattern.push_str(r"\A(");
36    full_match_pattern.push_str(pattern);
37    full_match_pattern.push_str(r")\z");
38
39    full_match_pattern
40}
41
42/// Parse a string as both a `Regex` that can be used for matching and as the
43/// more abstract `Hir` representation that gives as information such as
44/// prefixes for this regular expression.
45pub fn compile_and_parse_regex(pattern: &str) -> Result<(regex::Regex, regex_syntax::hir::Hir)> {
46    let compiled_regex =
47        regex::Regex::new(pattern).map_err(|e| GraphAnnisCoreError::Other(Box::new(e)))?;
48    let parsed_regex = regex_syntax::Parser::new()
49        .parse(pattern)
50        .map_err(|e| GraphAnnisCoreError::Other(Box::new(e)))?;
51    Ok((compiled_regex, parsed_regex))
52}