Skip to main content

microcad_lang/lower/ir/format_string/
format_spec.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Format Specification syntax element
5
6use microcad_lang_base::SrcRef;
7use microcad_lang_proc_macros::SrcReferrer;
8
9/// Format specification.
10#[derive(Clone, Debug, Default, PartialEq, SrcReferrer)]
11pub struct FormatSpec {
12    /// Precision for number formatting.
13    pub precision: Option<u32>,
14    /// Alignment width (leading zeros).
15    pub width: Option<u32>,
16    /// Source code reference.
17    pub src_ref: SrcRef,
18}
19
20impl std::fmt::Display for FormatSpec {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match (self.width, self.precision) {
23            (Some(width), Some(precision)) => write!(f, "0{width}.{precision}"),
24            (None, Some(precision)) => write!(f, ".{precision}"),
25            (Some(width), None) => write!(f, "0{width}"),
26            _ => Ok(()),
27        }
28    }
29}