| 1 |
use std::{ |
| 2 |
collections::BTreeMap, |
| 3 |
fmt::{Display, Write as FmtWrite}, |
| 4 |
io::{Result, Write}, |
| 5 |
ops::Index, |
| 6 |
}; |
| 7 |
|
| 8 |
use super::Visibility; |
| 9 |
use crate::{ |
| 10 |
analysis::{ |
| 11 |
self, |
| 12 |
general::StatusedTypeId, |
| 13 |
imports::{ImportConditions, Imports}, |
| 14 |
namespaces, |
| 15 |
out_parameters::{Mode, ThrowFunctionReturnStrategy}, |
| 16 |
special_functions::TraitInfo, |
| 17 |
}, |
| 18 |
config::{Config, derives::Derive}, |
| 19 |
env::Env, |
| 20 |
gir_version::VERSION, |
| 21 |
library::TypeId, |
| 22 |
nameutil::use_glib_type, |
| 23 |
version::Version, |
| 24 |
writer::primitives::tabs, |
| 25 |
}; |
| 26 |
|
| 27 |
pub fn start_comments(w: &mut dyn Write, conf: &Config) -> Result<()> { |
| 28 |
if conf.single_version_file.is_some() { |
| 29 |
start_comments_no_version(w, conf) |
| 30 |
} else { |
| 31 |
single_version_file(w, conf, "// ")?; |
| 32 |
writeln!(w, "// DO NOT EDIT") |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
pub fn start_comments_no_version(w: &mut dyn Write, conf: &Config) -> Result<()> { |
| 37 |
writeln!( |
| 38 |
w, |
| 39 |
"// This file was generated by gir (https://github.com/gtk-rs/gir)" |
| 40 |
)?; |
| 41 |
write!( |
| 42 |
w, |
| 43 |
"{}", |
| 44 |
conf.girs_version |
| 45 |
.iter() |
| 46 |
.fold(String::new(), |mut output, info| { |
| 47 |
writeln!( |
| 48 |
output, |
| 49 |
"// from {}{}", |
| 50 |
info.gir_dir.display(), |
| 51 |
info.get_repository_url() |
| 52 |
.map_or_else(String::new, |url| format!(" ({url})")), |
| 53 |
) |
| 54 |
.unwrap(); |
| 55 |
output |
| 56 |
}) |
| 57 |
)?; |
| 58 |
writeln!(w, "// DO NOT EDIT")?; |
| 59 |
Ok(()) |
| 60 |
} |
| 61 |
|
| 62 |
pub fn single_version_file(w: &mut dyn Write, conf: &Config, prefix: &str) -> Result<()> { |
| 63 |
write!( |
| 64 |
w, |
| 65 |
"{}Generated by gir (https://github.com/gtk-rs/gir @ {}) |
| 66 |
{}", |
| 67 |
prefix, |
| 68 |
VERSION, |
| 69 |
conf.girs_version |
| 70 |
.iter() |
| 71 |
.fold(String::new(), |mut output, info| { |
| 72 |
match (info.get_repository_url(), info.get_hash()) { |
| 73 |
(Some(url), Some(hash)) => writeln!( |
| 74 |
output, |
| 75 |
"{}from {} ({} @ {})", |
| 76 |
prefix, |
| 77 |
info.gir_dir.display(), |
| 78 |
url, |
| 79 |
hash, |
| 80 |
), |
| 81 |
(None, Some(hash)) => { |
| 82 |
writeln!( |
| 83 |
output, |
| 84 |
"{}from {} (@ {})", |
| 85 |
prefix, |
| 86 |
info.gir_dir.display(), |
| 87 |
hash, |
| 88 |
) |
| 89 |
} |
| 90 |
_ => writeln!(output, "{}from {}", prefix, info.gir_dir.display()), |
| 91 |
} |
| 92 |
.unwrap(); |
| 93 |
output |
| 94 |
}), |
| 95 |
) |
| 96 |
} |
| 97 |
|
| 98 |
pub fn uses( |
| 99 |
w: &mut dyn Write, |
| 100 |
env: &Env, |
| 101 |
imports: &Imports, |
| 102 |
outer_version: Option<Version>, |
| 103 |
) -> Result<()> { |
| 104 |
writeln!(w)?; |
| 105 |
|
| 106 |
let mut grouped_imports: BTreeMap<(&str, Option<ImportConditions>), Vec<&str>> = |
| 107 |
BTreeMap::new(); |
| 108 |
|
| 109 |
for (name, scope) in imports.iter() { |
| 110 |
let (crate_name, import) = name.split_once("::").unwrap(); |
| 111 |
let mut scope = scope.clone(); |
| 112 |
|
| 113 |
// The check here is needed to group unneeded version guards and allow grouping those imports |
| 114 |
scope.version = Version::if_stricter_than(scope.version, outer_version); |
| 115 |
let to_compare_with = env.config.min_required_version(env, None); |
| 116 |
scope.version = match (scope.version, to_compare_with) { |
| 117 |
(Some(v), Some(to_compare_v)) => { |
| 118 |
if v > to_compare_v { |
| 119 |
scope.version |
| 120 |
} else { |
| 121 |
None |
| 122 |
} |
| 123 |
} |
| 124 |
(Some(_), _) => scope.version, |
| 125 |
_ => None, |
| 126 |
}; |
| 127 |
|
| 128 |
let key = if scope.constraints.is_empty() && scope.version.is_none() { |
| 129 |
(crate_name, None) |
| 130 |
} else { |
| 131 |
(crate_name, Some(scope)) |
| 132 |
}; |
| 133 |
grouped_imports |
| 134 |
.entry(key) |
| 135 |
.and_modify(|entry| entry.push(import)) |
| 136 |
.or_insert_with(|| vec![import]); |
| 137 |
} |
| 138 |
|
| 139 |
for ((crate_name, scope), names) in grouped_imports.iter() { |
| 140 |
if !scope.is_none() { |
| 141 |
let scope = scope.as_ref().unwrap(); |
| 142 |
if !scope.constraints.is_empty() { |
| 143 |
if scope.constraints.len() == 1 { |
| 144 |
writeln!(w, "#[cfg({})]", scope.constraints[0])?; |
| 145 |
} else { |
| 146 |
writeln!(w, "#[cfg(any({}))]", scope.constraints.join(", "))?; |
| 147 |
} |
| 148 |
writeln!( |
| 149 |
w, |
| 150 |
"#[cfg_attr(docsrs, doc(cfg({})))]", |
| 151 |
scope.constraints.join(", ") |
| 152 |
)?; |
| 153 |
} |
| 154 |
version_condition(w, env, None, scope.version, false, 0)?; |
| 155 |
} |
| 156 |
writeln!(w, "use {crate_name}::{{{}}};", names.join(","))?; |
| 157 |
} |
| 158 |
|
| 159 |
Ok(()) |
| 160 |
} |
| 161 |
|
| 162 |
fn format_parent_name(env: &Env, p: &StatusedTypeId) -> String { |
| 163 |
if p.type_id.ns_id == namespaces::MAIN { |
| 164 |
p.name.clone() |
| 165 |
} else { |
| 166 |
format!( |
| 167 |
"{krate}::{name}", |
| 168 |
krate = env.namespaces[p.type_id.ns_id].crate_name, |
| 169 |
name = p.name, |
| 170 |
) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
pub fn define_fundamental_type( |
| 175 |
w: &mut dyn Write, |
| 176 |
env: &Env, |
| 177 |
type_name: &str, |
| 178 |
glib_name: &str, |
| 179 |
glib_func_name: &str, |
| 180 |
ref_func: Option<&str>, |
| 181 |
unref_func: Option<&str>, |
| 182 |
parents: &[StatusedTypeId], |
| 183 |
visibility: Visibility, |
| 184 |
type_id: TypeId, |
| 185 |
) -> Result<()> { |
| 186 |
let sys_crate_name = env.sys_crate_import(type_id); |
| 187 |
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?; |
| 188 |
doc_alias(w, glib_name, "", 1)?; |
| 189 |
external_doc_link( |
| 190 |
w, |
| 191 |
env.config.external_docs_url.as_deref(), |
| 192 |
type_name, |
| 193 |
&visibility, |
| 194 |
1, |
| 195 |
)?; |
| 196 |
writeln!( |
| 197 |
w, |
| 198 |
"\t{visibility} struct {type_name}(Shared<{sys_crate_name}::{glib_name}>);" |
| 199 |
)?; |
| 200 |
writeln!(w)?; |
| 201 |
|
| 202 |
writeln!(w, "\tmatch fn {{")?; |
| 203 |
let (ref_fn, unref_fn, ptr, ffi_crate_name) = if parents.is_empty() { |
| 204 |
// it's the super-type, it must have a ref/unref functions |
| 205 |
( |
| 206 |
ref_func.unwrap().to_owned(), |
| 207 |
unref_func.unwrap().to_owned(), |
| 208 |
"ptr".to_owned(), |
| 209 |
sys_crate_name.to_owned(), |
| 210 |
) |
| 211 |
} else { |
| 212 |
let (ref_fn, unref_fn, ptr, ffi_crate_name) = parents |
| 213 |
.iter() |
| 214 |
.find_map(|p| { |
| 215 |
use crate::library::*; |
| 216 |
let type_ = env.library.type_(p.type_id); |
| 217 |
let parent_sys_crate_name = env.sys_crate_import(p.type_id); |
| 218 |
match type_ { |
| 219 |
Type::Class(class) => Some(( |
| 220 |
class.ref_fn.as_ref().unwrap().clone(), |
| 221 |
class.unref_fn.as_ref().unwrap().clone(), |
| 222 |
format!( |
| 223 |
"ptr as *mut {}::{}", |
| 224 |
parent_sys_crate_name, |
| 225 |
class.c_type.clone() |
| 226 |
), |
| 227 |
parent_sys_crate_name, |
| 228 |
)), |
| 229 |
_ => None, |
| 230 |
} |
| 231 |
}) |
| 232 |
.unwrap(); |
| 233 |
// otherwise get that information from the parent |
| 234 |
(ref_fn, unref_fn, ptr, ffi_crate_name) |
| 235 |
}; |
| 236 |
|
| 237 |
writeln!(w, "\t\tref => |ptr| {ffi_crate_name}::{ref_fn}({ptr}),")?; |
| 238 |
writeln!(w, "\t\tunref => |ptr| {ffi_crate_name}::{unref_fn}({ptr}),")?; |
| 239 |
|
| 240 |
writeln!(w, "\t}}")?; |
| 241 |
writeln!(w, "}}")?; |
| 242 |
|
| 243 |
// We can't use type_ from glib::wrapper! because that would auto-implement |
| 244 |
// Value traits which are often not the correct types |
| 245 |
writeln!(w, "\n")?; |
| 246 |
writeln!(w, "impl StaticType for {type_name} {{")?; |
| 247 |
writeln!(w, "\tfn static_type() -> {} {{", use_glib_type(env, "Type"))?; |
| 248 |
writeln!( |
| 249 |
w, |
| 250 |
"\t\t unsafe {{ from_glib({sys_crate_name}::{glib_func_name}()) }}" |
| 251 |
)?; |
| 252 |
writeln!(w, "\t}}")?; |
| 253 |
writeln!(w, "}}")?; |
| 254 |
Ok(()) |
| 255 |
} |
| 256 |
|
| 257 |
pub fn define_object_type( |
| 258 |
w: &mut dyn Write, |
| 259 |
env: &Env, |
| 260 |
type_name: &str, |
| 261 |
glib_name: &str, |
| 262 |
glib_class_name: Option<&str>, |
| 263 |
glib_func_name: &str, |
| 264 |
is_interface: bool, |
| 265 |
parents: &[StatusedTypeId], |
| 266 |
visibility: Visibility, |
| 267 |
type_id: TypeId, |
| 268 |
) -> Result<()> { |
| 269 |
let sys_crate_name = env.sys_crate_import(type_id); |
| 270 |
let class_name = { |
| 271 |
if let Some(s) = glib_class_name { |
| 272 |
format!(", {sys_crate_name}::{s}") |
| 273 |
} else { |
| 274 |
String::new() |
| 275 |
} |
| 276 |
}; |
| 277 |
|
| 278 |
let kind_name = if is_interface { "Interface" } else { "Object" }; |
| 279 |
|
| 280 |
let parents: Vec<StatusedTypeId> = parents |
| 281 |
.iter() |
| 282 |
.filter(|p| !p.status.ignored()) |
| 283 |
.cloned() |
| 284 |
.collect(); |
| 285 |
|
| 286 |
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?; |
| 287 |
doc_alias(w, glib_name, "", 1)?; |
| 288 |
external_doc_link( |
| 289 |
w, |
| 290 |
env.config.external_docs_url.as_deref(), |
| 291 |
type_name, |
| 292 |
&visibility, |
| 293 |
1, |
| 294 |
)?; |
| 295 |
if parents.is_empty() { |
| 296 |
writeln!( |
| 297 |
w, |
| 298 |
"\t{visibility} struct {type_name}({kind_name}<{sys_crate_name}::{glib_name}{class_name}>);" |
| 299 |
)?; |
| 300 |
} else if is_interface { |
| 301 |
let prerequisites: Vec<String> = |
| 302 |
parents.iter().map(|p| format_parent_name(env, p)).collect(); |
| 303 |
|
| 304 |
writeln!( |
| 305 |
w, |
| 306 |
"\t{} struct {}(Interface<{}::{}{}>) @requires {};", |
| 307 |
visibility, |
| 308 |
type_name, |
| 309 |
sys_crate_name, |
| 310 |
glib_name, |
| 311 |
class_name, |
| 312 |
prerequisites.join(", ") |
| 313 |
)?; |
| 314 |
} else { |
| 315 |
let interfaces: Vec<String> = parents |
| 316 |
.iter() |
| 317 |
.filter(|p| { |
| 318 |
use crate::library::*; |
| 319 |
|
| 320 |
matches!( |
| 321 |
*env.library.type_(p.type_id), |
| 322 |
Type::Interface { .. } if !p.status.ignored() |
| 323 |
) |
| 324 |
}) |
| 325 |
.map(|p| format_parent_name(env, p)) |
| 326 |
.collect(); |
| 327 |
|
| 328 |
let parents: Vec<String> = parents |
| 329 |
.iter() |
| 330 |
.filter(|p| { |
| 331 |
use crate::library::*; |
| 332 |
|
| 333 |
matches!( |
| 334 |
*env.library.type_(p.type_id), |
| 335 |
Type::Class { .. } if !p.status.ignored() |
| 336 |
) |
| 337 |
}) |
| 338 |
.map(|p| format_parent_name(env, p)) |
| 339 |
.collect(); |
| 340 |
|
| 341 |
let mut parents_string = String::new(); |
| 342 |
if !parents.is_empty() { |
| 343 |
parents_string.push_str(format!(" @extends {}", parents.join(", ")).as_str()); |
| 344 |
} |
| 345 |
|
| 346 |
if !interfaces.is_empty() { |
| 347 |
if !parents.is_empty() { |
| 348 |
parents_string.push(','); |
| 349 |
} |
| 350 |
parents_string.push_str(format!(" @implements {}", interfaces.join(", ")).as_str()); |
| 351 |
} |
| 352 |
|
| 353 |
writeln!( |
| 354 |
w, |
| 355 |
"\t{visibility} struct {type_name}(Object<{sys_crate_name}::{glib_name}{class_name}>){parents_string};", |
| 356 |
)?; |
| 357 |
} |
| 358 |
writeln!(w)?; |
| 359 |
writeln!(w, "\tmatch fn {{")?; |
| 360 |
writeln!(w, "\t\ttype_ => || {sys_crate_name}::{glib_func_name}(),")?; |
| 361 |
writeln!(w, "\t}}")?; |
| 362 |
writeln!(w, "}}")?; |
| 363 |
|
| 364 |
Ok(()) |
| 365 |
} |
| 366 |
|
| 367 |
fn define_boxed_type_internal( |
| 368 |
w: &mut dyn Write, |
| 369 |
env: &Env, |
| 370 |
type_name: &str, |
| 371 |
glib_name: &str, |
| 372 |
copy_fn: &TraitInfo, |
| 373 |
free_fn: &str, |
| 374 |
boxed_inline: bool, |
| 375 |
init_function_expression: &Option<String>, |
| 376 |
copy_into_function_expression: &Option<String>, |
| 377 |
clear_function_expression: &Option<String>, |
| 378 |
get_type_fn: Option<&str>, |
| 379 |
derive: &[Derive], |
| 380 |
visibility: Visibility, |
| 381 |
type_id: TypeId, |
| 382 |
) -> Result<()> { |
| 383 |
let sys_crate_name = env.sys_crate_import(type_id); |
| 384 |
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?; |
| 385 |
|
| 386 |
derives(w, derive, 1)?; |
| 387 |
writeln!( |
| 388 |
w, |
| 389 |
"\t{} struct {}(Boxed{}<{}::{}>);", |
| 390 |
visibility, |
| 391 |
type_name, |
| 392 |
if boxed_inline { "Inline" } else { "" }, |
| 393 |
sys_crate_name, |
| 394 |
glib_name |
| 395 |
)?; |
| 396 |
writeln!(w)?; |
| 397 |
writeln!(w, "\tmatch fn {{")?; |
| 398 |
let mut_ov = if copy_fn.first_parameter_mut { |
| 399 |
"mut_override(ptr)" |
| 400 |
} else { |
| 401 |
"ptr" |
| 402 |
}; |
| 403 |
writeln!( |
| 404 |
w, |
| 405 |
"\t\tcopy => |ptr| {}::{}({}),", |
| 406 |
sys_crate_name, copy_fn.glib_name, mut_ov |
| 407 |
)?; |
| 408 |
writeln!(w, "\t\tfree => |ptr| {sys_crate_name}::{free_fn}(ptr),")?; |
| 409 |
|
| 410 |
if let ( |
| 411 |
Some(init_function_expression), |
| 412 |
Some(copy_into_function_expression), |
| 413 |
Some(clear_function_expression), |
| 414 |
) = ( |
| 415 |
init_function_expression, |
| 416 |
copy_into_function_expression, |
| 417 |
clear_function_expression, |
| 418 |
) { |
| 419 |
writeln!(w, "\t\tinit => {init_function_expression},",)?; |
| 420 |
writeln!(w, "\t\tcopy_into => {copy_into_function_expression},",)?; |
| 421 |
writeln!(w, "\t\tclear => {clear_function_expression},",)?; |
| 422 |
} |
| 423 |
|
| 424 |
if let Some(get_type_fn) = get_type_fn { |
| 425 |
writeln!(w, "\t\ttype_ => || {sys_crate_name}::{get_type_fn}(),")?; |
| 426 |
} |
| 427 |
writeln!(w, "\t}}")?; |
| 428 |
writeln!(w, "}}")?; |
| 429 |
|
| 430 |
Ok(()) |
| 431 |
} |
| 432 |
|
| 433 |
pub fn define_boxed_type( |
| 434 |
w: &mut dyn Write, |
| 435 |
env: &Env, |
| 436 |
type_name: &str, |
| 437 |
glib_name: &str, |
| 438 |
copy_fn: &TraitInfo, |
| 439 |
free_fn: &str, |
| 440 |
boxed_inline: bool, |
| 441 |
init_function_expression: &Option<String>, |
| 442 |
copy_into_function_expression: &Option<String>, |
| 443 |
clear_function_expression: &Option<String>, |
| 444 |
get_type_fn: Option<(String, Option<Version>)>, |
| 445 |
derive: &[Derive], |
| 446 |
visibility: Visibility, |
| 447 |
type_id: TypeId, |
| 448 |
) -> Result<()> { |
| 449 |
writeln!(w)?; |
| 450 |
|
| 451 |
if let Some((ref get_type_fn, get_type_version)) = get_type_fn { |
| 452 |
if get_type_version.is_some() { |
| 453 |
version_condition(w, env, None, get_type_version, false, 0)?; |
| 454 |
define_boxed_type_internal( |
| 455 |
w, |
| 456 |
env, |
| 457 |
type_name, |
| 458 |
glib_name, |
| 459 |
copy_fn, |
| 460 |
free_fn, |
| 461 |
boxed_inline, |
| 462 |
init_function_expression, |
| 463 |
copy_into_function_expression, |
| 464 |
clear_function_expression, |
| 465 |
Some(get_type_fn), |
| 466 |
derive, |
| 467 |
visibility, |
| 468 |
type_id, |
| 469 |
)?; |
| 470 |
|
| 471 |
writeln!(w)?; |
| 472 |
not_version_condition_no_docsrs(w, env, None, get_type_version, false, 0)?; |
| 473 |
define_boxed_type_internal( |
| 474 |
w, |
| 475 |
env, |
| 476 |
type_name, |
| 477 |
glib_name, |
| 478 |
copy_fn, |
| 479 |
free_fn, |
| 480 |
boxed_inline, |
| 481 |
init_function_expression, |
| 482 |
copy_into_function_expression, |
| 483 |
clear_function_expression, |
| 484 |
None, |
| 485 |
derive, |
| 486 |
visibility, |
| 487 |
type_id, |
| 488 |
)?; |
| 489 |
} else { |
| 490 |
define_boxed_type_internal( |
| 491 |
w, |
| 492 |
env, |
| 493 |
type_name, |
| 494 |
glib_name, |
| 495 |
copy_fn, |
| 496 |
free_fn, |
| 497 |
boxed_inline, |
| 498 |
init_function_expression, |
| 499 |
copy_into_function_expression, |
| 500 |
clear_function_expression, |
| 501 |
Some(get_type_fn), |
| 502 |
derive, |
| 503 |
visibility, |
| 504 |
type_id, |
| 505 |
)?; |
| 506 |
} |
| 507 |
} else { |
| 508 |
define_boxed_type_internal( |
| 509 |
w, |
| 510 |
env, |
| 511 |
type_name, |
| 512 |
glib_name, |
| 513 |
copy_fn, |
| 514 |
free_fn, |
| 515 |
boxed_inline, |
| 516 |
init_function_expression, |
| 517 |
copy_into_function_expression, |
| 518 |
clear_function_expression, |
| 519 |
None, |
| 520 |
derive, |
| 521 |
visibility, |
| 522 |
type_id, |
| 523 |
)?; |
| 524 |
} |
| 525 |
|
| 526 |
Ok(()) |
| 527 |
} |
| 528 |
|
| 529 |
pub fn define_auto_boxed_type( |
| 530 |
w: &mut dyn Write, |
| 531 |
env: &Env, |
| 532 |
type_name: &str, |
| 533 |
glib_name: &str, |
| 534 |
boxed_inline: bool, |
| 535 |
init_function_expression: &Option<String>, |
| 536 |
copy_into_function_expression: &Option<String>, |
| 537 |
clear_function_expression: &Option<String>, |
| 538 |
get_type_fn: &str, |
| 539 |
derive: &[Derive], |
| 540 |
visibility: Visibility, |
| 541 |
type_id: TypeId, |
| 542 |
) -> Result<()> { |
| 543 |
let sys_crate_name = env.sys_crate_import(type_id); |
| 544 |
writeln!(w)?; |
| 545 |
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?; |
| 546 |
derives(w, derive, 1)?; |
| 547 |
writeln!( |
| 548 |
w, |
| 549 |
"\t{} struct {}(Boxed{}<{}::{}>);", |
| 550 |
visibility, |
| 551 |
type_name, |
| 552 |
if boxed_inline { "Inline" } else { "" }, |
| 553 |
sys_crate_name, |
| 554 |
glib_name |
| 555 |
)?; |
| 556 |
writeln!(w)?; |
| 557 |
writeln!(w, "\tmatch fn {{")?; |
| 558 |
writeln!( |
| 559 |
w, |
| 560 |
"\t\tcopy => |ptr| {}({}::{}(), ptr as *mut _) as *mut {}::{},", |
| 561 |
use_glib_type(env, "gobject_ffi::g_boxed_copy"), |
| 562 |
sys_crate_name, |
| 563 |
get_type_fn, |
| 564 |
sys_crate_name, |
| 565 |
glib_name |
| 566 |
)?; |
| 567 |
writeln!( |
| 568 |
w, |
| 569 |
"\t\tfree => |ptr| {}({}::{}(), ptr as *mut _),", |
| 570 |
use_glib_type(env, "gobject_ffi::g_boxed_free"), |
| 571 |
sys_crate_name, |
| 572 |
get_type_fn |
| 573 |
)?; |
| 574 |
|
| 575 |
if let ( |
| 576 |
Some(init_function_expression), |
| 577 |
Some(copy_into_function_expression), |
| 578 |
Some(clear_function_expression), |
| 579 |
) = ( |
| 580 |
init_function_expression, |
| 581 |
copy_into_function_expression, |
| 582 |
clear_function_expression, |
| 583 |
) { |
| 584 |
writeln!(w, "\t\tinit => {init_function_expression},",)?; |
| 585 |
writeln!(w, "\t\tcopy_into => {copy_into_function_expression},",)?; |
| 586 |
writeln!(w, "\t\tclear => {clear_function_expression},",)?; |
| 587 |
} |
| 588 |
|
| 589 |
writeln!(w, "\t\ttype_ => || {sys_crate_name}::{get_type_fn}(),")?; |
| 590 |
writeln!(w, "\t}}")?; |
| 591 |
writeln!(w, "}}")?; |
| 592 |
|
| 593 |
Ok(()) |
| 594 |
} |
| 595 |
|
| 596 |
fn define_shared_type_internal( |
| 597 |
w: &mut dyn Write, |
| 598 |
env: &Env, |
| 599 |
type_name: &str, |
| 600 |
glib_name: &str, |
| 601 |
ref_fn: &str, |
| 602 |
unref_fn: &str, |
| 603 |
get_type_fn: Option<&str>, |
| 604 |
derive: &[Derive], |
| 605 |
visibility: Visibility, |
| 606 |
type_id: TypeId, |
| 607 |
) -> Result<()> { |
| 608 |
let sys_crate_name = env.sys_crate_import(type_id); |
| 609 |
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?; |
| 610 |
derives(w, derive, 1)?; |
| 611 |
writeln!( |
| 612 |
w, |
| 613 |
"\t{visibility} struct {type_name}(Shared<{sys_crate_name}::{glib_name}>);" |
| 614 |
)?; |
| 615 |
writeln!(w)?; |
| 616 |
writeln!(w, "\tmatch fn {{")?; |
| 617 |
writeln!(w, "\t\tref => |ptr| {sys_crate_name}::{ref_fn}(ptr),")?; |
| 618 |
writeln!(w, "\t\tunref => |ptr| {sys_crate_name}::{unref_fn}(ptr),")?; |
| 619 |
if let Some(get_type_fn) = get_type_fn { |
| 620 |
writeln!(w, "\t\ttype_ => || {sys_crate_name}::{get_type_fn}(),")?; |
| 621 |
} |
| 622 |
writeln!(w, "\t}}")?; |
| 623 |
writeln!(w, "}}")?; |
| 624 |
|
| 625 |
Ok(()) |
| 626 |
} |
| 627 |
|
| 628 |
pub fn define_shared_type( |
| 629 |
w: &mut dyn Write, |
| 630 |
env: &Env, |
| 631 |
type_name: &str, |
| 632 |
glib_name: &str, |
| 633 |
ref_fn: &str, |
| 634 |
unref_fn: &str, |
| 635 |
get_type_fn: Option<(String, Option<Version>)>, |
| 636 |
derive: &[Derive], |
| 637 |
visibility: Visibility, |
| 638 |
type_id: TypeId, |
| 639 |
) -> Result<()> { |
| 640 |
writeln!(w)?; |
| 641 |
|
| 642 |
if let Some((ref get_type_fn, get_type_version)) = get_type_fn { |
| 643 |
if get_type_version.is_some() { |
| 644 |
version_condition(w, env, None, get_type_version, false, 0)?; |
| 645 |
define_shared_type_internal( |
| 646 |
w, |
| 647 |
env, |
| 648 |
type_name, |
| 649 |
glib_name, |
| 650 |
ref_fn, |
| 651 |
unref_fn, |
| 652 |
Some(get_type_fn), |
| 653 |
derive, |
| 654 |
visibility, |
| 655 |
type_id, |
| 656 |
)?; |
| 657 |
|
| 658 |
writeln!(w)?; |
| 659 |
not_version_condition_no_docsrs(w, env, None, get_type_version, false, 0)?; |
| 660 |
define_shared_type_internal( |
| 661 |
w, env, type_name, glib_name, ref_fn, unref_fn, None, derive, visibility, type_id, |
| 662 |
)?; |
| 663 |
} else { |
| 664 |
define_shared_type_internal( |
| 665 |
w, |
| 666 |
env, |
| 667 |
type_name, |
| 668 |
glib_name, |
| 669 |
ref_fn, |
| 670 |
unref_fn, |
| 671 |
Some(get_type_fn), |
| 672 |
derive, |
| 673 |
visibility, |
| 674 |
type_id, |
| 675 |
)?; |
| 676 |
} |
| 677 |
} else { |
| 678 |
define_shared_type_internal( |
| 679 |
w, env, type_name, glib_name, ref_fn, unref_fn, None, derive, visibility, type_id, |
| 680 |
)?; |
| 681 |
} |
| 682 |
|
| 683 |
Ok(()) |
| 684 |
} |
| 685 |
|
| 686 |
pub fn cfg_deprecated( |
| 687 |
w: &mut dyn Write, |
| 688 |
env: &Env, |
| 689 |
type_tid: Option<TypeId>, |
| 690 |
deprecated: Option<Version>, |
| 691 |
commented: bool, |
| 692 |
indent: usize, |
| 693 |
) -> Result<()> { |
| 694 |
if let Some(s) = cfg_deprecated_string(env, type_tid, deprecated, commented, indent) { |
| 695 |
writeln!(w, "{s}")?; |
| 696 |
} |
| 697 |
Ok(()) |
| 698 |
} |
| 699 |
|
| 700 |
pub fn cfg_deprecated_string( |
| 701 |
env: &Env, |
| 702 |
type_tid: Option<TypeId>, |
| 703 |
deprecated: Option<Version>, |
| 704 |
commented: bool, |
| 705 |
indent: usize, |
| 706 |
) -> Option<String> { |
| 707 |
let comment = if commented { "//" } else { "" }; |
| 708 |
deprecated.map(|v| { |
| 709 |
if env.is_too_low_version(type_tid.map(|t| t.ns_id), Some(v)) { |
| 710 |
format!("{}{}#[deprecated = \"Since {}\"]", tabs(indent), comment, v) |
| 711 |
} else { |
| 712 |
let namespace_name = type_tid.map(|t| t.ns_id).and_then(|ns| { |
| 713 |
if ns == namespaces::MAIN || ns == crate::library::INTERNAL_NAMESPACE { |
| 714 |
None |
| 715 |
} else { |
| 716 |
Some(env.namespaces.index(ns).crate_name.clone()) |
| 717 |
} |
| 718 |
}); |
| 719 |
format!( |
| 720 |
"{}{}#[cfg_attr({}, deprecated = \"Since {}\")]", |
| 721 |
tabs(indent), |
| 722 |
comment, |
| 723 |
v.to_cfg(namespace_name.as_deref()), |
| 724 |
v, |
| 725 |
) |
| 726 |
} |
| 727 |
}) |
| 728 |
} |
| 729 |
|
| 730 |
pub fn version_condition( |
| 731 |
w: &mut dyn Write, |
| 732 |
env: &Env, |
| 733 |
ns_id: Option<u16>, |
| 734 |
version: Option<Version>, |
| 735 |
commented: bool, |
| 736 |
indent: usize, |
| 737 |
) -> Result<()> { |
| 738 |
if let Some(s) = version_condition_string(env, ns_id, version, commented, indent) { |
| 739 |
writeln!(w, "{s}")?; |
| 740 |
} |
| 741 |
Ok(()) |
| 742 |
} |
| 743 |
|
| 744 |
pub fn version_condition_no_doc( |
| 745 |
w: &mut dyn Write, |
| 746 |
env: &Env, |
| 747 |
ns_id: Option<u16>, |
| 748 |
version: Option<Version>, |
| 749 |
commented: bool, |
| 750 |
indent: usize, |
| 751 |
) -> Result<()> { |
| 752 |
let to_compare_with = env.config.min_required_version(env, ns_id); |
| 753 |
let should_generate = match (version, to_compare_with) { |
| 754 |
(Some(v), Some(to_compare_v)) => v > to_compare_v, |
| 755 |
(Some(_), _) => true, |
| 756 |
_ => false, |
| 757 |
}; |
| 758 |
if should_generate { |
| 759 |
// Prefix with the crate name if it's not the main one |
| 760 |
let namespace_name = ns_id.and_then(|ns| { |
| 761 |
if ns == namespaces::MAIN { |
| 762 |
None |
| 763 |
} else { |
| 764 |
Some(env.namespaces.index(ns).crate_name.clone()) |
| 765 |
} |
| 766 |
}); |
| 767 |
if let Some(s) = cfg_condition_string_no_doc( |
| 768 |
Some(&version.unwrap().to_cfg(namespace_name.as_deref())), |
| 769 |
commented, |
| 770 |
indent, |
| 771 |
) { |
| 772 |
writeln!(w, "{s}")?; |
| 773 |
} |
| 774 |
} |
| 775 |
Ok(()) |
| 776 |
} |
| 777 |
pub fn version_condition_doc( |
| 778 |
w: &mut dyn Write, |
| 779 |
env: &Env, |
| 780 |
version: Option<Version>, |
| 781 |
commented: bool, |
| 782 |
indent: usize, |
| 783 |
) -> Result<()> { |
| 784 |
match version { |
| 785 |
Some(v) if v > env.config.min_cfg_version => { |
| 786 |
if let Some(s) = cfg_condition_string_doc(Some(&v.to_cfg(None)), commented, indent) { |
| 787 |
writeln!(w, "{s}")?; |
| 788 |
} |
| 789 |
} |
| 790 |
_ => {} |
| 791 |
} |
| 792 |
Ok(()) |
| 793 |
} |
| 794 |
|
| 795 |
pub fn version_condition_string( |
| 796 |
env: &Env, |
| 797 |
ns_id: Option<u16>, |
| 798 |
version: Option<Version>, |
| 799 |
commented: bool, |
| 800 |
indent: usize, |
| 801 |
) -> Option<String> { |
| 802 |
let to_compare_with = env.config.min_required_version(env, ns_id); |
| 803 |
let should_generate = match (version, to_compare_with) { |
| 804 |
(Some(v), Some(to_compare_v)) => v > to_compare_v, |
| 805 |
(Some(_), _) => true, |
| 806 |
_ => false, |
| 807 |
}; |
| 808 |
if should_generate { |
| 809 |
// Prefix with the crate name if it's not the main one |
| 810 |
let namespace_name = ns_id.and_then(|ns| { |
| 811 |
if ns == namespaces::MAIN { |
| 812 |
None |
| 813 |
} else { |
| 814 |
Some(env.namespaces.index(ns).crate_name.clone()) |
| 815 |
} |
| 816 |
}); |
| 817 |
cfg_condition_string( |
| 818 |
Some(&version.unwrap().to_cfg(namespace_name.as_deref())), |
| 819 |
commented, |
| 820 |
indent, |
| 821 |
) |
| 822 |
} else { |
| 823 |
None |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
pub fn not_version_condition( |
| 828 |
w: &mut dyn Write, |
| 829 |
version: Option<Version>, |
| 830 |
commented: bool, |
| 831 |
indent: usize, |
| 832 |
) -> Result<()> { |
| 833 |
if let Some(s) = version.and_then(|v| { |
| 834 |
cfg_condition_string(Some(&format!("not({})", v.to_cfg(None))), commented, indent) |
| 835 |
}) { |
| 836 |
writeln!(w, "{s}")?; |
| 837 |
} |
| 838 |
Ok(()) |
| 839 |
} |
| 840 |
|
| 841 |
pub fn not_version_condition_no_docsrs( |
| 842 |
w: &mut dyn Write, |
| 843 |
env: &Env, |
| 844 |
ns_id: Option<u16>, |
| 845 |
version: Option<Version>, |
| 846 |
commented: bool, |
| 847 |
indent: usize, |
| 848 |
) -> Result<()> { |
| 849 |
if let Some(v) = version { |
| 850 |
let comment = if commented { "//" } else { "" }; |
| 851 |
let namespace_name = ns_id.and_then(|ns| { |
| 852 |
if ns == namespaces::MAIN { |
| 853 |
None |
| 854 |
} else { |
| 855 |
Some(env.namespaces.index(ns).crate_name.clone()) |
| 856 |
} |
| 857 |
}); |
| 858 |
let s = format!( |
| 859 |
"{}{}#[cfg(not({}))]", |
| 860 |
tabs(indent), |
| 861 |
comment, |
| 862 |
v.to_cfg(namespace_name.as_deref()) |
| 863 |
); |
| 864 |
writeln!(w, "{s}")?; |
| 865 |
} |
| 866 |
Ok(()) |
| 867 |
} |
| 868 |
|
| 869 |
pub fn cfg_condition( |
| 870 |
w: &mut dyn Write, |
| 871 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 872 |
commented: bool, |
| 873 |
indent: usize, |
| 874 |
) -> Result<()> { |
| 875 |
if let Some(s) = cfg_condition_string(cfg_condition, commented, indent) { |
| 876 |
writeln!(w, "{s}")?; |
| 877 |
} |
| 878 |
Ok(()) |
| 879 |
} |
| 880 |
|
| 881 |
pub fn cfg_condition_no_doc( |
| 882 |
w: &mut dyn Write, |
| 883 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 884 |
commented: bool, |
| 885 |
indent: usize, |
| 886 |
) -> Result<()> { |
| 887 |
if let Some(s) = cfg_condition_string_no_doc(cfg_condition, commented, indent) { |
| 888 |
writeln!(w, "{s}")?; |
| 889 |
} |
| 890 |
Ok(()) |
| 891 |
} |
| 892 |
|
| 893 |
pub fn cfg_condition_string_no_doc( |
| 894 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 895 |
commented: bool, |
| 896 |
indent: usize, |
| 897 |
) -> Option<String> { |
| 898 |
cfg_condition.map(|cfg| { |
| 899 |
let comment = if commented { "//" } else { "" }; |
| 900 |
format!("{0}{1}#[cfg({2})]", tabs(indent), comment, cfg) |
| 901 |
}) |
| 902 |
} |
| 903 |
|
| 904 |
pub fn cfg_condition_doc( |
| 905 |
w: &mut dyn Write, |
| 906 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 907 |
commented: bool, |
| 908 |
indent: usize, |
| 909 |
) -> Result<()> { |
| 910 |
if let Some(s) = cfg_condition_string_doc(cfg_condition, commented, indent) { |
| 911 |
writeln!(w, "{s}")?; |
| 912 |
} |
| 913 |
Ok(()) |
| 914 |
} |
| 915 |
|
| 916 |
pub fn cfg_condition_string_doc( |
| 917 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 918 |
commented: bool, |
| 919 |
indent: usize, |
| 920 |
) -> Option<String> { |
| 921 |
cfg_condition.map(|cfg| { |
| 922 |
let comment = if commented { "//" } else { "" }; |
| 923 |
format!( |
| 924 |
"{0}{1}#[cfg_attr(docsrs, doc(cfg({2})))]", |
| 925 |
tabs(indent), |
| 926 |
comment, |
| 927 |
cfg, |
| 928 |
) |
| 929 |
}) |
| 930 |
} |
| 931 |
|
| 932 |
pub fn cfg_condition_string( |
| 933 |
cfg_condition: Option<&(impl Display + ?Sized)>, |
| 934 |
commented: bool, |
| 935 |
indent: usize, |
| 936 |
) -> Option<String> { |
| 937 |
cfg_condition.map(|_| { |
| 938 |
format!( |
| 939 |
"{}\n{}", |
| 940 |
cfg_condition_string_no_doc(cfg_condition, commented, indent).unwrap(), |
| 941 |
cfg_condition_string_doc(cfg_condition, commented, indent).unwrap(), |
| 942 |
) |
| 943 |
}) |
| 944 |
} |
| 945 |
|
| 946 |
pub fn derives(w: &mut dyn Write, derives: &[Derive], indent: usize) -> Result<()> { |
| 947 |
for derive in derives { |
| 948 |
let s = match &derive.cfg_condition { |
| 949 |
Some(condition) => format!( |
| 950 |
"#[cfg_attr({}, derive({}))]", |
| 951 |
condition, |
| 952 |
derive.names.join(", ") |
| 953 |
), |
| 954 |
None => format!("#[derive({})]", derive.names.join(", ")), |
| 955 |
}; |
| 956 |
writeln!(w, "{}{}", tabs(indent), s)?; |
| 957 |
} |
| 958 |
Ok(()) |
| 959 |
} |
| 960 |
|
| 961 |
pub fn doc_alias(w: &mut dyn Write, name: &str, comment_prefix: &str, indent: usize) -> Result<()> { |
| 962 |
writeln!( |
| 963 |
w, |
| 964 |
"{}{}#[doc(alias = \"{}\")]", |
| 965 |
tabs(indent), |
| 966 |
comment_prefix, |
| 967 |
name, |
| 968 |
) |
| 969 |
} |
| 970 |
|
| 971 |
pub fn external_doc_link( |
| 972 |
w: &mut dyn Write, |
| 973 |
external_url: Option<&str>, |
| 974 |
name: &str, |
| 975 |
visibility: &Visibility, |
| 976 |
indent: usize, |
| 977 |
) -> Result<()> { |
| 978 |
// Don't generate the external doc link on non-public types. |
| 979 |
if !visibility.is_public() { |
| 980 |
Ok(()) |
| 981 |
} else if let Some(external_url) = external_url { |
| 982 |
writeln!( |
| 983 |
w, |
| 984 |
"{}/// This documentation is incomplete due to license restrictions and limitations on docs.rs. Please have a look at [our official docs]({}/index.html?search={}) for more information.", |
| 985 |
tabs(indent), |
| 986 |
external_url.trim_end_matches('/'), |
| 987 |
name |
| 988 |
) |
| 989 |
} else { |
| 990 |
Ok(()) |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
pub fn doc_hidden( |
| 995 |
w: &mut dyn Write, |
| 996 |
doc_hidden: bool, |
| 997 |
comment_prefix: &str, |
| 998 |
indent: usize, |
| 999 |
) -> Result<()> { |
| 1000 |
if doc_hidden { |
| 1001 |
writeln!(w, "{}{}#[doc(hidden)]", tabs(indent), comment_prefix) |
| 1002 |
} else { |
| 1003 |
Ok(()) |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
pub fn allow_deprecated( |
| 1008 |
w: &mut dyn Write, |
| 1009 |
allow_deprecated: Option<Version>, |
| 1010 |
commented: bool, |
| 1011 |
indent: usize, |
| 1012 |
) -> Result<()> { |
| 1013 |
if allow_deprecated.is_some() { |
| 1014 |
writeln!( |
| 1015 |
w, |
| 1016 |
"{}{}#[allow(deprecated)]", |
| 1017 |
tabs(indent), |
| 1018 |
if commented { "//" } else { "" } |
| 1019 |
) |
| 1020 |
} else { |
| 1021 |
Ok(()) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
pub fn write_vec<T: Display>(w: &mut dyn Write, v: &[T]) -> Result<()> { |
| 1026 |
for s in v { |
| 1027 |
writeln!(w, "{s}")?; |
| 1028 |
} |
| 1029 |
Ok(()) |
| 1030 |
} |
| 1031 |
|
| 1032 |
pub fn declare_default_from_new( |
| 1033 |
w: &mut dyn Write, |
| 1034 |
env: &Env, |
| 1035 |
name: &str, |
| 1036 |
functions: &[analysis::functions::Info], |
| 1037 |
has_builder: bool, |
| 1038 |
) -> Result<()> { |
| 1039 |
if let Some(func) = functions.iter().find(|f| { |
| 1040 |
!f.hidden |
| 1041 |
&& f.status.need_generate() |
| 1042 |
&& f.name == "new" |
| 1043 |
// Cannot generate Default implementation for Option<> |
| 1044 |
&& f.ret.parameter.as_ref().is_some_and(|x| !*x.lib_par.nullable) |
| 1045 |
// Cannot generate Default implementation for Result<> |
| 1046 |
&& f.outs.mode != Mode::Throws(ThrowFunctionReturnStrategy::ReturnResult) |
| 1047 |
}) { |
| 1048 |
if func.parameters.rust_parameters.is_empty() { |
| 1049 |
writeln!(w)?; |
| 1050 |
version_condition(w, env, None, func.version, false, 0)?; |
| 1051 |
writeln!( |
| 1052 |
w, |
| 1053 |
"impl Default for {name} {{ |
| 1054 |
fn default() -> Self {{ |
| 1055 |
Self::new() |
| 1056 |
}} |
| 1057 |
}}" |
| 1058 |
)?; |
| 1059 |
} else if has_builder { |
| 1060 |
// create an alternative default implementation the uses `glib::object::Object::new()` |
| 1061 |
writeln!(w)?; |
| 1062 |
version_condition(w, env, None, func.version, false, 0)?; |
| 1063 |
writeln!( |
| 1064 |
w, |
| 1065 |
"impl Default for {name} {{ |
| 1066 |
fn default() -> Self {{ |
| 1067 |
glib::object::Object::new::<Self>() |
| 1068 |
}} |
| 1069 |
}}" |
| 1070 |
)?; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
Ok(()) |
| 1075 |
} |
| 1076 |
|
| 1077 |
/// Escapes string in format suitable for placing inside double quotes. |
| 1078 |
pub fn escape_string(s: &str) -> String { |
| 1079 |
let mut es = String::with_capacity(s.len() * 2); |
| 1080 |
for c in s.chars() { |
| 1081 |
match c { |
| 1082 |
'\"' | '\\' => es.push('\\'), |
| 1083 |
_ => (), |
| 1084 |
} |
| 1085 |
es.push(c); |
| 1086 |
} |
| 1087 |
es |
| 1088 |
} |
| 1089 |
|
| 1090 |
#[cfg(test)] |
| 1091 |
mod tests { |
| 1092 |
use super::*; |
| 1093 |
|
| 1094 |
#[test] |
| 1095 |
fn test_escape_string() { |
| 1096 |
assert_eq!(escape_string(""), ""); |
| 1097 |
assert_eq!(escape_string("no escaping here"), "no escaping here"); |
| 1098 |
assert_eq!(escape_string(r#"'"\"#), r#"'\"\\"#); |
| 1099 |
} |
| 1100 |
} |