nitf_rs/
lib.rs

1//! Minimal Crate for reading and manipulating `NITF` files
2
3//! Interface for NITF version 2.1
4//!
5//! Constructing a [Nitf] object parses the file header and segment metadata.
6//! Each segment in contains a `header` field which stores the respective
7//! metadata defined in the file standard. The primary function for constructing a
8//! [Nitf] is [Nitf::from_reader()]
9//! ```no_run
10//! // Read a nitf file and dump metadata to stdout
11//! let mut nitf_file = std::fs::File::open("example.nitf").unwrap();
12//! let nitf = nitf_rs::Nitf::from_reader(&mut nitf_file).unwrap();
13//! println!("{nitf}");
14//! ```
15//!
16//!
17//! Aside from the `nitf_header`, all other segments use a generic [NitfSegment]
18//! to provide metadata and access to the segment data.
19//! ```no_run
20//! // Get the bytes from the first image segment
21//! let mut nitf_file = std::fs::File::open("example.nitf").unwrap();
22//! let nitf = nitf_rs::Nitf::from_reader(&mut nitf_file).unwrap();
23//! let im_seg = &nitf.image_segments[0];
24//! let im_seg_hdr = &im_seg.header;
25//! let im_seg_data = &im_seg.get_data_map(&mut nitf_file).unwrap();
26//! ```
27//!
28//! Most metadata elements are stored in a [NitfField](types::NitfField) structure.
29//! This structure has a `val` which holds on to native value of the field
30//! (i.e., the bytes parsed into a u8, u16, String, enum, etc.), as well as the
31//! length (in bytes) and name of the field.
32//! ```no_run
33//! let mut nitf_file = std::fs::File::open("example.nitf").unwrap();
34//! let nitf = nitf_rs::Nitf::from_reader(&mut nitf_file).unwrap();
35//! let file_title = nitf.nitf_header.ftitle.val;
36//! let n_img_segments = nitf.nitf_header.numi.val;
37//! let n_rows = nitf.image_segments[0].header.nrows.val;
38//! ```
39//!
40//! If there is user-defined tagged-record-extension (TRE) data within a segment,
41//! it is stored in an [ExtendedSubheader] for the user to parse accordingly.
42use thiserror::Error;
43
44pub mod headers;
45mod nitf;
46pub mod types;
47
48pub use nitf::*;
49
50pub type NitfResult<T> = Result<T, NitfError>;
51
52// Crate specific errors
53#[derive(Error, Debug)]
54pub enum NitfError {
55    #[error("error parsing {0}")]
56    ParseError(String),
57    #[error("{0}")]
58    Fatal(String),
59    #[error("Fatal error reading {0}")]
60    ReadFatal(String),
61    #[error("Cannot write without first providing a file")]
62    FileFatal,
63    #[error("value of {0} does not match")]
64    Value(String),
65    #[error("Couldn't update header values")]
66    Update(),
67    // Wrappers for built in errors
68    #[error(transparent)]
69    IOError(#[from] std::io::Error),
70}