Skip to content

Commit 5cf5443

Browse files
committed
cargo fmt
1 parent 8a4dd31 commit 5cf5443

File tree

3 files changed

+94
-59
lines changed

3 files changed

+94
-59
lines changed

crates/csharp/src/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
10741074
}
10751075

10761076
let resource_type_name = match self.kind {
1077-
FunctionKind::Method(resource_type_id) |
1078-
FunctionKind::Static(resource_type_id) |
1077+
FunctionKind::Method(resource_type_id) |
1078+
FunctionKind::Static(resource_type_id) |
10791079
FunctionKind::Constructor(resource_type_id) => {
10801080
format!(
10811081
".{}",

crates/csharp/src/interface.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) struct InterfaceFragment {
2020
pub(crate) csharp_src: String,
2121
pub(crate) csharp_interop_src: String,
2222
pub(crate) stub: String,
23-
pub(crate) direction: Option<Direction>, // Types do not have a direction.
23+
pub(crate) direction: Option<Direction>, // Types do not have a direction.
2424
}
2525

2626
pub(crate) struct InterfaceTypeAndFragments {
@@ -159,7 +159,7 @@ impl InterfaceGenerator<'_> {
159159
let tmp_name: String;
160160
if self.is_world {
161161
if CSharp::type_is_bidirectional(self.resolve(), ty) {
162-
name = name.rsplitn(2, ".").nth(1).unwrap();
162+
name = name.rsplitn(2, ".").nth(1).unwrap();
163163
}
164164
// World level types, except resources are always imports.
165165
else if name.ends_with("Exports") && type_def.kind != TypeDefKind::Resource {
@@ -175,7 +175,8 @@ impl InterfaceGenerator<'_> {
175175

176176
fn replace_last(haystack: &str, needle: &str, replacement: &str) -> String {
177177
if let Some(pos) = haystack.rfind(needle) {
178-
let mut result = String::with_capacity(haystack.len() - needle.len() + replacement.len());
178+
let mut result =
179+
String::with_capacity(haystack.len() - needle.len() + replacement.len());
179180
result.push_str(&haystack[..pos]);
180181
result.push_str(replacement);
181182
result.push_str(&haystack[pos + needle.len()..]);
@@ -810,11 +811,10 @@ impl InterfaceGenerator<'_> {
810811
let (_namespace, interface_name) =
811812
&CSharp::get_class_name_from_qualified_name(self.name);
812813
let impl_name = if resource_type_name.is_some() {
813-
resource_type_name.unwrap()
814-
}
815-
else {
816-
format!("{}Impl", interface_name.strip_prefix("I").unwrap())
817-
};
814+
resource_type_name.unwrap()
815+
} else {
816+
format!("{}Impl", interface_name.strip_prefix("I").unwrap())
817+
};
818818

819819
uwriteln!(
820820
self.csharp_interop_src,
@@ -1135,7 +1135,6 @@ impl InterfaceGenerator<'_> {
11351135
}}
11361136
"#
11371137
);
1138-
11391138
}
11401139
Direction::Export => {
11411140
let prefix = key
@@ -1238,7 +1237,7 @@ impl InterfaceGenerator<'_> {
12381237
}
12391238
}
12401239
};
1241-
1240+
12421241
uwrite!(
12431242
self.csharp_interop_src,
12441243
r#"
@@ -1540,7 +1539,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> {
15401539
.join(", ");
15411540

15421541
let access = self.csharp_gen.access_modifier();
1543-
1542+
15441543
// Write the top level bidirectional types to csharp_gen. Could also create a class to gather the interface level bidrectional types.
15451544
match &self.resolve.types[ty].owner {
15461545
TypeOwner::World(_id) => {
@@ -1551,7 +1550,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> {
15511550
}}
15521551
"
15531552
));
1554-
},
1553+
}
15551554
_ => {
15561555
uwrite!(
15571556
self.src,

crates/csharp/src/world_generator.rs

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ use std::collections::{HashMap, HashSet};
88
use std::fmt::Write;
99
use std::ops::Deref;
1010
use std::{iter, mem};
11-
use wit_bindgen_core::{Direction, Files, InterfaceGenerator as _, WorldGenerator, uwrite, uwriteln};
11+
use wit_bindgen_core::{
12+
uwrite, uwriteln, Direction, Files, InterfaceGenerator as _, WorldGenerator,
13+
};
1214
use wit_component::WitPrinter;
1315
use wit_parser::abi::WasmType;
1416
use wit_parser::{
15-
Function, InterfaceId, Resolve, SizeAlign, Type, TypeDefKind, TypeId, TypeOwner, WorldId, WorldItem, WorldKey
17+
Function, InterfaceId, Resolve, SizeAlign, Type, TypeDefKind, TypeId, TypeOwner, WorldId,
18+
WorldItem, WorldKey,
1619
};
1720

1821
/// CSharp is the world generator for wit files. It coordinates all the generated code.
@@ -97,7 +100,7 @@ impl CSharp {
97100
}
98101

99102
// We can share some types to save some code size and allow a more intuitive user experience.
100-
pub (crate) fn type_is_bidirectional(resolve: &Resolve, ty: &TypeId) -> bool {
103+
pub(crate) fn type_is_bidirectional(resolve: &Resolve, ty: &TypeId) -> bool {
101104
let type_def = &resolve.types[*ty];
102105
let kind = &type_def.kind;
103106

@@ -108,8 +111,19 @@ impl CSharp {
108111
};
109112
}
110113

111-
fn write_world_fragments(&mut self, direction: Direction, direction_name: &str, src: &mut String, access: &str, name: &String) {
112-
if self.world_fragments.iter().any(|f| f.direction == Some(direction)) {
114+
fn write_world_fragments(
115+
&mut self,
116+
direction: Direction,
117+
direction_name: &str,
118+
src: &mut String,
119+
access: &str,
120+
name: &String,
121+
) {
122+
if self
123+
.world_fragments
124+
.iter()
125+
.any(|f| f.direction == Some(direction))
126+
{
113127
uwrite!(
114128
src,
115129
"
@@ -281,26 +295,25 @@ impl WorldGenerator for CSharp {
281295
let world = &resolve.worlds[world_id];
282296
let mut types = Vec::new();
283297
for (name, export) in world.exports.iter() {
284-
match name {
285-
WorldKey::Name(name) => {
286-
match export {
287-
WorldItem::Type(id) => {
288-
if !CSharp::type_is_bidirectional(resolve, &id) {
289-
types.push((name.as_str(), *id))
290-
}
291-
},
292-
_ => {},
298+
match name {
299+
WorldKey::Name(name) => match export {
300+
WorldItem::Type(id) => {
301+
if !CSharp::type_is_bidirectional(resolve, &id) {
302+
types.push((name.as_str(), *id))
293303
}
294-
},
295-
WorldKey::Interface(_) => {},
304+
}
305+
_ => {}
306+
},
307+
WorldKey::Interface(_) => {}
296308
}
297309
}
298310

299311
if !types.is_empty() {
300312
export_types(&mut gen, &types);
301313
}
302314

303-
for (resource, funcs) in by_resource(funcs.iter().copied(),
315+
for (resource, funcs) in by_resource(
316+
funcs.iter().copied(),
304317
gen.csharp_gen.world_resources.keys().copied(),
305318
) {
306319
if let Some(resource) = resource {
@@ -384,16 +397,23 @@ impl WorldGenerator for CSharp {
384397
);
385398

386399
src.push_str(
387-
&self
388-
.world_fragments
389-
.iter()
390-
.filter(|f| f.direction.is_none())
391-
.map(|f| f.csharp_src.deref())
392-
.collect::<Vec<_>>()
393-
.join("\n"),
394-
);
400+
&self
401+
.world_fragments
402+
.iter()
403+
.filter(|f| f.direction.is_none())
404+
.map(|f| f.csharp_src.deref())
405+
.collect::<Vec<_>>()
406+
.join("\n"),
407+
);
395408

396-
src.push_str(self.bidirectional_types_src.iter().cloned().collect::<Vec<_>>().join("\n").as_str());
409+
src.push_str(
410+
self.bidirectional_types_src
411+
.iter()
412+
.cloned()
413+
.collect::<Vec<_>>()
414+
.join("\n")
415+
.as_str(),
416+
);
397417
self.write_world_fragments(Direction::Import, "Imports", &mut src, access, &name);
398418
self.write_world_fragments(Direction::Export, "Exports", &mut src, access, &name);
399419

@@ -568,15 +588,25 @@ impl WorldGenerator for CSharp {
568588
if !&self.world_fragments.is_empty() {
569589
src.push_str("\n");
570590

571-
if self.world_fragments.iter().any(|f| f.direction == Some(Direction::Import)) {
591+
if self
592+
.world_fragments
593+
.iter()
594+
.any(|f| f.direction == Some(Direction::Import))
595+
{
572596
src.push_str("\n");
573597

574598
src.push_str("namespace Imports {\n");
575599

576-
src.push_str(&format!("{access} partial class {name}WorldImportsInterop : I{name}WorldImports\n"));
600+
src.push_str(&format!(
601+
"{access} partial class {name}WorldImportsInterop : I{name}WorldImports\n"
602+
));
577603
src.push_str("{");
578604

579-
for fragment in self.world_fragments.iter().filter(|f| f.direction == Some(Direction::Import)) {
605+
for fragment in self
606+
.world_fragments
607+
.iter()
608+
.filter(|f| f.direction == Some(Direction::Import))
609+
{
580610
if !fragment.csharp_interop_src.is_empty() {
581611
src.push_str("\n");
582612

@@ -587,21 +617,28 @@ impl WorldGenerator for CSharp {
587617
src.push_str("}\n");
588618
}
589619

590-
if self.world_fragments.iter().any(|f| f.direction == Some(Direction::Export)) {
620+
if self
621+
.world_fragments
622+
.iter()
623+
.any(|f| f.direction == Some(Direction::Export))
624+
{
591625
src.push_str("namespace Exports {\n");
592626

593627
src.push_str(&format!("{access} static class {name}WorldInterop\n"));
594628
src.push_str("{");
595629

596-
for fragment in self.world_fragments.iter().filter(|f| f.direction == Some(Direction::Export)) {
630+
for fragment in self
631+
.world_fragments
632+
.iter()
633+
.filter(|f| f.direction == Some(Direction::Export))
634+
{
597635
src.push_str("\n");
598636

599637
src.push_str(&fragment.csharp_interop_src);
600638
}
601639
src.push_str("}\n");
602640
src.push_str("}\n");
603641
}
604-
605642
}
606643

607644
if self.needs_async_support {
@@ -847,18 +884,14 @@ impl WorldGenerator for CSharp {
847884

848885
Ok(())
849886
}
850-
851887
}
852888

853889
enum Stubs<'a> {
854890
World(&'a Vec<InterfaceFragment>),
855891
Interface(&'a Vec<InterfaceFragment>),
856892
}
857893

858-
fn export_types(
859-
gen: &mut InterfaceGenerator,
860-
types: &[(&str, TypeId)],
861-
) {
894+
fn export_types(gen: &mut InterfaceGenerator, types: &[(&str, TypeId)]) {
862895
for (ty_name, ty) in types {
863896
gen.define_type(ty_name, *ty);
864897
}
@@ -932,18 +965,21 @@ fn interface_name(
932965
}
933966
};
934967

935-
let name = format!("{}{}", match name {
936-
WorldKey::Name(name) => name.to_upper_camel_case(),
937-
WorldKey::Interface(id) => resolve.interfaces[*id]
938-
.name
939-
.as_ref()
940-
.unwrap()
941-
.to_upper_camel_case(),
968+
let name = format!(
969+
"{}{}",
970+
match name {
971+
WorldKey::Name(name) => name.to_upper_camel_case(),
972+
WorldKey::Interface(id) => resolve.interfaces[*id]
973+
.name
974+
.as_ref()
975+
.unwrap()
976+
.to_upper_camel_case(),
942977
},
943978
match direction {
944979
Direction::Import => "Imports",
945980
Direction::Export => "Exports",
946-
});
981+
}
982+
);
947983

948984
let namespace = match &pkg {
949985
Some(name) => {

0 commit comments

Comments
 (0)