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