MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / create

Function create

crates/opencode-core/src/id.rs:54–85  ·  view source on GitHub ↗
(prefix: Prefix, descending: bool, timestamp: Option<u64>)

Source from the content-addressed store, hash-verified

52}
53
54pub fn create(prefix: Prefix, descending: bool, timestamp: Option<u64>) -> String {
55 let current_timestamp = timestamp.unwrap_or_else(|| {
56 std::time::SystemTime::now()
57 .duration_since(std::time::UNIX_EPOCH)
58 .unwrap()
59 .as_millis() as u64
60 });
61
62 let last = LAST_TIMESTAMP.load(Ordering::Relaxed);
63 if current_timestamp != last {
64 LAST_TIMESTAMP.store(current_timestamp, Ordering::Relaxed);
65 let mut counter = COUNTER.lock().unwrap();
66 *counter = 0;
67 }
68
69 let counter_val = get_counter();
70 let mut now = u64::from(current_timestamp) * 0x1000 + u64::from(counter_val);
71
72 if descending {
73 now = !now;
74 }
75
76 let mut time_bytes = [0u8; 6];
77 for i in 0..6 {
78 time_bytes[i] = ((now >> (40 - 8 * i)) & 0xff) as u8;
79 }
80
81 let hex_time = hex::encode(time_bytes);
82 let random_part = random_base62(LENGTH - 12);
83
84 format!("{}_{}{}", prefix.as_str(), hex_time, random_part)
85}
86
87pub fn timestamp(id: &str) -> Option<u64> {
88 let parts: Vec<&str> = id.split('_').collect();

Callers 10

test_create_idFunction · 0.85
test_validate_prefixFunction · 0.85
defaultMethod · 0.85
loop_innerMethod · 0.85
append_delta_partMethod · 0.85
process_streamMethod · 0.85
processMethod · 0.85
createMethod · 0.85
save_truncatedMethod · 0.85

Calls 3

get_counterFunction · 0.85
random_base62Function · 0.85
loadMethod · 0.45

Tested by 3

test_create_idFunction · 0.68
test_validate_prefixFunction · 0.68