Skip to main content

Header

Struct Header 

Source
pub struct Header { /* private fields */ }
Expand description

An ordered FITS header unit: records in appearance order, with strict keyword access and CRUD.

Equality is semantic (records compare by content, not by retained bytes).

Implementations§

Source§

impl Header

Source

pub fn new() -> Self

An empty header.

Source

pub fn cards(&self) -> &[Record]

The records in order (read-only escape hatch).

Source

pub fn iter(&self) -> impl Iterator<Item = &Record>

Iterate the records in order.

Source

pub fn count(&self, name: &str) -> usize

How many records carry this keyword.

Source

pub fn get<T: FromCard>( &self, key: impl Into<Key>, ) -> Result<Option<T>, FitsError>

Read a keyword as T. Err only on an ambiguous bare name; Ok(None) when absent or the value does not convert; never panics.

§Examples
let mut h = Header::new();
h.set("EXPTIME", 120.0).unwrap();
assert_eq!(h.get::<f64>("EXPTIME").unwrap(), Some(120.0));
assert_eq!(h.get::<i64>("MISSING").unwrap(), None);
Source

pub fn get_str(&self, key: impl Into<Key>) -> Result<Option<&str>, FitsError>

Borrow a keyword’s string value (Str content, non-empty); None for empty or a literal.

Source

pub fn get_all<T: FromCard>(&self, name: &str) -> Vec<T>

Every value for a keyword, in order.

Source

pub fn set( &mut self, key: impl Into<Key>, value: impl IntoValue, ) -> Result<(), FitsError>

Update the addressed record in place, or append when the (unique) name is absent. The keyword must be FITS-standard (≤8, A-Z 0-9 - _); use set_raw for vendor keys.

§Examples
let mut h = Header::new();
h.set("OBJECT", "M31").unwrap(); // appends
h.set("OBJECT", "NGC 7000").unwrap(); // updates in place
assert_eq!(h.count("OBJECT"), 1);

let err = h.set("object", 1); // lowercase is not FITS-standard
assert!(matches!(err, Err(FitsError::InvalidKeyword { .. })));
Source

pub fn set_raw( &mut self, keyword: &str, value: impl IntoValue, ) -> Result<(), FitsError>

Like set but accepts any ≤8-char printable-ASCII keyword (vendor escape hatch).

Source

pub fn append( &mut self, name: &str, value: impl IntoValue, ) -> Result<(), FitsError>

Always add a record (a value card, or a commentary card for COMMENT/HISTORY/blank).

§Examples
let mut h = Header::new();
h.append("HISTORY", "dark subtracted").unwrap();
h.append("HISTORY", "flat fielded").unwrap();
assert_eq!(h.get_all::<String>("HISTORY").len(), 2);
Source

pub fn set_comment( &mut self, key: impl Into<Key>, comment: impl Into<String>, ) -> Result<(), FitsError>

Set or replace the addressed value card’s inline comment. No-op if the keyword is absent or not a value card.

Source

pub fn remove(&mut self, key: impl Into<Key>) -> Result<bool, FitsError>

Remove the addressed record. Returns whether anything was removed.

§Examples
let mut h = Header::new();
h.set("AIRMASS", 1.2).unwrap();
assert!(h.remove("AIRMASS").unwrap());
assert!(!h.remove("AIRMASS").unwrap());
Source

pub fn set_many<K, V>( &mut self, entries: impl IntoIterator<Item = (K, V)>, ) -> Result<(), FitsError>
where K: Into<Key>, V: IntoValue,

Apply several mutations atomically: validate every entry first, then apply all or none.

§Examples
let mut h = Header::new();
h.set_many([("FILTER", "Ha"), ("TELESCOP", "EdgeHD 8")]).unwrap();

// A rejected batch leaves the header untouched.
assert!(h.set_many([("GAIN", "1"), ("TOOLONGKEY", "2")]).is_err());
assert_eq!(h.count("GAIN"), 0);
Source

pub fn remove_many<K: Into<Key>>( &mut self, keys: impl IntoIterator<Item = K>, ) -> Result<usize, FitsError>

Remove several keys atomically (validation only guards ambiguity). Returns the count removed.

Source

pub fn to_header_bytes(&self) -> Vec<u8>

Serialize the header block only (cards, END, padded to a 2880 multiple) for splicing onto an existing file’s data.

§Examples
let mut h = Header::new();
h.set("OBJECT", "M31").unwrap();
let bytes = h.to_header_bytes();
assert_eq!(bytes.len() % fits_header::BLOCK_LEN, 0);
Source

pub fn to_bytes( &self, structural: &StructuralHints, ) -> Result<Vec<u8>, FitsError>

Serialize a standalone FITS object (header + a minimal zero data block). Mandatory structural cards are synthesized only when absent; structural is a fallback.

Errors with FitsError::DataTooLarge when the declared data segment exceeds MAX_ZERO_FILL — for real-file edits, serialize with to_header_bytes and splice the original data.

§Examples
let mut h = Header::new();
h.set("OBJECT", "M31").unwrap();
let file = h.to_bytes(&StructuralHints::default()).unwrap();
assert!(file.starts_with(b"SIMPLE"));

Trait Implementations§

Source§

impl Clone for Header

Source§

fn clone(&self) -> Header

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Header

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Header

Source§

fn default() -> Header

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Header

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Header

Source§

fn eq(&self, other: &Header) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Header

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Header

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.