float_format/
components.rs1use crate::*;
2
3#[derive(Clone, PartialEq, Eq, Hash)]
5pub struct Components {
6 pub sign: Option<bool>,
8
9 pub exp: BitPattern,
11
12 pub mant: BitPattern,
14}
15
16impl Components {
17 pub fn new(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
21 Ok(Components {
22 sign,
23 exp: BitPattern::from_str(exp)?,
24 mant: BitPattern::from_str(mant)?,
25 })
26 }
27
28 pub fn new_bin(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
32 Ok(Components {
33 sign,
34 exp: BitPattern::from_bin_str(exp),
35 mant: BitPattern::from_bin_str(mant),
36 })
37 }
38
39 pub fn new_oct(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
43 Ok(Components {
44 sign,
45 exp: BitPattern::from_oct_str(exp),
46 mant: BitPattern::from_oct_str(mant),
47 })
48 }
49
50 pub fn new_dec(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
54 Ok(Components {
55 sign,
56 exp: BitPattern::from_dec_str(exp),
57 mant: BitPattern::from_dec_str(mant),
58 })
59 }
60
61 pub fn new_hex(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
65 Ok(Components {
66 sign,
67 exp: BitPattern::from_hex_str(exp),
68 mant: BitPattern::from_hex_str(mant),
69 })
70 }
71
72 pub fn format_with_excess(&self, excess: u32) -> Format {
74 Format::new_with_sign(
75 self.sign.is_some(),
76 self.exp.len() as u8,
77 self.mant.len(),
78 excess,
79 )
80 }
81
82 pub fn format(&self) -> Format {
85 self.format_with_excess(0)
86 }
87
88 pub fn len(&self) -> usize {
90 self.sign.is_some() as usize + self.exp.len() + self.mant.len()
91 }
92
93 pub fn get_sign(&self) -> Option<&str> {
95 self.sign.as_ref().map(|s| if *s { "1" } else { "0" })
96 }
97}
98
99impl std::fmt::Debug for Components {
100 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
101 f.debug_struct("Components")
102 .field("sign", &format_args!("{}", &self.sign.map(|b| if b { "-" } else { "+" }).unwrap_or("None")))
103 .field("exp", &format_args!("{}", &self.exp.to_bin_string()))
104 .field("mant", &format_args!("{}", &self.mant.to_bin_string()))
105 .finish()
106 }
107}