Skip to content

Commit

Permalink
cds-astro#9 syntactic sugar for reading card keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
chrsoo committed Dec 28, 2024
1 parent 4bee550 commit 048d804
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/card.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{str, string::FromUtf8Error};

use nom::{
character::complete::{i64, space0},
combinator::map,
Expand All @@ -19,6 +21,11 @@ impl Card {
pub fn new(kw: Keyword, v: Value) -> Self {
Self { kw, v }
}
/// Return the ASCII trimmed keyword as an owned String.
pub fn keyword(&self) -> Result<String, FromUtf8Error> {
let kw = self.kw.trim_ascii().to_vec();
String::from_utf8(kw)
}
}

pub trait CardValue {
Expand Down Expand Up @@ -89,3 +96,16 @@ impl Value {
pub(crate) fn parse_integer(buf: &[u8]) -> IResult<&[u8], Value> {
preceded(space0, map(i64, |val| Value::Integer(val)))(buf)
}

#[cfg(test)]
mod tests {
use super::{Card, Value};

#[test]
fn trimmed_keyword_string() {
let kw = b"GAIN ".to_owned();
let v = Value::Float(30.00);
let card = Card { kw, v };
assert_eq!("GAIN".to_owned(), card.keyword().unwrap())
}
}

0 comments on commit 048d804

Please sign in to comment.