blob: 39ca183841dacc8a1a89a522b60ab72f954b48ef (
plain)
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
|
#[cfg(not(feature = "std"))]
use arraystring::{
ArrayString,
typenum::{U32, U160},
};
#[cfg(feature = "std")]
pub type NameString = String;
#[cfg(feature = "std")]
pub fn name_string_from_slice(s: &[u8]) -> NameString {
match String::from_utf8(s.to_vec()) {
Ok(s) => s,
Err(_) => "Unable to create string".to_string(),
}
}
#[cfg(not(feature = "std"))]
pub type NameString = ArrayString<U32>;
#[cfg(not(feature = "std"))]
pub fn name_string_from_slice(s: &[u8]) -> NameString {
NameString::from_str_truncate(core::str::from_utf8(s).unwrap())
}
#[cfg(feature = "std")]
pub type MessageString = String;
#[cfg(not(feature = "std"))]
pub type MessageString = ArrayString<U160>;
#[cfg(feature = "std")]
pub fn message_string_from_slice(s: &[u8]) -> MessageString {
let mut split = s.split(|c| *c == 0);
if let Some(s) = split.next() {
match String::from_utf8(s.to_vec()) {
Ok(s) => s.trim().to_string(),
Err(_) => "Unable to create string".to_string(),
}
} else {
"".to_string()
}
}
#[cfg(not(feature = "std"))]
pub fn message_string_from_slice(s: &[u8]) -> MessageString {
MessageString::from_str_truncate(core::str::from_utf8(s).unwrap())
}
#[cfg(feature = "std")]
pub type PasswordString = String;
// I believe that a password can only be 141 characters,
// but given it's close enough to 160, let's just round
// up so it's the same as MessageString.
#[cfg(not(feature = "std"))]
pub type PasswordString = ArrayString<U160>;
#[cfg(feature = "std")]
pub fn password_string_from_slice(s: &[u8]) -> PasswordString {
match String::from_utf8(s.to_vec()) {
Ok(s) => s,
Err(_) => "".to_string(),
}
}
#[cfg(not(feature = "std"))]
pub fn password_string_from_slice(s: &[u8]) -> PasswordString {
PasswordString::from_str_truncate(core::str::from_utf8(s).unwrap())
}
|