| 1 |
use log::error; |
| 2 |
use toml::Value; |
| 3 |
|
| 4 |
use super::{error::TomlHelper, gobjects::GStatus, ident::Ident, parsable::Parse}; |
| 5 |
use crate::version::Version; |
| 6 |
|
| 7 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 8 |
pub struct Constant { |
| 9 |
pub ident: Ident, |
| 10 |
pub status: GStatus, |
| 11 |
pub version: Option<Version>, |
| 12 |
pub cfg_condition: Option<String>, |
| 13 |
pub generate_doc: bool, |
| 14 |
} |
| 15 |
|
| 16 |
impl Parse for Constant { |
| 17 |
fn parse(toml: &Value, object_name: &str) -> Option<Self> { |
| 18 |
let Some(ident) = Ident::parse(toml, object_name, "constant") else { |
| 19 |
error!("No 'name' or 'pattern' given for constant for object {object_name}"); |
| 20 |
return None; |
| 21 |
}; |
| 22 |
toml.check_unwanted( |
| 23 |
&[ |
| 24 |
"ignore", |
| 25 |
"manual", |
| 26 |
"name", |
| 27 |
"version", |
| 28 |
"cfg_condition", |
| 29 |
"pattern", |
| 30 |
"generate_doc", |
| 31 |
], |
| 32 |
&format!("function {object_name}"), |
| 33 |
); |
| 34 |
|
| 35 |
let version = toml |
| 36 |
.lookup("version") |
| 37 |
.and_then(Value::as_str) |
| 38 |
.and_then(|s| s.parse().ok()); |
| 39 |
let cfg_condition = toml |
| 40 |
.lookup("cfg_condition") |
| 41 |
.and_then(Value::as_str) |
| 42 |
.map(ToOwned::to_owned); |
| 43 |
|
| 44 |
let status = { |
| 45 |
if toml |
| 46 |
.lookup("ignore") |
| 47 |
.and_then(Value::as_bool) |
| 48 |
.unwrap_or(false) |
| 49 |
{ |
| 50 |
GStatus::Ignore |
| 51 |
} else if toml |
| 52 |
.lookup("manual") |
| 53 |
.and_then(Value::as_bool) |
| 54 |
.unwrap_or(false) |
| 55 |
{ |
| 56 |
GStatus::Manual |
| 57 |
} else { |
| 58 |
GStatus::Generate |
| 59 |
} |
| 60 |
}; |
| 61 |
let generate_doc = toml |
| 62 |
.lookup("generate_doc") |
| 63 |
.and_then(Value::as_bool) |
| 64 |
.unwrap_or(true); |
| 65 |
|
| 66 |
Some(Self { |
| 67 |
ident, |
| 68 |
status, |
| 69 |
version, |
| 70 |
cfg_condition, |
| 71 |
generate_doc, |
| 72 |
}) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl AsRef<Ident> for Constant { |
| 77 |
fn as_ref(&self) -> &Ident { |
| 78 |
&self.ident |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
pub type Constants = Vec<Constant>; |
| 83 |
|
| 84 |
#[cfg(test)] |
| 85 |
mod tests { |
| 86 |
use super::{super::parsable::Parse, *}; |
| 87 |
|
| 88 |
fn toml(input: &str) -> ::toml::Value { |
| 89 |
let value = ::toml::from_str(input); |
| 90 |
assert!(value.is_ok()); |
| 91 |
value.unwrap() |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn child_property_parse_generate_doc() { |
| 96 |
let r = toml( |
| 97 |
r#" |
| 98 |
name = "prop" |
| 99 |
generate_doc = false |
| 100 |
"#, |
| 101 |
); |
| 102 |
let constant = Constant::parse(&r, "a").unwrap(); |
| 103 |
assert!(!constant.generate_doc); |
| 104 |
|
| 105 |
// Ensure that the default value is "true". |
| 106 |
let r = toml( |
| 107 |
r#" |
| 108 |
name = "prop" |
| 109 |
"#, |
| 110 |
); |
| 111 |
let constant = Constant::parse(&r, "a").unwrap(); |
| 112 |
assert!(constant.generate_doc); |
| 113 |
} |
| 114 |
} |