forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 21
/
name.rs
45 lines (35 loc) Β· 900 Bytes
/
name.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
use super::*;
#[derive(StructOpt)]
pub(crate) struct Name {
name: String,
}
impl Name {
pub(crate) fn run(self) -> Result {
if self.name.is_empty() || self.name.chars().any(|c| !('a'..='z').contains(&c)) {
return Err("Invalid name".into());
}
let mut min = 0;
let mut max = 2099999997690000;
let mut guess = max / 2;
loop {
log::info!("min max guess: {} {} {}", min, max, guess);
let name = Ordinal(guess).name();
match name
.len()
.cmp(&self.name.len())
.then(name.deref().cmp(&self.name))
.reverse()
{
Ordering::Less => min = guess + 1,
Ordering::Equal => break,
Ordering::Greater => max = guess,
}
if max - min == 0 {
return Err("Name out of range".into());
}
guess = min + (max - min) / 2;
}
println!("{}", guess);
Ok(())
}
}