(&self, value: &Value)
| 105 | } |
| 106 | |
| 107 | fn format(&self, value: &Value) -> Result<String, VmError> { |
| 108 | let mut formatted = match value { |
| 109 | Value::Number(n) => match self.format_type { |
| 110 | Some('d') => format!("{:.0}", n), |
| 111 | Some('x') => { |
| 112 | let int_val = *n as i64; |
| 113 | format!("{:x}", int_val) |
| 114 | } |
| 115 | Some('X') => { |
| 116 | let int_val = *n as i64; |
| 117 | format!("{:X}", int_val) |
| 118 | } |
| 119 | Some('o') => { |
| 120 | let int_val = *n as i64; |
| 121 | format!("{:o}", int_val) |
| 122 | } |
| 123 | Some('b') => { |
| 124 | let int_val = *n as i64; |
| 125 | format!("{:b}", int_val) |
| 126 | } |
| 127 | Some('f') => { |
| 128 | if let Some(precision) = self.precision { |
| 129 | format!("{:.*}", precision, n) |
| 130 | } else { |
| 131 | format!("{}", n) |
| 132 | } |
| 133 | } |
| 134 | _ => { |
| 135 | if let Some(precision) = self.precision { |
| 136 | format!("{:.*}", precision, n) |
| 137 | } else { |
| 138 | format!("{}", n) |
| 139 | } |
| 140 | } |
| 141 | }, |
| 142 | Value::String(s) => { |
| 143 | let s = s.to_str().unwrap(); |
| 144 | if let Some(precision) = self.precision { |
| 145 | s[..s.chars().take(precision).map(|c| c.len_utf8()).sum()].to_string() |
| 146 | } else { |
| 147 | s.to_string() |
| 148 | } |
| 149 | } |
| 150 | _ => format!("{}", value), |
| 151 | }; |
| 152 | |
| 153 | // Apply width and alignment |
| 154 | if let Some(width) = self.width { |
| 155 | if formatted.chars().count() < width { |
| 156 | let padding = width - formatted.chars().count(); |
| 157 | let fill_char = self.fill.unwrap_or(' '); |
| 158 | |
| 159 | match self.align.unwrap_or('>') { |
| 160 | '<' => { |
| 161 | formatted.extend(std::iter::repeat(fill_char).take(padding)); |
| 162 | } |
| 163 | '>' => { |
| 164 | let mut new_string = String::new(); |
no test coverage detected