open_ambient/
lib.rs

1//! Macros for opening files and directories using ambient authority, with
2//! paths known at compile time.
3
4/// Opens a file in read-only mode with ambient authority, using a path known
5/// at compile time.
6///
7/// # Examples
8///
9/// ```rust
10/// use open_ambient::open_ambient_file;
11///
12/// let file = open_ambient_file!("Cargo.toml").unwrap();
13/// ```
14#[macro_export]
15macro_rules! open_ambient_file {
16    ($path:literal) => {
17        ::cap_std::fs::File::open_ambient(
18            $path,
19            ::cap_std::ambient_authority_known_at_compile_time(),
20        )
21    };
22}
23
24/// Opens a file with ambient authority with the specified options, but using a
25/// path known at compile time.
26///
27/// # Examples
28///
29/// ```rust
30/// use cap_std::fs::OpenOptions;
31/// use open_ambient::open_ambient_file_with;
32///
33/// let file = open_ambient_file_with!("Cargo.toml", &OpenOptions::new().read(true)).unwrap();
34/// ```
35#[macro_export]
36macro_rules! open_ambient_file_with {
37    ($path:literal, $opts:expr) => {
38        ::cap_std::fs::File::open_ambient_with(
39            $path,
40            $opts,
41            ::cap_std::ambient_authority_known_at_compile_time(),
42        )
43    };
44}
45
46/// Opens a directory with ambient authority, using a path known at compile
47/// time.
48///
49/// # Examples
50///
51/// ```rust
52/// use open_ambient::open_ambient_dir;
53///
54/// let dir = open_ambient_dir!("src").unwrap();
55/// ```
56#[macro_export]
57macro_rules! open_ambient_dir {
58    ($path:literal) => {
59        ::cap_std::fs::Dir::open_ambient_dir(
60            $path,
61            ::cap_std::ambient_authority_known_at_compile_time(),
62        )
63    };
64}