Crate rk_utils

source ·
Expand description

§rk-utils

ci result

A collection of utility functions and data structures for rust.

§Features

  • str: String utilities
    • is_quoted, substring, unquote, url_to_nodes, ensure_prefix, ensure_suffix, drop_prefix, drop_suffix, join_path_segment, join_path_segments
  • topo_sort: Topological sorting
  • trie: Trie data structure for longest matching a path of nodes

§Usage

Add this to your Cargo.toml:

[dependencies]
rk-utils = "0.2"

§Examples

use rk_utils::StringUtil;

let s = "'Hello, World!'";
assert_eq!(s.is_quoted(), true);

let s = "'\\'Hello, World!\\''";
assert_eq!(s.unquote(true, None), "'Hello, World!'");

let s = "'Hello, World!'";
assert_eq!(s.substring(1, -1), "Hello, World!");
use rk_utils::topo_sort;
use std::collections::{ HashMap, HashSet };

let mut deps = HashMap::new();
deps.insert("b".to_string(), HashSet::from(["a".to_string()]));
deps.insert("c".to_string(), HashSet::from(["b".to_string()]));

let sorted = topo_sort(&deps).unwrap();
assert_eq!(sorted, ["a", "b", "c"]);
use crate::str::StringUtil;
use crate::trie::Trie;

let mut trie = Trie::new();

let route1 = "/cloud/instance/".url_to_nodes();
let route2 = "/cloud/".url_to_nodes();
let route3 = "/builder/instance".url_to_nodes();
let route4 = "/builder".url_to_nodes();
let route5 = "/".url_to_nodes();

trie.insert(route1, 1);
trie.insert(route2, 2);
trie.insert(route3, 3);
trie.insert(route4, 4);
trie.insert(route5, 5);

let input1 = "/cloud/instance/xxx".url_to_nodes();
assert_eq!(trie.find_longest_match(input1), Some(&1));

let input2 = "/cloud/xxx".url_to_nodes();
assert_eq!(trie.find_longest_match(input2), Some(&2));

let input3 = "/builder/instance/".url_to_nodes();
assert_eq!(trie.find_longest_match(input3), Some(&3));

let input4 = "/fjeao".url_to_nodes();
assert_eq!(trie.find_longest_match(input4), Some(&5));

§License

MIT

Macros§

Structs§

  • Trie is a data structure that stores a set of strings. It is used to find the longest match of a string.

Traits§

  • StringUtil provides utility methods for string manipulation.

Functions§

  • topo_sort sorts the dependencies topologically. It returns a Vec of the sorted nodes. If a cyclic reference is detected, it returns an error.

Type Aliases§

  • DepGraph is a dependency graph. It is represented as a HashMap where the key is the node and the value is a HashSet of its dependencies.