crustal/variant.rs
1// C/C++ Code Generator For Rust
2//
3//
4// MIT License
5//
6// Copyright (c) 2021, 2022 Reto Achermann
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all
16// copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25
26//! # Variant
27//!
28//! The variant modules provides a way to add enumeration variants to the
29//! generated code.
30
31use std::fmt::{self, Write};
32
33use crate::doc::Doc;
34use crate::formatter::Formatter;
35
36/// Defines an enumeration variant
37#[derive(Debug, Clone)]
38pub struct Variant {
39 /// The name of the variant
40 name: String,
41
42 /// The value of the variant
43 value: Option<u64>,
44
45 /// The documentation comment of the variant
46 doc: Option<Doc>,
47}
48
49impl Variant {
50 /// Creates a new `Variant`
51 pub fn new(name: &str) -> Self {
52 Variant::with_string(String::from(name))
53 }
54
55 /// creates a new `Variant` and consumes the given string
56 pub fn with_string(name: String) -> Self {
57 Variant {
58 name,
59 value: None,
60 doc: None,
61 }
62 }
63
64 /// Creates a new `Variant` with a given value
65 pub fn new_with_value(name: &str, value: u64) -> Self {
66 Variant::with_string_and_value(String::from(name), value)
67 }
68
69 /// creates a new `Variant` and consumes the given string and value
70 pub fn with_string_and_value(name: String, value: u64) -> Self {
71 Variant {
72 name,
73 value: Some(value),
74 doc: None,
75 }
76 }
77
78 /// adds a string to the documentation comment to the variant
79 pub fn doc_str(&mut self, doc: &str) -> &mut Self {
80 if let Some(d) = &mut self.doc {
81 d.add_text(doc);
82 } else {
83 self.doc = Some(Doc::with_str(doc));
84 }
85 self
86 }
87
88 // adds a documetnation comment to the variant
89 pub fn doc(&mut self, doc: Doc) -> &mut Self {
90 self.doc = Some(doc);
91 self
92 }
93
94 /// sets the current value
95 pub fn set_value(&mut self, value: u64) -> &mut Self {
96 self.value = Some(value);
97 self
98 }
99
100 /// obtains the name of the variant
101 pub fn name(&self) -> &str {
102 &self.name
103 }
104
105 /// obtains the current value of the variant
106 pub fn value(&self) -> Option<u64> {
107 self.value
108 }
109
110 /// Formats the variant using the given formatter.
111 pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
112 if let Some(ref docs) = self.doc {
113 docs.fmt(fmt)?;
114 }
115 write!(fmt, "{}", self.name)?;
116 if let Some(value) = self.value {
117 write!(fmt, " = {value}")?;
118 }
119
120 Ok(())
121 }
122}