A light utility helping with unix mode representation, with strong types to avoid misusing constants.
The Mode struct implements Display
and prints as "rwxrwxrwx"
In Cargo.toml:
umask = "'2.0"
```rust use umask::*;
// You can build from a number: asserteq!("rw-r--r--", Mode::from(0b110100100).tostring()); asserteq!("rw-r--r--", Mode::from(0o644).tostring());
// You may use |
to combine class permissions:
let mu = Mode::from(0o640);
let mo = Mode::from(0o044);
asserteq!("rw-r--r--", (mu | mo).tostring());
asserteq!("---r-----", (mu & mo).tostring());
// You can use more semantic constructs: let m = Mode::all() .without(ALLEXEC); asserteq!("rw-rw-rw-", m.tostring()); let mut m = Mode::new() .withclassperm(ALL, READ) .withclassperm(USER, WRITE); asserteq!("rw-r--r--", m.to_string()); // (semantic functions can be used in const declarations)
// Or m |= ALLEXEC; asserteq!("rwxr-xr-x", m.tostring()); let m = ALLREAD | USERWRITE; asserteq!("rw-r--r--", m.to_string());
// Displaying the mode can be done with the Display
// implementation but also bit per bit for more control
asserteq!(
m.tostring().chars().next().unwrap(), // first char: 'r' or '-'
if m.has(USER_READ) { 'r' } else { '-' },
);
// The Display
implementation shows the extra permission bits
// (setuid, setgid and sticky):
let mut m = Mode::all()
.withextra(STICKY)
.withextra(SETUID)
.withextra(SETGID);
asserteq!("rwsrwsrwt", m.to_string());
// But you can remove those bits for display if you want the // sometimes more familiar 'x' for execution: asserteq!("rwxrwxrwx", m.withoutanyextra().tostring());
```