-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.rs
169 lines (149 loc) · 5.03 KB
/
registry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::collections::{HashMap, HashSet};
use clap::Parser;
use typomania::{
checks::{Bitflips, Omitted, SwappedWords, Typos},
AuthorSet, Corpus, Harness, Package,
};
#[derive(Debug, Parser)]
struct Opt {
/// Valid characters in package names
#[arg(
long,
default_value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
)]
alphabet: String,
/// Package names to consider top (or popular) packages, delimited by commas
#[arg(short, long, value_name = "PACKAGE", value_delimiter = ',')]
top_packages: Vec<String>,
/// Packages to check against the top packages
#[arg(value_name = "PACKAGE")]
packages: Vec<String>,
}
fn main() -> typomania::Result<()> {
let opt = Opt::parse();
// Build a corpus of the top packages that we want to match against.
let corpus = TopPackages::from(opt.top_packages);
// Build a harness that uses the checks built into typomania.
let harness = Harness::builder()
.with_check(Bitflips::new(
&opt.alphabet,
corpus.0.keys().map(|s| s.as_str()),
))
.with_check(Omitted::new(&opt.alphabet))
.with_check(SwappedWords::new("-_."))
.with_check(Typos::new(TYPOS.iter().map(|(c, typos)| {
(*c, typos.iter().map(|ss| ss.to_string()).collect())
})))
.build(corpus);
// Actually check the given packages.
for (name, squats) in harness
.check(opt.packages.into_iter().map(|name| {
let package: Box<dyn Package> = Box::new(FakePackage::new(&name));
(name, package)
}))?
.into_iter()
{
println!("{name}: {squats:?}");
}
Ok(())
}
struct TopPackages(HashMap<String, FakePackage>);
impl From<Vec<String>> for TopPackages {
fn from(value: Vec<String>) -> Self {
Self(
value
.into_iter()
.map(|name| {
let package = FakePackage::new(&name);
(name, package)
})
.collect(),
)
}
}
impl Corpus for TopPackages {
fn contains_name(&self, name: &str) -> typomania::Result<bool> {
Ok(self.0.contains_key(name))
}
fn get(&self, name: &str) -> typomania::Result<Option<&dyn typomania::Package>> {
Ok(self
.0
.get(name)
.map(|package| package as &dyn typomania::Package))
}
}
struct FakePackage {
authors: HashSet<String>,
description: String,
}
impl FakePackage {
fn new(name: &str) -> Self {
Self {
// We'll set up a fake author based on the name so that there's no possibility of
// having a match excluded because of a shared author.
authors: [format!("{name} author <{name}@example.com>")]
.into_iter()
.collect(),
description: format!("{name} is a package that does {name}"),
}
}
}
impl Package for FakePackage {
fn authors(&self) -> &dyn AuthorSet {
self
}
fn description(&self) -> Option<&str> {
Some(&self.description)
}
fn shared_authors(&self, other: &dyn AuthorSet) -> bool {
self.authors.iter().any(|author| other.contains(author))
}
}
impl AuthorSet for FakePackage {
fn contains(&self, author: &str) -> bool {
self.authors.contains(author)
}
}
// This is based on a pre-existing list we've used with crates.io for "easily confused characters".
// (I'm not really sure that I consider all of these easily confused, but it's better than nothing.)
static TYPOS: &[(char, &[&str])] = &[
('1', &["2", "q", "i", "l"]),
('2', &["1", "q", "w", "3"]),
('3', &["2", "w", "e", "4"]),
('4', &["3", "e", "r", "5"]),
('5', &["4", "r", "t", "6", "s"]),
('6', &["5", "t", "y", "7"]),
('7', &["6", "y", "u", "8"]),
('8', &["7", "u", "i", "9"]),
('9', &["8", "i", "o", "0"]),
('0', &["9", "o", "p", "-"]),
('-', &["_", "0", "p", ".", ""]),
('_', &["-", "0", "p", ".", ""]),
('q', &["1", "2", "w", "a"]),
('w', &["2", "3", "e", "s", "a", "q", "vv"]),
('e', &["3", "4", "r", "d", "s", "w"]),
('r', &["4", "5", "t", "f", "d", "e"]),
('t', &["5", "6", "y", "g", "f", "r"]),
('y', &["6", "7", "u", "h", "t", "i"]),
('u', &["7", "8", "i", "j", "y", "v"]),
('i', &["1", "8", "9", "o", "l", "k", "j", "u", "y"]),
('o', &["9", "0", "p", "l", "i"]),
('p', &["0", "-", "o"]),
('a', &["q", "w", "s", "z"]),
('s', &["w", "d", "x", "z", "a", "5"]),
('d', &["e", "r", "f", "c", "x", "s"]),
('f', &["r", "g", "v", "c", "d"]),
('g', &["t", "h", "b", "v", "f"]),
('h', &["y", "j", "n", "b", "g"]),
('j', &["u", "i", "k", "m", "n", "h"]),
('k', &["i", "o", "l", "m", "j"]),
('l', &["i", "o", "p", "k", "1"]),
('z', &["a", "s", "x"]),
('x', &["z", "s", "d", "c"]),
('c', &["x", "d", "f", "v"]),
('v', &["c", "f", "g", "b", "u"]),
('b', &["v", "g", "h", "n"]),
('n', &["b", "h", "j", "m"]),
('m', &["n", "j", "k", "rn"]),
('.', &["-", "_", ""]),
];