-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathreformat-id.rs
More file actions
73 lines (63 loc) · 1.86 KB
/
reformat-id.rs
File metadata and controls
73 lines (63 loc) · 1.86 KB
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Parse an [`Id`] as an integer, and reformat as a path
use kas_core::Id;
fn main() {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: {} CODE", args.next().unwrap());
eprintln!("where CODE is #HEX_PATH or DECIMAL");
return;
}
let s = args.skip(1).next().unwrap();
if s.starts_with("#") {
print!("[");
let mut first = true;
let mut i = 1;
let mut n: usize = 0;
while i < s.len() {
let b = match u8::from_str_radix(&s[i..i + 1], 16) {
Ok(b) => b,
Err(err) => {
println!();
eprintln!("Parse error: {err}");
return;
}
};
i += 1;
n |= (b as usize) & 7;
if b & 8 != 0 {
n <<= 3;
continue;
}
if !first {
print!(", ");
}
first = false;
print!("{n}");
n = 0;
}
println!("]");
return;
}
let n: u64 = match s.parse() {
Ok(n) => n,
Err(e) => {
eprintln!("{e}");
return;
}
};
if let Some(id) = Id::bits_from_u64(n) {
let path: Vec<usize> = id.iter().collect();
println!("{id}: {path:?}");
} else {
eprintln!("Failed to convert {n}");
if n & 3 == 2 {
eprintln!(
"Note: long paths are stack allocated and cannot be reconstructed outside of the constructing thread"
);
}
}
}