]> git.sesse.net Git - bcachefs-tools-debian/blob - rust-src/src/lib.rs
New upstream release
[bcachefs-tools-debian] / rust-src / src / lib.rs
1 use clap::Subcommand;
2
3 pub mod key;
4 pub mod logger;
5 pub mod cmd_mount;
6 pub mod cmd_list;
7 pub mod cmd_completions;
8
9 #[derive(clap::Parser, Debug)]
10 #[command(name = "bcachefs")]
11 pub struct Cli {
12     #[command(subcommand)]
13     subcommands: Subcommands,
14 }
15
16 #[derive(Subcommand, Debug)]
17 enum Subcommands {
18     List(cmd_list::Cli),
19     Mount(cmd_mount::Cli),
20     Completions(cmd_completions::Cli),
21 }
22
23 #[macro_export]
24 macro_rules! c_str {
25     ($lit:expr) => {
26         unsafe {
27             std::ffi::CStr::from_ptr(concat!($lit, "\0").as_ptr() as *const std::os::raw::c_char)
28                 .to_bytes_with_nul()
29                 .as_ptr() as *const std::os::raw::c_char
30         }
31     };
32 }
33
34 #[macro_export]
35 macro_rules! transform_c_args {
36     ($var:ident, $argc:expr, $argv:expr) => {
37         // TODO: `OsStr::from_bytes` only exists on *nix
38         use ::std::os::unix::ffi::OsStrExt;
39         let $var: Vec<_> = (0..$argc)
40         .map(|i| unsafe { ::std::ffi::CStr::from_ptr(*$argv.add(i as usize)) })
41         .map(|i| ::std::ffi::OsStr::from_bytes(i.to_bytes()))
42         .collect();
43     };
44 }
45
46 #[derive(Debug)]
47 struct ErrnoError(errno::Errno);
48 impl std::fmt::Display for ErrnoError {
49     fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
50         self.0.fmt(f)
51     }
52 }
53 impl std::error::Error for ErrnoError {}