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;
15pub mod rom_detect;
16pub mod runtime;
17pub mod serde;
18pub mod serializable_error;
19pub mod status;
20pub mod testing;
21pub mod usb;
22pub mod usr_access;
23pub mod vcd;
24pub mod vmem;
25pub mod voltage;
26
27pub use ot_hal::util::bitfield;
28
29pub use runtime::runtime;
30
31/// The `collection` macro provides syntax for hash and set literals.
32#[macro_export]
33macro_rules! collection {
34    // map-like
35    ($($k:expr => $v:expr),* $(,)?) => {{
36        use std::iter::{Iterator, IntoIterator};
37        Iterator::collect(IntoIterator::into_iter([$(($k, $v),)*]))
38    }};
39
40    // set-like
41    ($($v:expr),* $(,)?) => {{
42        use std::iter::{Iterator, IntoIterator};
43        Iterator::collect(IntoIterator::into_iter([$($v),*]))
44    }};
45}
46
47/// The `testdata` function can be used in tests to reference testdata directories.
48#[cfg(test)]
49pub fn testdata(test: &str) -> std::path::PathBuf {
50    let mut path: std::path::PathBuf = std::env::var_os("TESTDATA").unwrap().into();
51    // TESTDATA points an arbitrary test, remove two levels to get the directory.
52    path.pop();
53    path.pop();
54    path.push(test);
55    path
56}
57
58/// Create a filename in a temporary directory.
59///
60/// When running under bazel, the filename will exist in the test's undeclared outputs directory.
61pub fn tmpfilename(name: &str) -> String {
62    let mut dir = match std::env::var("TEST_UNDECLARED_OUTPUTS_DIR") {
63        Ok(name) => std::path::PathBuf::from(name),
64        Err(_) => std::env::temp_dir(),
65    };
66    dir.push(name);
67    dir.to_str().unwrap().into()
68}