Skip to content

Commit f3f9a01

Browse files
authored
Merge pull request #172 from tom-leys/fix/cargo-fmt
Cargo fmt
2 parents 0b9ab62 + 2e0365c commit f3f9a01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+958
-931
lines changed

bindings_generator/src/api.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::collections::{HashMap, HashSet};
21
use serde_json;
2+
use std::collections::{HashMap, HashSet};
33

44
pub struct Api {
55
pub classes: Vec<GodotClass>,
@@ -9,7 +9,8 @@ pub struct Api {
99
impl Api {
1010
pub fn new() -> Self {
1111
let mut api = Api {
12-
classes: serde_json::from_slice(get_api_json()).expect("Failed to parse the API description"),
12+
classes: serde_json::from_slice(get_api_json())
13+
.expect("Failed to parse the API description"),
1314
api_underscore: Default::default(),
1415
};
1516

@@ -74,9 +75,13 @@ pub struct GodotClass {
7475
}
7576

7677
impl GodotClass {
77-
pub fn is_refcounted(&self) -> bool { self.is_reference || &self.name == "Reference" }
78+
pub fn is_refcounted(&self) -> bool {
79+
self.is_reference || &self.name == "Reference"
80+
}
7881

79-
pub fn is_pointer_safe(&self) -> bool { self.is_refcounted() || self.singleton }
82+
pub fn is_pointer_safe(&self) -> bool {
83+
self.is_refcounted() || self.singleton
84+
}
8085
}
8186

8287
#[derive(Deserialize, Debug)]
@@ -207,9 +212,7 @@ impl Ty {
207212
let name = split.next().unwrap();
208213
Ty::Enum(format!("{}{}", class, name))
209214
}
210-
ty => {
211-
Ty::Object(ty.into())
212-
},
215+
ty => Ty::Object(ty.into()),
213216
}
214217
}
215218

@@ -286,6 +289,10 @@ impl Ty {
286289
}
287290
}
288291

289-
pub fn get_api_json() -> &'static [u8] { include_bytes!("../api.json") }
292+
pub fn get_api_json() -> &'static [u8] {
293+
include_bytes!("../api.json")
294+
}
290295

291-
pub fn get_namespaces_json() -> &'static [u8] { include_bytes!("../namespaces.json") }
296+
pub fn get_namespaces_json() -> &'static [u8] {
297+
include_bytes!("../namespaces.json")
298+
}

bindings_generator/src/classes.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use crate::api::*;
2-
use std::io::Write;
32
use crate::GeneratorResult;
43
use heck::CamelCase;
4+
use std::io::Write;
55

66
pub fn generate_class_struct(output: &mut impl Write, class: &GodotClass) -> GeneratorResult {
77
if !class.is_refcounted() {
88
writeln!(output, "#[derive(Copy, Clone)]")?;
99
}
1010

11-
writeln!(output,
12-
r#"#[allow(non_camel_case_types)]
11+
writeln!(
12+
output,
13+
r#"#[allow(non_camel_case_types)]
1314
#[derive(Debug)]
1415
pub struct {name} {{
1516
#[doc(hidden)]
@@ -28,14 +29,16 @@ pub fn generate_enum(output: &mut impl Write, class: &GodotClass, e: &Enum) -> G
2829
// it. For example ImageFormat::Rgb8 instead of ImageFormat::FormatRgb8.
2930

3031
let mut values: Vec<(&String, &u32)> = e.values.iter().collect();
31-
values.sort_by(|a, b|{ a.1.cmp(&b.1) });
32+
values.sort_by(|a, b| a.1.cmp(&b.1));
3233

33-
writeln!(output,
34-
r#"#[repr(u32)]
34+
writeln!(
35+
output,
36+
r#"#[repr(u32)]
3537
#[allow(non_camel_case_types)]
3638
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
3739
pub enum {class_name}{enum_name} {{"#,
38-
class_name = class.name, enum_name = e.name
40+
class_name = class.name,
41+
enum_name = e.name
3942
)?;
4043

4144
for &(key, val) in &values {
@@ -60,9 +63,14 @@ pub enum {class_name}{enum_name} {{"#,
6063

6164
fn try_remove_prefix(key: &str, prefix: &str) -> Option<String> {
6265
let key_lower = key.to_lowercase();
63-
if key_lower.starts_with(prefix) && !key.chars().nth(prefix.len()).map_or(true, |c| c.is_numeric()) {
66+
if key_lower.starts_with(prefix)
67+
&& !key
68+
.chars()
69+
.nth(prefix.len())
70+
.map_or(true, |c| c.is_numeric())
71+
{
6472
return Some(key[prefix.len()..].to_string());
6573
}
6674

6775
None
68-
}
76+
}

bindings_generator/src/dependency.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ pub fn strongly_connected_components(
1515
) -> HashSet<String> {
1616
let mut visited = visited.unwrap_or_default();
1717

18-
if let Some(class) = api.find_class(class) {
18+
if let Some(class) = api.find_class(class) {
1919
visit(api, class, &mut visited);
2020
}
2121

2222
visited
2323
}
2424

25-
fn visit(
26-
api: &Api,
27-
class: &GodotClass,
28-
visited: &mut HashSet<String>,
29-
) {
25+
fn visit(api: &Api, class: &GodotClass, visited: &mut HashSet<String>) {
3026
visited.insert(class.name.clone());
3127

3228
let mut to_visit = HashSet::new();
@@ -45,11 +41,7 @@ fn visit(
4541
}
4642
}
4743

48-
49-
fn base_classes(
50-
api: &Api,
51-
class: &GodotClass,
52-
) -> HashSet<String> {
44+
fn base_classes(api: &Api, class: &GodotClass) -> HashSet<String> {
5345
let mut bases = HashSet::new();
5446

5547
if class.base_class.is_empty() {
@@ -64,10 +56,7 @@ fn base_classes(
6456
}
6557
}
6658

67-
fn referenced_classes(
68-
api: &Api,
69-
class: &GodotClass
70-
) -> HashSet<String> {
59+
fn referenced_classes(api: &Api, class: &GodotClass) -> HashSet<String> {
7160
let mut classes = HashSet::new();
7261

7362
for method in &class.methods {
@@ -84,4 +73,4 @@ fn referenced_classes(
8473
}
8574

8675
classes
87-
}
76+
}

bindings_generator/src/documentation.rs

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::api::*;
2-
use std::io::Write;
32
use crate::GeneratorResult;
3+
use std::io::Write;
44

55
pub fn class_doc_link(class: &GodotClass) -> String {
66
// TODO: link the correct crate
@@ -15,51 +15,69 @@ pub fn official_doc_url(class: &GodotClass) -> String {
1515
)
1616
}
1717

18-
pub fn generate_class_documentation(output: &mut impl Write, api: &Api, class: &GodotClass) -> GeneratorResult {
19-
let has_parent = class.base_class != "";
20-
let singleton_str = if class.singleton { "singleton " } else { "" } ;
21-
let ownership_type = if class.is_refcounted() { "reference counted" } else { "unsafe" };
22-
if &class.name == "Reference" {
23-
writeln!(output, "/// Base class of all reference-counted types. Inherits `Object`.")?;
24-
} else if &class.name == "Object" {
25-
writeln!(output, "/// The base class of most Godot classes.")?;
26-
} else if has_parent {
27-
writeln!(output, r#"
18+
pub fn generate_class_documentation(
19+
output: &mut impl Write,
20+
api: &Api,
21+
class: &GodotClass,
22+
) -> GeneratorResult {
23+
let has_parent = class.base_class != "";
24+
let singleton_str = if class.singleton { "singleton " } else { "" };
25+
let ownership_type = if class.is_refcounted() {
26+
"reference counted"
27+
} else {
28+
"unsafe"
29+
};
30+
if &class.name == "Reference" {
31+
writeln!(
32+
output,
33+
"/// Base class of all reference-counted types. Inherits `Object`."
34+
)?;
35+
} else if &class.name == "Object" {
36+
writeln!(output, "/// The base class of most Godot classes.")?;
37+
} else if has_parent {
38+
writeln!(
39+
output,
40+
r#"
2841
/// `{api_type} {singleton}class {name}` inherits `{base_class}` ({ownership_type})."#,
29-
api_type = class.api_type,
30-
name = class.name,
31-
base_class = class.base_class,
32-
ownership_type = ownership_type,
33-
singleton = singleton_str
34-
)?;
35-
} else {
36-
writeln!(output, r#"
42+
api_type = class.api_type,
43+
name = class.name,
44+
base_class = class.base_class,
45+
ownership_type = ownership_type,
46+
singleton = singleton_str
47+
)?;
48+
} else {
49+
writeln!(
50+
output,
51+
r#"
3752
/// `{api_type} {singleton}class {name}` ({ownership_type})."#,
38-
api_type = class.api_type,
39-
name = class.name,
40-
ownership_type = ownership_type,
41-
singleton = singleton_str
42-
)?;
43-
}
53+
api_type = class.api_type,
54+
name = class.name,
55+
ownership_type = ownership_type,
56+
singleton = singleton_str
57+
)?;
58+
}
4459

45-
writeln!(output,
46-
r#"///
60+
writeln!(
61+
output,
62+
r#"///
4763
/// ## Official documentation
4864
///
4965
/// See the [documentation of this class]({url}) in the Godot engine's official documentation."#,
50-
url = official_doc_url(class),
51-
)?;
66+
url = official_doc_url(class),
67+
)?;
5268

53-
if class.is_refcounted() {
54-
writeln!(output,
55-
r#"///
69+
if class.is_refcounted() {
70+
writeln!(
71+
output,
72+
r#"///
5673
/// ## Memory management
5774
///
5875
/// The lifetime of this object is automatically managed through reference counting."#
59-
)?;
60-
} else if class.instanciable {
61-
writeln!(output,
62-
r#"///
76+
)?;
77+
} else if class.instanciable {
78+
writeln!(
79+
output,
80+
r#"///
6381
/// ## Memory management
6482
///
6583
/// Non reference counted objects such as the ones of this type are usually owned by the engine.
@@ -69,43 +87,37 @@ r#"///
6987
/// In the cases where Rust code owns an object of this type, for example if the object was just
7088
/// created on the Rust side and not passed to the engine yet, ownership should be either given
7189
/// to the engine or the object must be manually destroyed using `{name}::free`."#,
72-
name = class.name
73-
)?;
74-
}
90+
name = class.name
91+
)?;
92+
}
7593

76-
if class.base_class != "" {
77-
writeln!(output,
78-
r#"///
94+
if class.base_class != "" {
95+
writeln!(
96+
output,
97+
r#"///
7998
/// ## Class hierarchy
8099
///
81100
/// {name} inherits methods from:"#,
82-
name = class.name,
83-
)?;
101+
name = class.name,
102+
)?;
84103

85-
list_base_classes(
86-
output,
87-
api,
88-
&class.base_class,
89-
)?;
90-
}
104+
list_base_classes(output, api, &class.base_class)?;
105+
}
91106

92-
if class.api_type == "tools" {
93-
writeln!(output,
94-
r#"///
107+
if class.api_type == "tools" {
108+
writeln!(
109+
output,
110+
r#"///
95111
/// ## Tool
96112
///
97113
/// This class is used to interact with Godot's editor."#,
98-
)?;
99-
}
114+
)?;
115+
}
100116

101-
Ok(())
117+
Ok(())
102118
}
103119

104-
fn list_base_classes(
105-
output: &mut impl Write,
106-
api: &Api,
107-
parent_name: &str,
108-
) -> GeneratorResult {
120+
fn list_base_classes(output: &mut impl Write, api: &Api, parent_name: &str) -> GeneratorResult {
109121
if let Some(parent) = api.find_class(parent_name) {
110122
let class_link = class_doc_link(&parent);
111123

0 commit comments

Comments
 (0)