| 1 |
use std::{collections::HashSet, str::FromStr}; |
| 2 |
|
| 3 |
use log::error; |
| 4 |
use toml::Value; |
| 5 |
|
| 6 |
use super::{ |
| 7 |
error::TomlHelper, |
| 8 |
gobjects::GStatus, |
| 9 |
ident::Ident, |
| 10 |
parameter_matchable::Functionlike, |
| 11 |
parsable::{Parsable, Parse}, |
| 12 |
string_type::StringType, |
| 13 |
}; |
| 14 |
use crate::{ |
| 15 |
analysis::safety_assertion_mode::SafetyAssertionMode, |
| 16 |
codegen::Visibility, |
| 17 |
library::{Infallible, Mandatory, Nullable}, |
| 18 |
version::Version, |
| 19 |
}; |
| 20 |
|
| 21 |
#[derive(Clone, Debug)] |
| 22 |
pub struct CallbackParameter { |
| 23 |
pub ident: Ident, |
| 24 |
pub nullable: Option<Nullable>, |
| 25 |
} |
| 26 |
|
| 27 |
pub type CallbackParameters = Vec<CallbackParameter>; |
| 28 |
|
| 29 |
impl Parse for CallbackParameter { |
| 30 |
fn parse(toml: &Value, object_name: &str) -> Option<Self> { |
| 31 |
let Some(ident) = Ident::parse(toml, object_name, "callback parameter") else { |
| 32 |
error!("No 'name' or 'pattern' given for parameter for object {object_name}"); |
| 33 |
return None; |
| 34 |
}; |
| 35 |
toml.check_unwanted(&["nullable"], &format!("callback parameter {object_name}")); |
| 36 |
|
| 37 |
let nullable = toml |
| 38 |
.lookup("nullable") |
| 39 |
.and_then(Value::as_bool) |
| 40 |
.map(Nullable); |
| 41 |
|
| 42 |
Some(Self { ident, nullable }) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl AsRef<Ident> for CallbackParameter { |
| 47 |
fn as_ref(&self) -> &Ident { |
| 48 |
&self.ident |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Clone, Debug)] |
| 53 |
pub struct Parameter { |
| 54 |
pub ident: Ident, |
| 55 |
// true - parameter don't changed in FFI function, |
| 56 |
// false(default) - parameter can be changed in FFI function |
| 57 |
pub constant: bool, |
| 58 |
pub move_: Option<bool>, |
| 59 |
pub nullable: Option<Nullable>, |
| 60 |
pub mandatory: Option<Mandatory>, |
| 61 |
pub infallible: Option<Infallible>, |
| 62 |
pub length_of: Option<String>, |
| 63 |
pub string_type: Option<StringType>, |
| 64 |
pub callback_parameters: CallbackParameters, |
| 65 |
} |
| 66 |
|
| 67 |
impl Parse for Parameter { |
| 68 |
fn parse(toml: &Value, object_name: &str) -> Option<Self> { |
| 69 |
let Some(ident) = Ident::parse(toml, object_name, "function parameter") else { |
| 70 |
error!("No 'name' or 'pattern' given for parameter for object {object_name}"); |
| 71 |
return None; |
| 72 |
}; |
| 73 |
toml.check_unwanted( |
| 74 |
&[ |
| 75 |
"const", |
| 76 |
"nullable", |
| 77 |
"mandatory", |
| 78 |
"infallible", |
| 79 |
"length_of", |
| 80 |
"name", |
| 81 |
"move", |
| 82 |
"pattern", |
| 83 |
"string_type", |
| 84 |
"callback_parameter", |
| 85 |
], |
| 86 |
&format!("function parameter {object_name}"), |
| 87 |
); |
| 88 |
|
| 89 |
let constant = toml |
| 90 |
.lookup("const") |
| 91 |
.and_then(Value::as_bool) |
| 92 |
.unwrap_or(false); |
| 93 |
let move_ = toml.lookup("move").and_then(Value::as_bool); |
| 94 |
let nullable = toml |
| 95 |
.lookup("nullable") |
| 96 |
.and_then(Value::as_bool) |
| 97 |
.map(Nullable); |
| 98 |
let mandatory = toml |
| 99 |
.lookup("mandatory") |
| 100 |
.and_then(Value::as_bool) |
| 101 |
.map(Mandatory); |
| 102 |
let infallible = toml |
| 103 |
.lookup("infallible") |
| 104 |
.and_then(Value::as_bool) |
| 105 |
.map(Infallible); |
| 106 |
let length_of = toml |
| 107 |
.lookup("length_of") |
| 108 |
.and_then(Value::as_str) |
| 109 |
.map(|s| if s == "return" { "" } else { s }) |
| 110 |
.map(ToOwned::to_owned); |
| 111 |
let string_type = toml.lookup("string_type").and_then(Value::as_str); |
| 112 |
let string_type = match string_type { |
| 113 |
None => None, |
| 114 |
Some(val) => match StringType::from_str(val) { |
| 115 |
Ok(val) => Some(val), |
| 116 |
Err(error_str) => { |
| 117 |
error!("Error: {error_str} for parameter for object {object_name}"); |
| 118 |
None |
| 119 |
} |
| 120 |
}, |
| 121 |
}; |
| 122 |
let callback_parameters = |
| 123 |
CallbackParameters::parse(toml.lookup("callback_parameter"), object_name); |
| 124 |
|
| 125 |
Some(Self { |
| 126 |
ident, |
| 127 |
constant, |
| 128 |
move_, |
| 129 |
nullable, |
| 130 |
mandatory, |
| 131 |
infallible, |
| 132 |
length_of, |
| 133 |
string_type, |
| 134 |
callback_parameters, |
| 135 |
}) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl AsRef<Ident> for Parameter { |
| 140 |
fn as_ref(&self) -> &Ident { |
| 141 |
&self.ident |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub type Parameters = Vec<Parameter>; |
| 146 |
|
| 147 |
#[derive(Clone, Debug)] |
| 148 |
pub struct Return { |
| 149 |
pub nullable: Option<Nullable>, |
| 150 |
pub mandatory: Option<Mandatory>, |
| 151 |
pub infallible: Option<Infallible>, |
| 152 |
pub bool_return_is_error: Option<String>, |
| 153 |
pub nullable_return_is_error: Option<String>, |
| 154 |
pub use_return_for_result: Option<bool>, |
| 155 |
pub string_type: Option<StringType>, |
| 156 |
pub type_name: Option<String>, |
| 157 |
} |
| 158 |
|
| 159 |
impl Return { |
| 160 |
pub fn parse(toml: Option<&Value>, object_name: &str) -> Self { |
| 161 |
if toml.is_none() { |
| 162 |
return Self { |
| 163 |
nullable: None, |
| 164 |
mandatory: None, |
| 165 |
infallible: None, |
| 166 |
bool_return_is_error: None, |
| 167 |
nullable_return_is_error: None, |
| 168 |
use_return_for_result: None, |
| 169 |
string_type: None, |
| 170 |
type_name: None, |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
let v = toml.unwrap(); |
| 175 |
v.check_unwanted( |
| 176 |
&[ |
| 177 |
"nullable", |
| 178 |
"mandatory", |
| 179 |
"infallible", |
| 180 |
"bool_return_is_error", |
| 181 |
"nullable_return_is_error", |
| 182 |
"use_return_for_result", |
| 183 |
"string_type", |
| 184 |
"type", |
| 185 |
], |
| 186 |
"return", |
| 187 |
); |
| 188 |
|
| 189 |
let nullable = v.lookup("nullable").and_then(Value::as_bool).map(Nullable); |
| 190 |
let mandatory = v |
| 191 |
.lookup("mandatory") |
| 192 |
.and_then(Value::as_bool) |
| 193 |
.map(Mandatory); |
| 194 |
let infallible = v |
| 195 |
.lookup("infallible") |
| 196 |
.and_then(Value::as_bool) |
| 197 |
.map(Infallible); |
| 198 |
let bool_return_is_error = v |
| 199 |
.lookup("bool_return_is_error") |
| 200 |
.and_then(Value::as_str) |
| 201 |
.map(ToOwned::to_owned); |
| 202 |
let nullable_return_is_error = v |
| 203 |
.lookup("nullable_return_is_error") |
| 204 |
.and_then(Value::as_str) |
| 205 |
.map(ToOwned::to_owned); |
| 206 |
let use_return_for_result = v.lookup("use_return_for_result").and_then(Value::as_bool); |
| 207 |
let string_type = v.lookup("string_type").and_then(Value::as_str); |
| 208 |
let string_type = match string_type { |
| 209 |
None => None, |
| 210 |
Some(v) => match StringType::from_str(v) { |
| 211 |
Ok(v) => Some(v), |
| 212 |
Err(error_str) => { |
| 213 |
error!("Error: {error_str} for return"); |
| 214 |
None |
| 215 |
} |
| 216 |
}, |
| 217 |
}; |
| 218 |
let type_name = v |
| 219 |
.lookup("type") |
| 220 |
.and_then(Value::as_str) |
| 221 |
.map(ToOwned::to_owned); |
| 222 |
if string_type.is_some() && type_name.is_some() { |
| 223 |
error!( |
| 224 |
"\"string_type\" and \"type\" parameters can't be passed at the same time for \ |
| 225 |
object {object_name}, only \"type\" will be applied in this case" |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
Self { |
| 230 |
nullable, |
| 231 |
mandatory, |
| 232 |
infallible, |
| 233 |
bool_return_is_error, |
| 234 |
nullable_return_is_error, |
| 235 |
use_return_for_result, |
| 236 |
string_type, |
| 237 |
type_name, |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
pub fn check_rename(rename: &Option<String>, object_name: &str, function_name: &Ident) -> bool { |
| 243 |
if let Some(rename) = rename { |
| 244 |
for c in &["\t", "\n", " "] { |
| 245 |
if rename.contains(c) { |
| 246 |
error!( |
| 247 |
"Invalid 'rename' value given to {object_name}::{function_name}: forbidden character '{c:?}'" |
| 248 |
); |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
true |
| 254 |
} |
| 255 |
|
| 256 |
#[derive(Clone, Debug)] |
| 257 |
pub struct Function { |
| 258 |
pub ident: Ident, |
| 259 |
pub status: GStatus, |
| 260 |
pub version: Option<Version>, |
| 261 |
pub cfg_condition: Option<String>, |
| 262 |
pub parameters: Parameters, |
| 263 |
pub ret: Return, |
| 264 |
pub doc_hidden: bool, |
| 265 |
pub doc_ignore_parameters: HashSet<String>, |
| 266 |
pub disable_length_detect: bool, |
| 267 |
pub doc_trait_name: Option<String>, |
| 268 |
pub doc_struct_name: Option<String>, |
| 269 |
pub no_future: bool, |
| 270 |
pub unsafe_: bool, |
| 271 |
pub rename: Option<String>, |
| 272 |
pub visibility: Option<Visibility>, |
| 273 |
pub bypass_auto_rename: bool, |
| 274 |
pub is_constructor: Option<bool>, |
| 275 |
pub assertion: Option<SafetyAssertionMode>, |
| 276 |
pub generate_doc: bool, |
| 277 |
} |
| 278 |
|
| 279 |
impl Parse for Function { |
| 280 |
fn parse(toml: &Value, object_name: &str) -> Option<Self> { |
| 281 |
let Some(ident) = Ident::parse(toml, object_name, "function") else { |
| 282 |
error!("No 'name' or 'pattern' given for function for object {object_name}"); |
| 283 |
return None; |
| 284 |
}; |
| 285 |
toml.check_unwanted( |
| 286 |
&[ |
| 287 |
"ignore", |
| 288 |
"manual", |
| 289 |
"version", |
| 290 |
"cfg_condition", |
| 291 |
"parameter", |
| 292 |
"return", |
| 293 |
"name", |
| 294 |
"doc_hidden", |
| 295 |
"doc_ignore_parameters", |
| 296 |
"disable_length_detect", |
| 297 |
"pattern", |
| 298 |
"doc_trait_name", |
| 299 |
"doc_struct_name", |
| 300 |
"no_future", |
| 301 |
"unsafe", |
| 302 |
"rename", |
| 303 |
"bypass_auto_rename", |
| 304 |
"constructor", |
| 305 |
"assertion", |
| 306 |
"visibility", |
| 307 |
"generate_doc", |
| 308 |
], |
| 309 |
&format!("function {object_name}"), |
| 310 |
); |
| 311 |
|
| 312 |
let status = { |
| 313 |
if toml |
| 314 |
.lookup("ignore") |
| 315 |
.and_then(Value::as_bool) |
| 316 |
.unwrap_or(false) |
| 317 |
{ |
| 318 |
GStatus::Ignore |
| 319 |
} else if toml |
| 320 |
.lookup("manual") |
| 321 |
.and_then(Value::as_bool) |
| 322 |
.unwrap_or(false) |
| 323 |
{ |
| 324 |
GStatus::Manual |
| 325 |
} else { |
| 326 |
GStatus::Generate |
| 327 |
} |
| 328 |
}; |
| 329 |
let version = toml |
| 330 |
.lookup("version") |
| 331 |
.and_then(Value::as_str) |
| 332 |
.and_then(|s| s.parse().ok()); |
| 333 |
let cfg_condition = toml |
| 334 |
.lookup("cfg_condition") |
| 335 |
.and_then(Value::as_str) |
| 336 |
.map(ToOwned::to_owned); |
| 337 |
let parameters = Parameters::parse(toml.lookup("parameter"), object_name); |
| 338 |
let ret = Return::parse(toml.lookup("return"), object_name); |
| 339 |
let doc_hidden = toml |
| 340 |
.lookup("doc_hidden") |
| 341 |
.and_then(Value::as_bool) |
| 342 |
.unwrap_or(false); |
| 343 |
let doc_ignore_parameters = toml |
| 344 |
.lookup_vec("doc_ignore_parameters", "Invalid doc_ignore_parameters") |
| 345 |
.map(|v| { |
| 346 |
v.iter() |
| 347 |
.filter_map(|v| v.as_str().map(String::from)) |
| 348 |
.collect() |
| 349 |
}) |
| 350 |
.unwrap_or_default(); |
| 351 |
let disable_length_detect = toml |
| 352 |
.lookup("disable_length_detect") |
| 353 |
.and_then(Value::as_bool) |
| 354 |
.unwrap_or(false); |
| 355 |
let doc_trait_name = toml |
| 356 |
.lookup("doc_trait_name") |
| 357 |
.and_then(Value::as_str) |
| 358 |
.map(ToOwned::to_owned); |
| 359 |
let doc_struct_name = toml |
| 360 |
.lookup("doc_struct_name") |
| 361 |
.and_then(Value::as_str) |
| 362 |
.map(ToOwned::to_owned); |
| 363 |
let no_future = toml |
| 364 |
.lookup("no_future") |
| 365 |
.and_then(Value::as_bool) |
| 366 |
.unwrap_or(false); |
| 367 |
let unsafe_ = toml |
| 368 |
.lookup("unsafe") |
| 369 |
.and_then(Value::as_bool) |
| 370 |
.unwrap_or(false); |
| 371 |
let rename = toml |
| 372 |
.lookup("rename") |
| 373 |
.and_then(Value::as_str) |
| 374 |
.map(ToOwned::to_owned); |
| 375 |
if !check_rename(&rename, object_name, &ident) { |
| 376 |
return None; |
| 377 |
} |
| 378 |
let bypass_auto_rename = toml |
| 379 |
.lookup("bypass_auto_rename") |
| 380 |
.and_then(Value::as_bool) |
| 381 |
.unwrap_or(false); |
| 382 |
let is_constructor = toml.lookup("constructor").and_then(Value::as_bool); |
| 383 |
let assertion = toml |
| 384 |
.lookup("assertion") |
| 385 |
.and_then(Value::as_str) |
| 386 |
.map(|s| s.parse::<SafetyAssertionMode>()) |
| 387 |
.transpose(); |
| 388 |
if let Err(ref err) = assertion { |
| 389 |
error!("{err}"); |
| 390 |
} |
| 391 |
let assertion = assertion.ok().flatten(); |
| 392 |
let visibility = toml |
| 393 |
.lookup("visibility") |
| 394 |
.and_then(Value::as_str) |
| 395 |
.map(std::str::FromStr::from_str) |
| 396 |
.transpose(); |
| 397 |
if let Err(ref err) = visibility { |
| 398 |
error!("{err}"); |
| 399 |
} |
| 400 |
let visibility = visibility.ok().flatten(); |
| 401 |
let generate_doc = toml |
| 402 |
.lookup("generate_doc") |
| 403 |
.and_then(Value::as_bool) |
| 404 |
.unwrap_or(true); |
| 405 |
Some(Self { |
| 406 |
ident, |
| 407 |
status, |
| 408 |
version, |
| 409 |
cfg_condition, |
| 410 |
parameters, |
| 411 |
ret, |
| 412 |
doc_hidden, |
| 413 |
doc_ignore_parameters, |
| 414 |
disable_length_detect, |
| 415 |
doc_trait_name, |
| 416 |
doc_struct_name, |
| 417 |
no_future, |
| 418 |
unsafe_, |
| 419 |
rename, |
| 420 |
visibility, |
| 421 |
bypass_auto_rename, |
| 422 |
is_constructor, |
| 423 |
assertion, |
| 424 |
generate_doc, |
| 425 |
}) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
impl Functionlike for Function { |
| 430 |
type Parameter = self::Parameter; |
| 431 |
|
| 432 |
fn parameters(&self) -> &[Self::Parameter] { |
| 433 |
&self.parameters |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
impl AsRef<Ident> for Function { |
| 438 |
fn as_ref(&self) -> &Ident { |
| 439 |
&self.ident |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
pub type Functions = Vec<Function>; |
| 444 |
|
| 445 |
#[cfg(test)] |
| 446 |
mod tests { |
| 447 |
use super::{ |
| 448 |
super::{ |
| 449 |
ident::Ident, |
| 450 |
matchable::Matchable, |
| 451 |
parameter_matchable::ParameterMatchable, |
| 452 |
parsable::{Parsable, Parse}, |
| 453 |
}, |
| 454 |
*, |
| 455 |
}; |
| 456 |
use crate::{library::Nullable, version::Version}; |
| 457 |
|
| 458 |
fn functions_toml(input: &str) -> ::toml::Value { |
| 459 |
let mut value: ::toml::value::Table = ::toml::from_str(input).unwrap(); |
| 460 |
value.remove("f").unwrap() |
| 461 |
} |
| 462 |
|
| 463 |
fn toml(input: &str) -> ::toml::Value { |
| 464 |
let value = ::toml::from_str(input); |
| 465 |
assert!(value.is_ok()); |
| 466 |
value.unwrap() |
| 467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn function_parse_ignore() { |
| 471 |
let toml = toml( |
| 472 |
r#" |
| 473 |
name = "func1" |
| 474 |
ignore = true |
| 475 |
"#, |
| 476 |
); |
| 477 |
let f = Function::parse(&toml, "a").unwrap(); |
| 478 |
assert_eq!(f.ident, Ident::Name("func1".into())); |
| 479 |
assert!(f.status.ignored()); |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn function_parse_manual() { |
| 484 |
let toml = toml( |
| 485 |
r#" |
| 486 |
name = "func1" |
| 487 |
manual = true |
| 488 |
"#, |
| 489 |
); |
| 490 |
let f = Function::parse(&toml, "a").unwrap(); |
| 491 |
assert_eq!(f.ident, Ident::Name("func1".into())); |
| 492 |
assert!(f.status.manual()); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn function_parse_version_default() { |
| 497 |
let toml = toml( |
| 498 |
r#" |
| 499 |
name = "func1" |
| 500 |
"#, |
| 501 |
); |
| 502 |
let f = Function::parse(&toml, "a").unwrap(); |
| 503 |
assert_eq!(f.version, None); |
| 504 |
assert!(f.status.need_generate()); |
| 505 |
} |
| 506 |
|
| 507 |
#[test] |
| 508 |
fn function_parse_version() { |
| 509 |
let toml = toml( |
| 510 |
r#" |
| 511 |
name = "func1" |
| 512 |
version = "3.20" |
| 513 |
"#, |
| 514 |
); |
| 515 |
let f = Function::parse(&toml, "a").unwrap(); |
| 516 |
assert_eq!(f.version, Some(Version::new(3, 20, 0))); |
| 517 |
} |
| 518 |
|
| 519 |
#[test] |
| 520 |
fn function_parse_cfg_condition_default() { |
| 521 |
let toml = toml( |
| 522 |
r#" |
| 523 |
name = "func1" |
| 524 |
"#, |
| 525 |
); |
| 526 |
let f = Function::parse(&toml, "a").unwrap(); |
| 527 |
assert_eq!(f.cfg_condition, None); |
| 528 |
} |
| 529 |
|
| 530 |
#[test] |
| 531 |
fn function_parse_cfg_condition() { |
| 532 |
let toml = toml( |
| 533 |
r#" |
| 534 |
name = "func1" |
| 535 |
cfg_condition = 'unix' |
| 536 |
"#, |
| 537 |
); |
| 538 |
let f = Function::parse(&toml, "a").unwrap(); |
| 539 |
assert_eq!(f.cfg_condition, Some("unix".to_string())); |
| 540 |
} |
| 541 |
|
| 542 |
#[test] |
| 543 |
fn function_parse_return_nullable_default1() { |
| 544 |
let toml = toml( |
| 545 |
r#" |
| 546 |
name = "func1" |
| 547 |
"#, |
| 548 |
); |
| 549 |
let f = Function::parse(&toml, "a").unwrap(); |
| 550 |
assert_eq!(f.ret.nullable, None); |
| 551 |
} |
| 552 |
|
| 553 |
#[test] |
| 554 |
fn function_parse_return_nullable_default2() { |
| 555 |
let toml = toml( |
| 556 |
r#" |
| 557 |
name = "func1" |
| 558 |
[return] |
| 559 |
"#, |
| 560 |
); |
| 561 |
let f = Function::parse(&toml, "a").unwrap(); |
| 562 |
assert_eq!(f.ret.nullable, None); |
| 563 |
} |
| 564 |
|
| 565 |
#[test] |
| 566 |
fn function_parse_parameters() { |
| 567 |
let toml = toml( |
| 568 |
r#" |
| 569 |
name = "func1" |
| 570 |
[[parameter]] |
| 571 |
name = "par1" |
| 572 |
[[parameter]] |
| 573 |
name = "par2" |
| 574 |
const = false |
| 575 |
nullable = false |
| 576 |
[[parameter]] |
| 577 |
name = "par3" |
| 578 |
const = true |
| 579 |
nullable = true |
| 580 |
[[parameter]] |
| 581 |
pattern = "par4" |
| 582 |
const = true |
| 583 |
"#, |
| 584 |
); |
| 585 |
let f = Function::parse(&toml, "a").unwrap(); |
| 586 |
let pars = f.parameters; |
| 587 |
assert_eq!(pars.len(), 4); |
| 588 |
assert_eq!(pars[0].ident, Ident::Name("par1".into())); |
| 589 |
assert!(!pars[0].constant); |
| 590 |
assert_eq!(pars[0].nullable, None); |
| 591 |
assert_eq!(pars[1].ident, Ident::Name("par2".into())); |
| 592 |
assert!(!pars[1].constant); |
| 593 |
assert_eq!(pars[1].nullable, Some(Nullable(false))); |
| 594 |
assert_eq!(pars[2].ident, Ident::Name("par3".into())); |
| 595 |
assert!(pars[2].constant); |
| 596 |
assert_eq!(pars[2].nullable, Some(Nullable(true))); |
| 597 |
assert!(matches!(pars[3].ident, Ident::Pattern(_))); |
| 598 |
assert!(pars[3].constant); |
| 599 |
assert_eq!(pars[3].nullable, None); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn function_parse_return_nullable_false() { |
| 604 |
let toml = toml( |
| 605 |
r#" |
| 606 |
name = "func1" |
| 607 |
[return] |
| 608 |
nullable = false |
| 609 |
"#, |
| 610 |
); |
| 611 |
let f = Function::parse(&toml, "a").unwrap(); |
| 612 |
assert_eq!(f.ret.nullable, Some(Nullable(false))); |
| 613 |
} |
| 614 |
|
| 615 |
#[test] |
| 616 |
fn function_parse_return_nullable_true() { |
| 617 |
let toml = toml( |
| 618 |
r#" |
| 619 |
name = "func1" |
| 620 |
[return] |
| 621 |
nullable = true |
| 622 |
"#, |
| 623 |
); |
| 624 |
let f = Function::parse(&toml, "a").unwrap(); |
| 625 |
assert_eq!(f.ret.nullable, Some(Nullable(true))); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn function_parse_generate_doc() { |
| 630 |
let r = toml( |
| 631 |
r#" |
| 632 |
name = "prop" |
| 633 |
generate_doc = false |
| 634 |
"#, |
| 635 |
); |
| 636 |
let f = Function::parse(&r, "a").unwrap(); |
| 637 |
assert!(!f.generate_doc); |
| 638 |
|
| 639 |
// Ensure that the default value is "true". |
| 640 |
let r = toml( |
| 641 |
r#" |
| 642 |
name = "prop" |
| 643 |
"#, |
| 644 |
); |
| 645 |
let f = Function::parse(&r, "a").unwrap(); |
| 646 |
assert!(f.generate_doc); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn functions_parse_empty_for_none() { |
| 651 |
let fns = Functions::parse(None, "a"); |
| 652 |
assert!(fns.is_empty()); |
| 653 |
} |
| 654 |
|
| 655 |
#[test] |
| 656 |
fn functions_parse_ident() { |
| 657 |
let toml = functions_toml( |
| 658 |
r#" |
| 659 |
[[f]] |
| 660 |
name = "func1" |
| 661 |
[[f]] |
| 662 |
not_name = "func1.5" |
| 663 |
[[f]] |
| 664 |
name = "func2" |
| 665 |
[[f]] |
| 666 |
pattern = 'func3\w+' |
| 667 |
[[f]] |
| 668 |
pattern = 'bad_func4[\w+' |
| 669 |
"#, |
| 670 |
); |
| 671 |
let fns = Functions::parse(Some(&toml), "a"); |
| 672 |
assert_eq!(fns.len(), 3); |
| 673 |
assert_eq!(fns[0].ident, Ident::Name("func1".into())); |
| 674 |
assert_eq!(fns[1].ident, Ident::Name("func2".into())); |
| 675 |
assert!(matches!(fns[2].ident, Ident::Pattern(_))); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn functions_parse_matches() { |
| 680 |
let toml = functions_toml( |
| 681 |
r#" |
| 682 |
[[f]] |
| 683 |
name = "func1" |
| 684 |
[[f]] |
| 685 |
name = "f1.5" |
| 686 |
[[f]] |
| 687 |
name = "func2" |
| 688 |
[[f]] |
| 689 |
pattern = 'func\d+' |
| 690 |
"#, |
| 691 |
); |
| 692 |
let fns = Functions::parse(Some(&toml), "a"); |
| 693 |
assert_eq!(fns.len(), 3); |
| 694 |
|
| 695 |
assert_eq!(fns.matched("func1").len(), 2); |
| 696 |
assert_eq!(fns.matched("func2").len(), 2); |
| 697 |
assert_eq!(fns.matched("func3").len(), 1); |
| 698 |
// "f1.5" is not a valid name |
| 699 |
assert_eq!(fns.matched("f1.5").len(), 0); |
| 700 |
assert_eq!(fns.matched("none").len(), 0); |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn functions_parse_matched_parameters() { |
| 705 |
let toml = functions_toml( |
| 706 |
r#" |
| 707 |
[[f]] |
| 708 |
name = "func" |
| 709 |
[[f.parameter]] |
| 710 |
name="par1" |
| 711 |
[[f.parameter]] |
| 712 |
name="par2" |
| 713 |
[[f.parameter]] |
| 714 |
pattern='par\d+' |
| 715 |
[[f]] |
| 716 |
name = "func" |
| 717 |
[[f.parameter]] |
| 718 |
name="par2" |
| 719 |
[[f.parameter]] |
| 720 |
name="par3" |
| 721 |
[[f.parameter]] |
| 722 |
pattern='par\d+' |
| 723 |
"#, |
| 724 |
); |
| 725 |
let fns = Functions::parse(Some(&toml), "a"); |
| 726 |
assert_eq!(fns.len(), 2); |
| 727 |
let m = fns.matched("func"); |
| 728 |
assert_eq!(m.len(), 2); |
| 729 |
|
| 730 |
assert_eq!(m.matched_parameters("param").len(), 0); |
| 731 |
assert_eq!(m.matched_parameters("par1").len(), 3); |
| 732 |
assert_eq!(m.matched_parameters("par2").len(), 4); |
| 733 |
assert_eq!(m.matched_parameters("par3").len(), 3); |
| 734 |
assert_eq!(m.matched_parameters("par4").len(), 2); |
| 735 |
} |
| 736 |
|
| 737 |
#[test] |
| 738 |
fn functions_parse_rename() { |
| 739 |
let toml = toml( |
| 740 |
r#" |
| 741 |
name = "func1" |
| 742 |
rename = "another" |
| 743 |
"#, |
| 744 |
); |
| 745 |
let f = Function::parse(&toml, "a").unwrap(); |
| 746 |
assert_eq!(f.rename, Some("another".to_owned())); |
| 747 |
} |
| 748 |
|
| 749 |
#[test] |
| 750 |
fn functions_parse_rename_fail() { |
| 751 |
let toml = toml( |
| 752 |
r#" |
| 753 |
name = "func1" |
| 754 |
rename = "anoth er" |
| 755 |
"#, |
| 756 |
); |
| 757 |
let f = Function::parse(&toml, "a"); |
| 758 |
assert!(f.is_none()); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn function_bypass_auto_rename() { |
| 763 |
let toml = toml( |
| 764 |
r#" |
| 765 |
name = "func1" |
| 766 |
bypass_auto_rename = true |
| 767 |
"#, |
| 768 |
); |
| 769 |
let f = Function::parse(&toml, "a").unwrap(); |
| 770 |
assert_eq!(f.ident, Ident::Name("func1".into())); |
| 771 |
assert!(f.bypass_auto_rename); |
| 772 |
} |
| 773 |
|
| 774 |
#[test] |
| 775 |
fn parse_return_mandatory_default() { |
| 776 |
let toml = toml( |
| 777 |
r#" |
| 778 |
name = "func1" |
| 779 |
"#, |
| 780 |
); |
| 781 |
let f = Function::parse(&toml, "a"); |
| 782 |
let f = f.unwrap(); |
| 783 |
assert!(f.ret.mandatory.is_none()); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn parse_return_mandatory() { |
| 788 |
let toml = toml( |
| 789 |
r#" |
| 790 |
name = "func1" |
| 791 |
[return] |
| 792 |
mandatory = true |
| 793 |
"#, |
| 794 |
); |
| 795 |
let f = Function::parse(&toml, "a"); |
| 796 |
let f = f.unwrap(); |
| 797 |
assert_eq!(f.ret.mandatory, Some(Mandatory(true))); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn parse_return_non_mandatory() { |
| 802 |
let toml = toml( |
| 803 |
r#" |
| 804 |
name = "func1" |
| 805 |
[return] |
| 806 |
mandatory = false |
| 807 |
"#, |
| 808 |
); |
| 809 |
let f = Function::parse(&toml, "a"); |
| 810 |
let f = f.unwrap(); |
| 811 |
assert_eq!(f.ret.mandatory, Some(Mandatory(false))); |
| 812 |
} |
| 813 |
|
| 814 |
#[test] |
| 815 |
fn parse_parameter_mandatory_default() { |
| 816 |
let toml = toml( |
| 817 |
r#" |
| 818 |
name = "func1" |
| 819 |
[[parameter]] |
| 820 |
name = "param1" |
| 821 |
"#, |
| 822 |
); |
| 823 |
let f = Function::parse(&toml, "a"); |
| 824 |
let f = f.unwrap(); |
| 825 |
let param1 = &f.parameters[0]; |
| 826 |
assert!(param1.mandatory.is_none()); |
| 827 |
} |
| 828 |
|
| 829 |
#[test] |
| 830 |
fn parse_parameter_mandatory() { |
| 831 |
let toml = toml( |
| 832 |
r#" |
| 833 |
name = "func1" |
| 834 |
[[parameter]] |
| 835 |
name = "param1" |
| 836 |
mandatory = true |
| 837 |
"#, |
| 838 |
); |
| 839 |
let f = Function::parse(&toml, "a"); |
| 840 |
let f = f.unwrap(); |
| 841 |
let param1 = &f.parameters[0]; |
| 842 |
assert_eq!(param1.mandatory, Some(Mandatory(true))); |
| 843 |
} |
| 844 |
|
| 845 |
#[test] |
| 846 |
fn parse_parameter_non_mandatory() { |
| 847 |
let toml = toml( |
| 848 |
r#" |
| 849 |
name = "func1" |
| 850 |
[[parameter]] |
| 851 |
name = "param1" |
| 852 |
mandatory = false |
| 853 |
"#, |
| 854 |
); |
| 855 |
let f = Function::parse(&toml, "a"); |
| 856 |
let f = f.unwrap(); |
| 857 |
let param1 = &f.parameters[0]; |
| 858 |
assert_eq!(param1.mandatory, Some(Mandatory(false))); |
| 859 |
} |
| 860 |
|
| 861 |
#[test] |
| 862 |
fn parse_return_infallible_default() { |
| 863 |
let toml = toml( |
| 864 |
r#" |
| 865 |
name = "func1" |
| 866 |
"#, |
| 867 |
); |
| 868 |
let f = Function::parse(&toml, "a"); |
| 869 |
let f = f.unwrap(); |
| 870 |
assert!(f.ret.infallible.is_none()); |
| 871 |
} |
| 872 |
|
| 873 |
#[test] |
| 874 |
fn parse_return_infallible() { |
| 875 |
let toml = toml( |
| 876 |
r#" |
| 877 |
name = "func1" |
| 878 |
[return] |
| 879 |
infallible = true |
| 880 |
"#, |
| 881 |
); |
| 882 |
let f = Function::parse(&toml, "a"); |
| 883 |
let f = f.unwrap(); |
| 884 |
assert_eq!(f.ret.infallible, Some(Infallible(true))); |
| 885 |
} |
| 886 |
|
| 887 |
#[test] |
| 888 |
fn parse_return_faillible() { |
| 889 |
let toml = toml( |
| 890 |
r#" |
| 891 |
name = "func1" |
| 892 |
[return] |
| 893 |
infallible = false |
| 894 |
"#, |
| 895 |
); |
| 896 |
let f = Function::parse(&toml, "a"); |
| 897 |
let f = f.unwrap(); |
| 898 |
assert_eq!(f.ret.infallible, Some(Infallible(false))); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn parse_parameter_infallible_default() { |
| 903 |
let toml = toml( |
| 904 |
r#" |
| 905 |
name = "func1" |
| 906 |
[[parameter]] |
| 907 |
name = "param1" |
| 908 |
"#, |
| 909 |
); |
| 910 |
let f = Function::parse(&toml, "a"); |
| 911 |
let f = f.unwrap(); |
| 912 |
let param1 = &f.parameters[0]; |
| 913 |
assert!(param1.infallible.is_none()); |
| 914 |
} |
| 915 |
|
| 916 |
#[test] |
| 917 |
fn parse_parameter_infallible() { |
| 918 |
let toml = toml( |
| 919 |
r#" |
| 920 |
name = "func1" |
| 921 |
[[parameter]] |
| 922 |
name = "param1" |
| 923 |
infallible = true |
| 924 |
"#, |
| 925 |
); |
| 926 |
let f = Function::parse(&toml, "a"); |
| 927 |
let f = f.unwrap(); |
| 928 |
let param1 = &f.parameters[0]; |
| 929 |
assert_eq!(param1.infallible, Some(Infallible(true))); |
| 930 |
} |
| 931 |
|
| 932 |
#[test] |
| 933 |
fn parse_parameter_faillible() { |
| 934 |
let toml = toml( |
| 935 |
r#" |
| 936 |
name = "func1" |
| 937 |
[[parameter]] |
| 938 |
name = "param1" |
| 939 |
infallible = false |
| 940 |
"#, |
| 941 |
); |
| 942 |
let f = Function::parse(&toml, "a"); |
| 943 |
let f = f.unwrap(); |
| 944 |
let param1 = &f.parameters[0]; |
| 945 |
assert_eq!(param1.infallible, Some(Infallible(false))); |
| 946 |
} |
| 947 |
} |