Skip to main content

microcad_lang/lower/ir/format_string/
format_expression.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Format expression syntax element
5
6use crate::lower::ir;
7
8use microcad_lang_base::SrcRef;
9use microcad_lang_proc_macros::SrcReferrer;
10
11/// Format expression including format specification.
12#[derive(Clone, Debug, SrcReferrer)]
13pub struct FormatExpression {
14    /// Format specifier
15    pub spec: Option<ir::FormatSpec>,
16    /// Expression to format
17    pub expression: ir::Expression,
18    /// Source code reference
19    src_ref: SrcRef,
20}
21
22impl FormatExpression {
23    /// Create new format expression.
24    pub fn new(spec: Option<ir::FormatSpec>, expression: ir::Expression, src_ref: SrcRef) -> Self {
25        Self {
26            src_ref,
27            spec,
28            expression,
29        }
30    }
31}
32
33impl std::fmt::Display for FormatExpression {
34    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
35        if let Some(spec) = &self.spec {
36            write!(f, "{{{}:{}}}", spec, self.expression)
37        } else {
38            write!(f, "{{{}}}", self.expression)
39        }
40    }
41}