|
1 | | -//! This crate provides common functionality common functionality for Roc to interface with `std::env` |
2 | 1 | pub mod arg; |
3 | | - |
4 | | -use roc_std::{roc_refcounted_noop_impl, RocList, RocRefcounted, RocResult, RocStr}; |
5 | | -use std::borrow::Borrow; |
6 | | -use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
7 | | - |
8 | | -pub fn env_dict() -> RocList<(RocStr, RocStr)> { |
9 | | - // TODO: can we be more efficient about reusing the String's memory for RocStr? |
10 | | - std::env::vars_os() |
11 | | - .map(|(key, val)| { |
12 | | - ( |
13 | | - RocStr::from(key.to_string_lossy().borrow()), |
14 | | - RocStr::from(val.to_string_lossy().borrow()), |
15 | | - ) |
16 | | - }) |
17 | | - .collect() |
18 | | -} |
19 | | - |
20 | | -pub fn temp_dir() -> RocList<u8> { |
21 | | - let path_os_string_bytes = std::env::temp_dir().into_os_string().into_encoded_bytes(); |
22 | | - |
23 | | - RocList::from(path_os_string_bytes.as_slice()) |
24 | | -} |
25 | | - |
26 | | -pub fn env_var(roc_str: &RocStr) -> RocResult<RocStr, ()> { |
27 | | - // TODO: can we be more efficient about reusing the String's memory for RocStr? |
28 | | - match std::env::var_os(roc_str.as_str()) { |
29 | | - Some(os_str) => RocResult::ok(RocStr::from(os_str.to_string_lossy().borrow())), |
30 | | - None => RocResult::err(()), |
31 | | - } |
32 | | -} |
33 | | - |
34 | | -pub fn get_locale() -> RocResult<RocStr, ()> { |
35 | | - sys_locale::get_locale().map_or_else( |
36 | | - || RocResult::err(()), |
37 | | - |locale| RocResult::ok(locale.to_string().as_str().into()), |
38 | | - ) |
39 | | -} |
40 | | - |
41 | | -pub fn get_locales() -> RocList<RocStr> { |
42 | | - const DEFAULT_MAX_LOCALES: usize = 10; |
43 | | - let locales = sys_locale::get_locales(); |
44 | | - let mut roc_locales = RocList::with_capacity(DEFAULT_MAX_LOCALES); |
45 | | - for l in locales { |
46 | | - roc_locales.push(l.to_string().as_str().into()); |
47 | | - } |
48 | | - roc_locales |
49 | | -} |
50 | | - |
51 | | -#[derive(Debug)] |
52 | | -#[repr(C)] |
53 | | -pub struct ReturnArchOS { |
54 | | - pub arch: RocStr, |
55 | | - pub os: RocStr, |
56 | | -} |
57 | | - |
58 | | -roc_refcounted_noop_impl!(ReturnArchOS); |
59 | | - |
60 | | -pub fn current_arch_os() -> ReturnArchOS { |
61 | | - ReturnArchOS { |
62 | | - arch: std::env::consts::ARCH.into(), |
63 | | - os: std::env::consts::OS.into(), |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -pub fn posix_time() -> roc_std::U128 { |
68 | | - // TODO in future may be able to avoid this panic by using C APIs |
69 | | - let since_epoch = SystemTime::now() |
70 | | - .duration_since(UNIX_EPOCH) |
71 | | - .expect("time went backwards"); |
72 | | - |
73 | | - roc_std::U128::from(since_epoch.as_nanos()) |
74 | | -} |
75 | | - |
76 | | -pub fn sleep_millis(milliseconds: u64) { |
77 | | - let duration = Duration::from_millis(milliseconds); |
78 | | - std::thread::sleep(duration); |
79 | | -} |
0 commit comments