fips_md/parser/
types.rs

1//! Structures related to typing
2
3use std::fmt::Display;
4
5use super::CompileTimeConstant;
6
7/// A type in the FIPS language
8#[derive(Clone,Eq,PartialEq,Debug)]
9pub enum FipsType {
10    Double,
11    Int64,
12    //Position, // This is an alias for [f64; NDIM]
13    Array{
14        typ: Box<FipsType>,
15        length: CompileTimeConstant<usize>
16    }
17}
18
19impl FipsType {
20    /// Returns true if type is scalar
21    pub(crate) fn is_scalar(&self) -> bool {
22        match self {
23            FipsType::Double | FipsType::Int64 => true,
24            FipsType::Array {..} => false
25        }
26    }
27
28    /// Returns true if the type is derived from i64 (i.e. i64 or [i64] or [[i64]] ...)
29    pub(crate) fn is_i64_derived(&self) -> bool {
30        match self {
31            FipsType::Int64 => true,
32            FipsType::Double => false,
33            FipsType::Array { typ, ..} => typ.is_i64_derived()
34        }
35    }
36
37    /// Returns true if the type is derived from f64 (i.e. f64 or [f64] or [[f64]] ...)
38    pub(crate) fn is_f64_derived(&self) -> bool {
39        match self {
40            FipsType::Int64 => false,
41            FipsType::Double => true,
42            FipsType::Array { typ, ..} => typ.is_f64_derived()
43        }
44    }
45}
46
47impl Display for FipsType {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        match self {
50            FipsType::Double => f.write_str("f64"),
51            FipsType::Int64 => f.write_str("i64"),
52            FipsType::Array { typ, length } => f.write_fmt(format_args!("[{},{}]", typ, length))
53        }
54    }
55}