| 1 |
use std::collections::HashSet; |
| 2 |
|
| 3 |
use log::error; |
| 4 |
use toml::Value; |
| 5 |
|
| 6 |
use super::{ |
| 7 |
error::TomlHelper, |
| 8 |
functions::{Parameters, Return, check_rename}, |
| 9 |
gobjects::GStatus, |
| 10 |
ident::Ident, |
| 11 |
parsable::{Parsable, Parse}, |
| 12 |
}; |
| 13 |
use crate::version::Version; |
| 14 |
|
| 15 |
#[derive(Clone, Debug)] |
| 16 |
pub struct VirtualMethod { |
| 17 |
pub ident: Ident, |
| 18 |
pub status: GStatus, |
| 19 |
pub version: Option<Version>, |
| 20 |
pub cfg_condition: Option<String>, |
| 21 |
pub parameters: Parameters, |
| 22 |
pub ret: Return, |
| 23 |
pub doc_hidden: bool, |
| 24 |
pub doc_ignore_parameters: HashSet<String>, |
| 25 |
pub doc_trait_name: Option<String>, |
| 26 |
pub unsafe_: bool, |
| 27 |
pub rename: Option<String>, |
| 28 |
pub bypass_auto_rename: bool, |
| 29 |
pub generate_doc: bool, |
| 30 |
} |
| 31 |
|
| 32 |
impl Parse for VirtualMethod { |
| 33 |
fn parse(toml: &Value, object_name: &str) -> Option<Self> { |
| 34 |
let Some(ident) = Ident::parse(toml, object_name, "virtual_method") else { |
| 35 |
error!("No 'name' or 'pattern' given for virtual_method for object {object_name}"); |
| 36 |
return None; |
| 37 |
}; |
| 38 |
toml.check_unwanted( |
| 39 |
&[ |
| 40 |
"ignore", |
| 41 |
"manual", |
| 42 |
"version", |
| 43 |
"cfg_condition", |
| 44 |
"parameter", |
| 45 |
"return", |
| 46 |
"name", |
| 47 |
"doc_hidden", |
| 48 |
"doc_ignore_parameters", |
| 49 |
"pattern", |
| 50 |
"doc_trait_name", |
| 51 |
"unsafe", |
| 52 |
"rename", |
| 53 |
"bypass_auto_rename", |
| 54 |
"generate_doc", |
| 55 |
], |
| 56 |
&format!("virtual_method {object_name}"), |
| 57 |
); |
| 58 |
|
| 59 |
let status = { |
| 60 |
if toml |
| 61 |
.lookup("ignore") |
| 62 |
.and_then(Value::as_bool) |
| 63 |
.unwrap_or(false) |
| 64 |
{ |
| 65 |
GStatus::Ignore |
| 66 |
} else if toml |
| 67 |
.lookup("manual") |
| 68 |
.and_then(Value::as_bool) |
| 69 |
.unwrap_or(false) |
| 70 |
{ |
| 71 |
GStatus::Manual |
| 72 |
} else { |
| 73 |
GStatus::Generate |
| 74 |
} |
| 75 |
}; |
| 76 |
let version = toml |
| 77 |
.lookup("version") |
| 78 |
.and_then(Value::as_str) |
| 79 |
.and_then(|s| s.parse().ok()); |
| 80 |
let cfg_condition = toml |
| 81 |
.lookup("cfg_condition") |
| 82 |
.and_then(Value::as_str) |
| 83 |
.map(ToOwned::to_owned); |
| 84 |
let parameters = Parameters::parse(toml.lookup("parameter"), object_name); |
| 85 |
let ret = Return::parse(toml.lookup("return"), object_name); |
| 86 |
let doc_hidden = toml |
| 87 |
.lookup("doc_hidden") |
| 88 |
.and_then(Value::as_bool) |
| 89 |
.unwrap_or(false); |
| 90 |
let doc_ignore_parameters = toml |
| 91 |
.lookup_vec("doc_ignore_parameters", "Invalid doc_ignore_parameters") |
| 92 |
.map(|v| { |
| 93 |
v.iter() |
| 94 |
.filter_map(|v| v.as_str().map(String::from)) |
| 95 |
.collect() |
| 96 |
}) |
| 97 |
.unwrap_or_default(); |
| 98 |
let doc_trait_name = toml |
| 99 |
.lookup("doc_trait_name") |
| 100 |
.and_then(Value::as_str) |
| 101 |
.map(ToOwned::to_owned); |
| 102 |
let unsafe_ = toml |
| 103 |
.lookup("unsafe") |
| 104 |
.and_then(Value::as_bool) |
| 105 |
.unwrap_or(false); |
| 106 |
let rename = toml |
| 107 |
.lookup("rename") |
| 108 |
.and_then(Value::as_str) |
| 109 |
.map(ToOwned::to_owned); |
| 110 |
if !check_rename(&rename, object_name, &ident) { |
| 111 |
return None; |
| 112 |
} |
| 113 |
let bypass_auto_rename = toml |
| 114 |
.lookup("bypass_auto_rename") |
| 115 |
.and_then(Value::as_bool) |
| 116 |
.unwrap_or(false); |
| 117 |
|
| 118 |
let generate_doc = toml |
| 119 |
.lookup("generate_doc") |
| 120 |
.and_then(Value::as_bool) |
| 121 |
.unwrap_or(true); |
| 122 |
Some(Self { |
| 123 |
ident, |
| 124 |
status, |
| 125 |
version, |
| 126 |
cfg_condition, |
| 127 |
parameters, |
| 128 |
ret, |
| 129 |
doc_hidden, |
| 130 |
doc_ignore_parameters, |
| 131 |
doc_trait_name, |
| 132 |
unsafe_, |
| 133 |
rename, |
| 134 |
bypass_auto_rename, |
| 135 |
generate_doc, |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl AsRef<Ident> for VirtualMethod { |
| 141 |
fn as_ref(&self) -> &Ident { |
| 142 |
&self.ident |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
pub type VirtualMethods = Vec<VirtualMethod>; |
| 147 |
|
| 148 |
#[cfg(test)] |
| 149 |
mod tests { |
| 150 |
use super::{super::ident::Ident, *}; |
| 151 |
|
| 152 |
fn toml(input: &str) -> ::toml::Value { |
| 153 |
let value = ::toml::from_str(input); |
| 154 |
assert!(value.is_ok()); |
| 155 |
value.unwrap() |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn function_parse_ignore() { |
| 160 |
let toml = toml( |
| 161 |
r#" |
| 162 |
name = "func1" |
| 163 |
ignore = true |
| 164 |
"#, |
| 165 |
); |
| 166 |
let f = VirtualMethod::parse(&toml, "a").unwrap(); |
| 167 |
assert_eq!(f.ident, Ident::Name("func1".into())); |
| 168 |
assert!(f.status.ignored()); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn function_parse_manual() { |
| 173 |
let toml = toml( |
| 174 |
r#" |
| 175 |
name = "func1" |
| 176 |
manual = true |
| 177 |
"#, |
| 178 |
); |
| 179 |
let f = VirtualMethod::parse(&toml, "a").unwrap(); |
| 180 |
assert_eq!(f.ident, Ident::Name("func1".into())); |
| 181 |
assert!(f.status.manual()); |
| 182 |
} |
| 183 |
} |