use std::fmt::{self, Write};
use crate::doc::Doc;
use crate::formatter::Formatter;
#[derive(Debug, Clone)]
pub struct Variant {
name: String,
value: Option<u64>,
doc: Option<Doc>,
}
impl Variant {
pub fn new(name: &str) -> Self {
Variant::with_string(String::from(name))
}
pub fn with_string(name: String) -> Self {
Variant {
name,
value: None,
doc: None,
}
}
pub fn new_with_value(name: &str, value: u64) -> Self {
Variant::with_string_and_value(String::from(name), value)
}
pub fn with_string_and_value(name: String, value: u64) -> Self {
Variant {
name,
value: Some(value),
doc: None,
}
}
pub fn doc_str(&mut self, doc: &str) -> &mut Self {
if let Some(d) = &mut self.doc {
d.add_text(doc);
} else {
self.doc = Some(Doc::with_str(doc));
}
self
}
pub fn doc(&mut self, doc: Doc) -> &mut Self {
self.doc = Some(doc);
self
}
pub fn set_value(&mut self, value: u64) -> &mut Self {
self.value = Some(value);
self
}
pub fn name(&self) -> &str {
&self.name
}
pub fn value(&self) -> Option<u64> {
self.value
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if let Some(ref docs) = self.doc {
docs.fmt(fmt)?;
}
write!(fmt, "{}", self.name)?;
if let Some(value) = self.value {
write!(fmt, " = {value}")?;
}
Ok(())
}
}