donet_core/dcparameter.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 that represents a single parameter of an atomic
21//! field, which together form a RPC method signature.
22
23use crate::dctype::DCTypeDefinition;
24use crate::hashgen::*;
25
26/// Represents the type specification of a parameter,
27/// which can live under an atomic field, or become
28/// a standalone parameter field. (e.g. for structs)
29#[derive(Debug)]
30pub struct DCParameter {
31 base_type: DCTypeDefinition,
32 identifier: Option<String>,
33 default_value: Vec<u8>,
34}
35
36impl std::fmt::Display for DCParameter {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 self.base_type.data_type.fmt(f)?;
39
40 // if we have an identifier, write it out
41 if let Some(id) = &self.identifier {
42 write!(f, " {}", id)?;
43 }
44 // if we have a default value, write it as a hex literal
45 if self.has_default_value() {
46 write!(
47 f,
48 " = 0x{}",
49 self.default_value
50 .clone()
51 .iter()
52 .map(|b| format!("{:02X}", b))
53 .collect::<Vec<String>>()
54 .join("")
55 )?;
56 }
57 Ok(())
58 }
59}
60
61impl LegacyDCHash for DCParameter {
62 fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
63 self.base_type.generate_hash(hashgen);
64 }
65}
66
67impl DCParameter {
68 #[inline(always)]
69 pub fn has_identifier(&self) -> bool {
70 self.identifier.is_some()
71 }
72
73 #[inline(always)]
74 pub fn has_default_value(&self) -> bool {
75 !self.default_value.is_empty()
76 }
77
78 #[inline(always)]
79 pub fn get_default_value(&self) -> &Vec<u8> {
80 &self.default_value
81 }
82}