ligen_ir/function/parameter/
mod.rs

1//! Function parameter.
2
3#[cfg(any(test, feature = "mocks"))]
4pub mod mock;
5
6use std::fmt::{Display, Formatter};
7
8use crate::{prelude::*, Literal};
9use crate::{Identifier, Type, Attributes, Mutability};
10
11#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
12/// Parameter representation.
13pub struct Parameter {
14    /// Attributes.
15    pub attributes: Attributes,
16    /// Identifier.
17    pub identifier: Identifier,
18    /// Type.
19    pub type_: Type,
20    /// Default value.
21    pub default_value: Option<Literal>,
22}
23
24impl Parameter {
25    /// Get parameter mutability.
26    pub fn mutability(&self) -> Mutability {
27        if self.type_.is_mutable_reference() {
28            Mutability::Mutable
29        } else {
30            Mutability::Constant
31        }
32    }
33}
34
35impl Display for Parameter {
36    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37        let attributes = if self.attributes.is_empty() { "".into() } else { format!(" {}", self.attributes) };
38        f.write_str(&format!("{}: {}{}", self.identifier, self.type_, attributes))
39    }
40}