opentitanlib/util/
mod.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5pub mod bigint;
6pub mod bitbang;
7pub mod file;
8pub mod fs;
9pub mod hexdump;
10pub mod num_de;
11pub mod parse_int;
12pub mod present;
13pub mod printer;
14pub mod raw_tty;
15mod regex;
16pub mod rom_detect;
17pub mod runtime;
18pub mod serde;
19pub mod serializable_error;
20pub mod status;
21pub mod testing;
22pub mod usb;
23pub mod usr_access;
24pub mod vcd;
25pub mod vmem;
26pub mod voltage;
27
28pub use ot_hal::util::bitfield;
29
30pub use runtime::runtime;
31
32/// The `collection` macro provides syntax for hash and set literals.
33#[macro_export]
34macro_rules! collection {
35    // map-like
36    ($($k:expr => $v:expr),* $(,)?) => {{
37        use std::iter::{Iterator, IntoIterator};
38        Iterator::collect(IntoIterator::into_iter([$(($k, $v),)*]))
39    }};
40
41    // set-like
42    ($($v:expr),* $(,)?) => {{
43        use std::iter::{Iterator, IntoIterator};
44        Iterator::collect(IntoIterator::into_iter([$($v),*]))
45    }};
46}
47
48/// The `testdata` function can be used in tests to reference testdata directories.
49#[cfg(test)]
50pub fn testdata(test: &str) -> std::path::PathBuf {
51    let mut path: std::path::PathBuf = std::env::var_os("TESTDATA").unwrap().into();
52    // TESTDATA points an arbitrary test, remove two levels to get the directory.
53    path.pop();
54    path.pop();
55    path.push(test);
56    path
57}
58
59/// Create a filename in a temporary directory.
60///
61/// When running under bazel, the filename will exist in the test's undeclared outputs directory.
62pub fn tmpfilename(name: &str) -> String {
63    let mut dir = match std::env::var("TEST_UNDECLARED_OUTPUTS_DIR") {
64        Ok(name) => std::path::PathBuf::from(name),
65        Err(_) => std::env::temp_dir(),
66    };
67    dir.push(name);
68    dir.to_str().unwrap().into()
69}