forked from apezord/ord-dogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutgoing.rs
70 lines (61 loc) Β· 1.57 KB
/
outgoing.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
use super::*;
#[derive(Debug, PartialEq)]
pub(crate) enum Outgoing {
Amount(Amount),
InscriptionId(InscriptionId),
SatPoint(SatPoint),
}
impl FromStr for Outgoing {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if s.contains(':') {
Self::SatPoint(s.parse()?)
} else if s.len() >= 66 {
Self::InscriptionId(s.parse()?)
} else if s.contains(' ') {
Self::Amount(s.parse()?)
} else if let Some(i) = s.find(|c: char| c.is_alphabetic()) {
let mut s = s.to_owned();
s.insert(i, ' ');
Self::Amount(s.parse()?)
} else {
Self::Amount(s.parse()?)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse() {
assert_eq!(
"0000000000000000000000000000000000000000000000000000000000000000i0"
.parse::<Outgoing>()
.unwrap(),
Outgoing::InscriptionId(
"0000000000000000000000000000000000000000000000000000000000000000i0"
.parse()
.unwrap()
),
);
assert_eq!(
"0000000000000000000000000000000000000000000000000000000000000000:0:0"
.parse::<Outgoing>()
.unwrap(),
Outgoing::SatPoint(
"0000000000000000000000000000000000000000000000000000000000000000:0:0"
.parse()
.unwrap()
),
);
assert_eq!(
"0 sat".parse::<Outgoing>().unwrap(),
Outgoing::Amount("0 sat".parse().unwrap()),
);
assert_eq!(
"0sat".parse::<Outgoing>().unwrap(),
Outgoing::Amount("0 sat".parse().unwrap()),
);
assert!("0".parse::<Outgoing>().is_err());
}
}