# fourcc-rs
The `fourcc-rs` crate provides types and macros for working with _four-character
codes_ which are 32-bit numbers, usually represented by four ASCII characters.
_FourCCs_ were heavily used on the classic macintosh platform to identify file and
resource types, applications and more.
Check out the [Wikpedia Aritcle on FourCCs](https://en.wikipedia.org/wiki/FourCC) if you want to know more.
```rust
use fourcc::{FourCC, fourcc};
// Use macro to create a new FourCC from a string or number at compile-time
let bndl = fourcc!("BNDL");
assert_eq!(u32::from(bndl), 0x424e444c);
// Use constructor to create a new FourCC from a u32 at run-time
let bndl = FourCC::new(0x424e444c);
assert_eq!(format!("{}", bndl), "'BNDL'".to_string());
// Or do the same thing via the From<u32> implementation
let bndl: FourCC = 0x424e444c.into();
assert_eq!(format!("{}", bndl), "'BNDL'".to_string());
```
One of the defining features is that the [`fourcc`] macro can be used to build
four-character codes at compile time, which allows them to be used in `match` statements.
```rust
use fourcc::{FourCC, fourcc};
// Imagine reading the code from a file
let code = FourCC::new(0x424e444c);
match code {
fourcc!("SIT!") => println!("Found a StuffIt Archive!"),
fourcc!("BNDL") => println!("Found a bundle definition!"),
code => println!("Unknown FourCC {} detected", code),
}
```
## Features
The crate provides the optional features `serde` and `binrw`.
Using the `binrw` feature, allows you to read FourCCs from any [`std::io::Read`] + [`std::io::Seek`]
implementation.
```rust
use fourcc::{FourCC, fourcc};
use binrw::BinReaderExt as _;
use std::io;
let mut reader = io::Cursor::new(b"\x42\x4e\x44\x4c");
let code: FourCC = reader.read_le().unwrap();
assert_eq!(code, fourcc!("BNDL"));
```
Similarly enabling the `serde` feature allows you to read and write FourCCs using
serde.
```rust
use fourcc::{FourCC, fourcc};
let json = "\"BDNL\"";
let code: FourCC = serde_json::from_str(json).unwrap();
assert_eq!(code, fourcc!("BDNL"));
// Invalid strings will be rejected
let json = "\"BUNDLE\"";
let code: Result<FourCC, _> = serde_json::from_str(json);
assert!(code.is_err());
```
## Similar Crates
- [fourcc](https://crates.io/crates/fourcc) _(Deprecated?)_ syntax extension for rust
- [four-char-code](https://crates.io/crates/four-char-code) very similar to this crate, but lacks the ability to use codes in match statements