use crate::value::{FromField, IntoValue, Value};
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FitsKeyword {
pub name: String,
pub(crate) value: Value,
pub comment: String,
}
impl FitsKeyword {
pub fn new(name: impl Into<String>, value: impl IntoValue, comment: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into_value(),
comment: comment.into(),
}
}
#[must_use]
pub fn value_str(&self) -> &str {
self.value.text()
}
#[must_use]
pub fn get<T: FromField>(&self) -> Option<T> {
T::from_field(self.value.text())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_selects_value_kind_from_rust_type() {
let s = FitsKeyword::new("OBJECT", "M31", "target");
assert_eq!(s.value_str(), "M31");
assert_eq!(s.comment, "target");
assert_eq!(s.get::<String>(), Some("M31".to_owned()));
let n = FitsKeyword::new("GAIN", 100_i64, "");
assert_eq!(n.value_str(), "100");
assert_eq!(n.get::<i64>(), Some(100));
assert_eq!(n.get::<bool>(), None);
}
}