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