Skip to content

Commit

Permalink
Renamed bluestat to bluejay, which outputs file info in JSON. More to…
Browse files Browse the repository at this point in the history
… be added.
  • Loading branch information
billallen256 committed Sep 29, 2024
1 parent ddfa372 commit 4185ad0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ name = "bluefile"
path = "src/lib.rs"

[[bin]]
name = "bluestat"
path = "src/bluestat.rs"
name = "bluejay"
path = "src/bluejay.rs"
18 changes: 18 additions & 0 deletions src/bluefile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Functions, structures, and traits common to all bluefiles.
use std::fmt;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;
Expand All @@ -23,6 +24,23 @@ const EXT_KEYWORD_LENGTH: usize = 4;
pub enum TypeCode {
Type1000(i32),
Type2000(i32),
Type3000(i32),
Type4000(i32),
Type5000(i32),
Type6000(i32),
}

impl fmt::Display for TypeCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TypeCode::Type1000(t) => write!(f, "{}", t),
TypeCode::Type2000(t) => write!(f, "{}", t),
TypeCode::Type3000(t) => write!(f, "{}", t),
TypeCode::Type4000(t) => write!(f, "{}", t),
TypeCode::Type5000(t) => write!(f, "{}", t),
TypeCode::Type6000(t) => write!(f, "{}", t),
}
}
}

/// Tracks information necesary to iterate through the extended header.
Expand Down
14 changes: 13 additions & 1 deletion src/bluestat.rs → src/bluejay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,17 @@ fn main() {
},
};

dbg!(header);
println!("{{");
println!(" \"type_code\": \"{}\",", header.type_code);
println!(" \"header_endianness\": \"{}\",", header.header_endianness);
println!(" \"data_endianness\": \"{}\",", header.data_endianness);
println!(" \"ext_header_start\": {},", header.ext_start);
println!(" \"ext_header_size\": {},", header.ext_size);
println!(" \"data_start\": {},", header.data_start);
println!(" \"data_size\": {},", header.data_size);
println!(" \"data_type\": \"{}\",", header.raw_data_type);
println!(" \"data_rank\": \"{}\",", header.data_type.rank);
println!(" \"data_format\": \"{}\",", header.data_type.format);
println!(" \"timecode\": {}", header.timecode);
println!("}}");
}
22 changes: 22 additions & 0 deletions src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ impl TryFrom<u8> for Rank {
}
}

impl fmt::Display for Rank {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Rank::Scalar => write!(f, "scalar"),
Rank::Complex => write!(f, "complex"),
}
}
}

/// Defines the number of elements required by each Rank enum type.
pub fn rank_multiple(r: &Rank) -> usize {
match r {
Expand Down Expand Up @@ -76,6 +85,19 @@ impl TryFrom<u8> for Format {
}
}

impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Format::Byte => write!(f, "byte"),
Format::Int => write!(f, "int"),
Format::Long => write!(f, "long"),
Format::LongLong => write!(f, "long long"),
Format::Float => write!(f, "float"),
Format::Double => write!(f, "double"),
}
}
}

/// Defines the number of bytes required by each Format enum type.
pub fn format_size(f: &Format) -> usize {
match f {
Expand Down
10 changes: 10 additions & 0 deletions src/endian.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryFrom;
use std::fmt;

use crate::error::Error;

Expand All @@ -9,6 +10,15 @@ pub enum Endianness {
Little,
}

impl fmt::Display for Endianness {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Endianness::Big => write!(f, "big"),
Endianness::Little => write!(f, "little"),
}
}
}

/// Converts raw bytes to an Endianness enum type.
impl TryFrom<&[u8]> for Endianness {
type Error = Error;
Expand Down
11 changes: 11 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Header {
pub data_start: f64, // in bytes
pub data_size: f64, // in bytes
pub type_code: TypeCode,
pub raw_data_type: String,
pub data_type: DataType,
pub timecode: f64, // seconds since Jan. 1, 1950
pub keywords: Vec<HeaderKeyword>,
Expand All @@ -55,6 +56,7 @@ pub fn parse_header(data: &[u8]) -> Result<Header> {
let data_start = bytes_to_f64(&data[32..40], header_endianness)?;
let data_size = bytes_to_f64(&data[40..48], header_endianness)?;
let type_code = parse_type_code(&data[48..52], header_endianness)?;
let raw_data_type = from_utf8(&data[52..54]).unwrap().to_string();
let data_type = DataType{rank: Rank::try_from(data[52])?, format: Format::try_from(data[53])?};
let timecode = bytes_to_f64(&data[56..64], header_endianness)?;
let keylength: usize = match bytes_to_i32(&data[160..164], header_endianness).unwrap().try_into() {
Expand All @@ -72,6 +74,7 @@ pub fn parse_header(data: &[u8]) -> Result<Header> {
data_start,
data_size,
type_code,
raw_data_type,
data_type,
timecode,
keywords,
Expand Down Expand Up @@ -149,6 +152,14 @@ fn parse_type_code(v: &[u8], endianness: Endianness) -> Result<TypeCode> {
Ok(TypeCode::Type1000(t))
} else if t/1000 == 2 {
Ok(TypeCode::Type2000(t))
} else if t/1000 == 3 {
Ok(TypeCode::Type3000(t))
} else if t/1000 == 4 {
Ok(TypeCode::Type4000(t))
} else if t/1000 == 5 {
Ok(TypeCode::Type5000(t))
} else if t/1000 == 6 {
Ok(TypeCode::Type6000(t))
} else {
Err(Error::UnknownFileTypeCode(t))
}
Expand Down

0 comments on commit 4185ad0

Please sign in to comment.