donet_core/
dcstruct.rs

1/*
2    This file is part of Donet.
3
4    Copyright © 2024-2025 Max Rodriguez <[email protected]>
5
6    Donet is free software; you can redistribute it and/or modify
7    it under the terms of the GNU Affero General Public License,
8    as published by the Free Software Foundation, either version 3
9    of the License, or (at your option) any later version.
10
11    Donet is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU Affero General Public License for more details.
15
16    You should have received a copy of the GNU Affero General Public
17    License along with Donet. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Data model representing a DC Struct element.
21//! Very similar to a DClass element, but does not allow
22//! declaring atomic or molecular fields.
23
24use crate::dcfield::DCField;
25use crate::globals;
26use crate::hashgen::*;
27
28#[derive(Debug)]
29pub struct DCStruct {
30    pub identifier: String,
31    pub id: globals::DClassId,
32    pub fields: Vec<DCField>,
33    /// Whether this struct has any fields that have a constraint. (e.g. ranges)
34    pub has_range: bool,
35}
36
37impl std::fmt::Display for DCStruct {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "struct ")?;
40        f.write_str(&self.identifier)?;
41
42        write!(f, " {{  // index ")?;
43        self.id.fmt(f)?;
44        writeln!(f)?;
45
46        for field in &self.fields {
47            field.fmt(f)?;
48        }
49        writeln!(f, "}};")
50    }
51}
52
53impl LegacyDCHash for DCStruct {
54    fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
55        hashgen.add_string(self.identifier.clone());
56        hashgen.add_int(self.fields.len().try_into().unwrap());
57
58        for field in &self.fields {
59            field.generate_hash(hashgen);
60        }
61    }
62}