oc-wasm-opencomputers 0.10.2

High-level APIs for OpenComputers components
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
//! Provides high-level access to the robot component APIs.

use crate::common::Rgb;
use crate::error::Error;
use core::convert::TryFrom;
use core::fmt::{Display, Formatter};
use core::num::NonZeroU32;
use core::str::FromStr;
use minicbor::Decode;
use oc_wasm_futures::invoke::{component_method, Buffer};
use oc_wasm_helpers::{
	error::NullAndStringOr,
	sides::{Relative as RelativeSide, Side},
	Lockable,
};
use oc_wasm_safe::{
	component::{Invoker, MethodCallError},
	Address,
};

/// The type name for robot components.
pub const TYPE: &str = "robot";

/// An error returned when converting a [`RelativeSide`](RelativeSide) into an
/// [`ActionSide`](ActionSide) if the value does not map.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TryFromRelativeSideError(());

impl Display for TryFromRelativeSideError {
	fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
		"invalid value".fmt(f)
	}
}

#[cfg(feature = "std")]
impl std::error::Error for TryFromRelativeSideError {}

/// The directions a robot can move.
///
/// A robot cannot strafe to the left or right. In order to move in such directions, it must first
/// turn to face towards or away from that direction instead.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum MoveDirection {
	Down,
	Up,
	Back,
	Front,
}

impl From<MoveDirection> for u8 {
	fn from(x: MoveDirection) -> Self {
		match x {
			MoveDirection::Down => 0,
			MoveDirection::Up => 1,
			MoveDirection::Back => 2,
			MoveDirection::Front => 3,
		}
	}
}

impl From<MoveDirection> for usize {
	fn from(x: MoveDirection) -> Self {
		u8::from(x) as usize
	}
}

impl From<MoveDirection> for RelativeSide {
	fn from(x: MoveDirection) -> Self {
		match x {
			MoveDirection::Down => Self::Bottom,
			MoveDirection::Up => Self::Top,
			MoveDirection::Back => Self::Back,
			MoveDirection::Front => Self::Front,
		}
	}
}

impl TryFrom<u8> for MoveDirection {
	type Error = oc_wasm_helpers::error::TryFromInt;

	fn try_from(x: u8) -> Result<Self, Self::Error> {
		match x {
			0 => Ok(Self::Down),
			1 => Ok(Self::Up),
			2 => Ok(Self::Back),
			3 => Ok(Self::Front),
			_ => Err(oc_wasm_helpers::error::TryFromInt),
		}
	}
}

impl TryFrom<RelativeSide> for MoveDirection {
	type Error = TryFromRelativeSideError;

	fn try_from(x: RelativeSide) -> Result<Self, Self::Error> {
		match x {
			RelativeSide::Bottom => Ok(Self::Down),
			RelativeSide::Top => Ok(Self::Up),
			RelativeSide::Back => Ok(Self::Back),
			RelativeSide::Front => Ok(Self::Front),
			_ => Err(TryFromRelativeSideError(())),
		}
	}
}

impl From<ActionSide> for MoveDirection {
	fn from(x: ActionSide) -> Self {
		match x {
			ActionSide::Bottom => Self::Down,
			ActionSide::Top => Self::Up,
			ActionSide::Front => Self::Front,
		}
	}
}

impl Side for MoveDirection {}

/// The sides on which a robot can manipulate blocks.
///
/// A robot cannot manipulate blocks to its left or right sides or behind it. In order to
/// manipulate such blocks, it must first turn to face that direction instead.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ActionSide {
	Bottom,
	Top,
	Front,
}

impl From<ActionSide> for u8 {
	fn from(x: ActionSide) -> Self {
		match x {
			ActionSide::Bottom => 0,
			ActionSide::Top => 1,
			ActionSide::Front => 3,
		}
	}
}

impl From<ActionSide> for usize {
	fn from(x: ActionSide) -> Self {
		u8::from(x) as usize
	}
}

impl From<ActionSide> for RelativeSide {
	fn from(x: ActionSide) -> Self {
		match x {
			ActionSide::Bottom => Self::Bottom,
			ActionSide::Top => Self::Top,
			ActionSide::Front => Self::Front,
		}
	}
}

impl TryFrom<u8> for ActionSide {
	type Error = oc_wasm_helpers::error::TryFromInt;

	fn try_from(x: u8) -> Result<Self, Self::Error> {
		match x {
			0 => Ok(Self::Bottom),
			1 => Ok(Self::Top),
			3 => Ok(Self::Front),
			_ => Err(oc_wasm_helpers::error::TryFromInt),
		}
	}
}

impl TryFrom<RelativeSide> for ActionSide {
	type Error = TryFromRelativeSideError;

	fn try_from(x: RelativeSide) -> Result<Self, Self::Error> {
		match x {
			RelativeSide::Bottom => Ok(Self::Bottom),
			RelativeSide::Top => Ok(Self::Top),
			RelativeSide::Front => Ok(Self::Front),
			_ => Err(TryFromRelativeSideError(())),
		}
	}
}

impl Side for ActionSide {}

/// The directions in which a robot can rotate.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Rotation {
	Clockwise,
	Counterclockwise,
}

/// The things that can be hit when swinging a tool.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ToolHit {
	/// The tool hit an entity, which was attacked.
	Entity,

	/// The tool hit a block, which was broken.
	Block,

	/// The tool hit a fire, which was extinguished.
	Fire,

	/// The tool hit air, which did nothing.
	Air,
}

/// The possible results of successfully right-clicking on a location.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ActivateResult {
	/// A usable block, such as a lever or button, was activated.
	BlockActivated,

	/// The equipped inventory item, such as a block of dirt, a torch, a spawn egg, or a fire
	/// charge, was placed into the world.
	ItemPlaced,

	/// The equipped inventory item, such as a water bucket or an empty bucket when facing a fluid,
	/// was used in a way other than placing a block into the world.
	ItemUsed,

	/// The equipped inventory item, such as shears, was used on an adjacent entity.
	ItemInteracted,
}

/// The things that can exist in the space of a block.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum BlockContent {
	/// There is a creature (player, friendly, neutral, or hostile) or minecart in the space.
	///
	/// Despite the name, note that certain other entities, such as drones, do not return this
	/// value. The name is based on the name used within OpenComputers itself.
	Entity,

	/// There is nothing in the space.
	Air,

	/// There is a liquid in the space.
	Liquid,

	/// There is a block in the space that the robot can move into, which would be destroyed if it
	/// did so.
	///
	/// An example of this kind of content is tall grass.
	Replaceable,

	/// There is a block in the space that a player could walk through, but the robot cannot.
	///
	/// An example of this kind of content is a flower.
	Passable,

	/// There is a normal block in the space.
	Solid,
}

impl BlockContent {
	/// Returns a string describing the content.
	#[must_use = "This function is only useful for its return value"]
	pub fn as_str(&self) -> &'static str {
		match self {
			Self::Entity => "entity",
			Self::Air => "air",
			Self::Liquid => "liquid",
			Self::Replaceable => "replaceable",
			Self::Passable => "passable",
			Self::Solid => "solid",
		}
	}
}

impl Display for BlockContent {
	fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
		self.as_str().fmt(f)
	}
}

impl FromStr for BlockContent {
	type Err = Error;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s {
			"entity" => Ok(Self::Entity),
			"air" => Ok(Self::Air),
			"liquid" => Ok(Self::Liquid),
			"replaceable" => Ok(Self::Replaceable),
			"passable" => Ok(Self::Passable),
			"solid" => Ok(Self::Solid),
			_ => Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown)),
		}
	}
}

impl TryFrom<&str> for BlockContent {
	type Error = <Self as FromStr>::Err;

	fn try_from(s: &str) -> Result<Self, Self::Error> {
		Self::from_str(s)
	}
}

/// A robot component.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Robot(Address);

impl Robot {
	/// Creates a wrapper around a robot.
	///
	/// The `address` parameter is the address of the robot. It is not checked for correctness at
	/// this time because network topology could change after this function returns; as such, each
	/// usage of the value may fail instead.
	#[must_use = "This function is only useful for its return value"]
	pub fn new(address: Address) -> Self {
		Self(address)
	}

	/// Returns the address of the robot.
	#[must_use = "This function is only useful for its return value"]
	pub fn address(&self) -> &Address {
		&self.0
	}
}

impl<'invoker, 'buffer, B: 'buffer + Buffer> Lockable<'invoker, 'buffer, B> for Robot {
	type Locked = Locked<'invoker, 'buffer, B>;

	fn lock(&self, invoker: &'invoker mut Invoker, buffer: &'buffer mut B) -> Self::Locked {
		Locked {
			address: self.0,
			invoker,
			buffer,
		}
	}
}

/// A robot component on which methods can be invoked.
///
/// This type combines a robot address, an [`Invoker`](Invoker) that can be used to make method
/// calls, and a scratch buffer used to perform CBOR encoding and decoding. A value of this type
/// can be created by calling [`Robot::lock`](Robot::lock), and it can be dropped to return the
/// borrow of the invoker and buffer to the caller so they can be reused for other purposes.
///
/// The `'invoker` lifetime is the lifetime of the invoker. The `'buffer` lifetime is the lifetime
/// of the buffer. The `B` type is the type of scratch buffer to use.
pub struct Locked<'invoker, 'buffer, B: Buffer> {
	/// The component address.
	address: Address,

	/// The invoker.
	invoker: &'invoker mut Invoker,

	/// The buffer.
	buffer: &'buffer mut B,
}

impl<'invoker, 'buffer, B: Buffer> Locked<'invoker, 'buffer, B> {
	/// Returns the colour of the robot’s side body light.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn get_light_colour(&mut self) -> Result<Rgb, Error> {
		let ret: (u32,) = component_method::<(), _, _>(
			self.invoker,
			self.buffer,
			&self.address,
			"getLightColor",
			None,
		)
		.await?;
		Ok(Rgb(ret.0))
	}

	/// Sets the colour of the robot’s side body light.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn set_light_colour(&mut self, colour: Rgb) -> Result<(), Error> {
		component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"setLightColor",
			Some(&(colour.0,)),
		)
		.await?;
		Ok(())
	}

	/// Returns the durability of the equipped tool, or `None` if no tool is equipped or the
	/// equipped tool does not have a concept of durability.
	///
	/// The durability value, if available, is a number between 0 and 1.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn durability(&mut self) -> Result<Option<f64>, Error> {
		let ret: NullAndStringOr<'_, (f64,)> = component_method::<(), _, _>(
			self.invoker,
			self.buffer,
			&self.address,
			"durability",
			None,
		)
		.await?;
		Ok(match ret {
			NullAndStringOr::Ok(v) => Some(v.0),
			NullAndStringOr::Err(_) => None,
		})
	}

	/// Moves the robot.
	///
	/// The `direction` parameter indicates in which direction to try to move.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`Blocked`](Error::Blocked)
	/// * [`ImpossibleMove`](Error::ImpossibleMove)
	/// * [`NotEnoughEnergy`](Error::NotEnoughEnergy)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn move_robot(&mut self, direction: MoveDirection) -> Result<(), Error> {
		let ret: NullAndStringOr<'_, (bool,)> = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"move",
			Some(&(u8::from(direction),)),
		)
		.await?;
		match ret {
			NullAndStringOr::Ok(_) => Ok(()),
			NullAndStringOr::Err("not enough energy") => Err(Error::NotEnoughEnergy),
			NullAndStringOr::Err("impossible move") => Err(Error::ImpossibleMove),
			NullAndStringOr::Err(s) => Err(Error::Blocked(s.parse()?)),
		}
	}

	/// Turns the robot.
	///
	/// The `direction` parameter indicates in which direction to turn.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`NotEnoughEnergy`](Error::NotEnoughEnergy)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn turn(&mut self, direction: Rotation) -> Result<(), Error> {
		let ret: NullAndStringOr<'_, (bool,)> = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"turn",
			Some(&(direction == Rotation::Clockwise,)),
		)
		.await?;
		match ret {
			NullAndStringOr::Ok(_) => Ok(()),
			NullAndStringOr::Err("not enough energy") => Err(Error::NotEnoughEnergy),
			NullAndStringOr::Err(_) => {
				Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
			}
		}
	}

	/// Returns the robot’s name.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn name(self) -> Result<&'buffer str, Error> {
		Ok(component_method::<(), (&'buffer str,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"name",
			None,
		)
		.await?
		.0)
	}

	/// Left-clicks the currently equipped tool on an adjacent block or space.
	///
	/// The `side` parameter indicates where to swing the tool, relative the robot’s current facing
	/// direction. The `face` parameter indicates which face of the target location to aim at. The
	/// `sneak` parameter indicates whether or not to sneak while operating the tool.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadItem`](Error::BadItem) is returned if the block in the target location is too hard
	///   for the equipped tool to break, or the tool is of the wrong kind (for example, a shovel
	///   swinging at cobblestone).
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn swing(
		&mut self,
		side: ActionSide,
		face: Option<RelativeSide>,
		sneak: bool,
	) -> Result<ToolHit, Error> {
		let ret: (bool, &str) = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"swing",
			Some(&(u8::from(side), face.map(u8::from), sneak)),
		)
		.await?;
		if ret.1 == "entity" {
			Ok(ToolHit::Entity)
		} else if ret.1 == "block" {
			if ret.0 {
				Ok(ToolHit::Block)
			} else {
				Err(Error::BadItem)
			}
		} else if ret.1 == "fire" {
			Ok(ToolHit::Fire)
		} else if ret.1 == "air" {
			Ok(ToolHit::Air)
		} else {
			// The OpenComputers robot component’s swing method does not return any other values.
			// Therefore, if we see another value, we must be addressing a different component that
			// also has a method named swing.
			Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
		}
	}

	/// Right-clicks the currently equipped item on an adjacent block or space.
	///
	/// The `side` parameter indicates where to use the item, relative the robot’s current facing
	/// direction. The `face` parameter indicates which face of the target location to aim at. The
	/// `sneak` parameter indicates whether or not to sneak while operating the tool. The
	/// `duration` parameter indicates how long to hold down the right mouse button.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	/// * [`Failed`](Error::Failed) is returned if the item is not usable (for example, if it is a
	///   piece of coal), is usable on certain entities but such entities are not present (for
	///   example, if it is a shears and there is nothing in front of the robot or the entity in
	///   front of the robot is a zombie), or is a placeable block but there is no adjacent block
	///   on which to mount it and the robot is not equipped with an angel upgrade.
	pub async fn use_item(
		&mut self,
		side: ActionSide,
		face: Option<RelativeSide>,
		sneak: bool,
		duration: f64,
	) -> Result<ActivateResult, Error> {
		let ret: (bool, Option<&str>) = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"use",
			Some(&(u8::from(side), face.map(u8::from), sneak, duration)),
		)
		.await?;
		if ret.1 == Some("block_activated") {
			Ok(ActivateResult::BlockActivated)
		} else if ret.1 == Some("item_placed") {
			Ok(ActivateResult::ItemPlaced)
		} else if ret.1 == Some("item_used") {
			Ok(ActivateResult::ItemUsed)
		} else if ret.1 == Some("item_interacted") {
			Ok(ActivateResult::ItemInteracted)
		} else if ret.0 {
			// The OpenComputers robot component’s swing method does not return any other success
			// reasons. Therefore, if we see another value, we must be addressing a different
			// component that also has a method named use.
			Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
		} else {
			Err(Error::Failed)
		}
	}

	/// Places one of the currently selected item into the world as a block.
	///
	/// The `side` parameter indicates in which location, relative to the robot’s current facing
	/// direction, the item should be placed. The `face` parameter indicates on which face of the
	/// target location the item should be placed (for example, for a lever or torch, on which
	/// adjacent surface the item should be mounted), again relative to the robot’s current facing
	/// direction; or is set to `None` to try all possible faces. The `sneak` parameter indicates
	/// whether or not to sneak while placing the item.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadItem`](Error::BadItem) is returned if there is no item selected.
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	/// * [`Failed`](Error::Failed) is returned if the selected item does not have a placeable
	///   block form (e.g. it is a tool instead); the target location is obstructed by an existing
	///   block; there is no existing block adjacent to the target placement location and the robot
	///   is not equipped with an angel upgrade; or `face` is provided, there is no existing block
	///   adjacent to the target placement location on the specified side, and the robot is not
	///   equipped with an angel upgrade; `face` is provided and points back towards the robot; or
	///   there is not enough energy.
	pub async fn place(
		&mut self,
		side: ActionSide,
		face: Option<RelativeSide>,
		sneak: bool,
	) -> Result<(), Error> {
		let ret: NullAndStringOr<'_, (bool,)> = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"place",
			Some(&(u8::from(side), face.map(u8::from), sneak)),
		)
		.await?;
		match ret {
			NullAndStringOr::Ok((true,)) => Ok(()),
			NullAndStringOr::Ok((false,)) => Err(Error::Failed),
			NullAndStringOr::Err("nothing selected") => Err(Error::BadItem),
			NullAndStringOr::Err(_) => {
				Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
			}
		}
	}

	/// Checks what’s present in a specified direction.
	///
	/// The `side` parameter indicates which space, relative to the robot’s current facing
	/// direction, to scan.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn detect(&mut self, side: ActionSide) -> Result<BlockContent, Error> {
		let ret: (bool, &str) = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"detect",
			Some(&(u8::from(side),)),
		)
		.await?;
		ret.1.parse()
	}

	/// Returns the size of the robot’s inventory, in slots.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn inventory_size(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"inventorySize",
			None,
		)
		.await?
		.0)
	}

	/// Returns the currently selected inventory slot number.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`NoInventory`](Error::NoInventory)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn selected(&mut self) -> Result<NonZeroU32, Error> {
		let ret: (u32,) =
			component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "select", None)
				.await?;
		match NonZeroU32::new(ret.0) {
			Some(n) => Ok(n),
			None => Err(Error::NoInventory),
		}
	}

	/// Selects an inventory slot.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn select(&mut self, slot: NonZeroU32) -> Result<(), Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"select",
			Some(&(slot,)),
		)
		.await;
		match ret {
			Ok(_) => Ok(()),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of items in an inventory slot.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn count(&mut self, slot: NonZeroU32) -> Result<u32, Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"count",
			Some(&(slot,)),
		)
		.await;
		match ret {
			Ok((n,)) => Ok(n),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of items in the currently selected inventory slot.
	///
	/// If the robot does not have an inventory, this function returns 0.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn count_selected(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"count",
			None,
		)
		.await?
		.0)
	}

	/// Returns the number of items that can be added to an inventory slot.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn space(&mut self, slot: NonZeroU32) -> Result<u32, Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"space",
			Some(&(slot,)),
		)
		.await;
		match ret {
			Ok((n,)) => Ok(n),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of items that can be added to the currently selected inventory slot.
	///
	/// If the robot does not have an inventory, this function returns 64.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn space_selected(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"space",
			None,
		)
		.await?
		.0)
	}

	/// Returns whether or not the currently selected inventory slot contains the same item as the
	/// specified inventory slot.
	///
	/// The `nbt` parameter indicates whether to consider NBT data during the comparison. Two empty
	/// slots are considered equal to each other but not to any nonempty slot. Between two nonempty
	/// slots, the sizes of the stacks do not matter; a slot with one torch and a slot with two
	/// torches are considered equal.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn compare_to(&mut self, other_slot: NonZeroU32, nbt: bool) -> Result<bool, Error> {
		let ret = component_method::<_, (bool,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"compareTo",
			Some(&(other_slot, nbt)),
		)
		.await;
		match ret {
			Ok((f,)) => Ok(f),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Moves items from the currently selected inventory slot to the specified inventory slot.
	///
	/// The `amount` parameter indicates the maximum number of items to transfer. If the target
	/// slot is empty, then the lesser of `amount` and the number of items available is moved. If
	/// the selected and target slots contain the same type of item, then the lesser of `amount`,
	/// the number of items available, and the remaining space in the target is moved. If the
	/// target slot contains a different type of item and `amount` is greater than or equal to the
	/// number of items in the selected slot, then the two stacks are swapped.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	/// * [`Failed`](Error::Failed) is returned if `amount` was nonzero but no items could be moved
	///   (because the source stack is empty, the target stack is of the same type and full, or the
	///   target stack is of a different type and `amount` is less than the size of the source
	///   stack and therefore the stacks cannot be swapped).
	pub async fn transfer_to(&mut self, target_slot: NonZeroU32, amount: u32) -> Result<(), Error> {
		let ret = component_method::<_, (bool,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"transferTo",
			Some(&(target_slot, amount)),
		)
		.await;
		match ret {
			Ok((true,)) => Ok(()),
			Ok((false,)) => Err(Error::Failed),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Compares a block in the world with the selected inventory slot.
	///
	/// The `fuzzy` parameter, when `true`, indicates that the comparison should ignore item
	/// subtypes (for example, dirt and coarse dirt are considered equal under fuzzy comparison
	/// rules). An empty itemstack is considered unequal to any block, and an air block is
	/// considered unequal to any itemstack.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn compare(&mut self, side: ActionSide, fuzzy: bool) -> Result<bool, Error> {
		Ok(component_method::<_, (bool,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"compare",
			Some(&(u8::from(side), fuzzy)),
		)
		.await?
		.0)
	}

	/// Drops items from the robot’s selected slot into the world as an itemstack entity or into an
	/// adjacent inventory.
	///
	/// If a block with an inventory is present on the specified side, up to `count` items from the
	/// currently selected slot are inserted into the inventory following the usual item insertion
	/// rules. If there are fewer than `count` items available or the inventory does not have space
	/// to hold `count` items, then fewer items are moved.
	///
	/// If no inventory is present on the specified side, up to `count` items are dropped into the
	/// world as an itemstack entity. If there are fewer than `count` items available, then a
	/// smaller stack is dropped.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`InventoryFull`](Error::InventoryFull)
	/// * [`NoItem`](Error::NoItem)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn drop(&mut self, side: ActionSide, count: u32) -> Result<(), Error> {
		let ret = component_method::<_, (bool, Option<&str>), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"drop",
			Some(&(u8::from(side), count)),
		)
		.await;
		match ret {
			Ok((true, _)) => Ok(()),
			Ok((false, None)) => Err(Error::NoItem),
			Ok((false, Some("inventory full"))) => Err(Error::InventoryFull),
			Ok((false, Some(_))) => Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown)),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Sucks up items from an itemstack entity or an adjacent inventory block.
	///
	/// If a block with an inventory is present on the specified side, up to `count` items from a
	/// single stack in that inventory are inserted into the robot’s inventory.
	///
	/// If no inventory is present on the specified side, one itemstack entity (of any size,
	/// ignoring `count`) is picked up from the world.
	///
	/// The sucked items are placed into the robot’s inventory, initially into the currently
	/// selected slot, then into slots after it, then wrapping around to slots before it, as
	/// necessary to hold all the sucked items. If there is not enough space to hold the items,
	/// then the items that cannot be held are left behind in their original location.
	///
	/// If all the robot’s inventory slots contain items, preventing a new type of item from being
	/// added, external inventory slots or itemstack entities are selected to match an item type
	/// already present in the robot. Otherwise, the first populated inventory slot, or an
	/// arbitrary itemstack entity, is selected.
	///
	/// On success, the number of items actually moved is returned, which may be less than `count`
	/// if the source stack does not have that many items or if that many items do not fit into the
	/// robot’s inventory, or may be more than `count` if the source is an itemstack entity because
	/// `count` is ignored.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	/// * [`Failed`](Error::Failed) is returned if there are no items in the specified direction,
	///   there is no space to move the items into (due to either number or type), or the adjacent
	///   inventory does not allow items to be removed.
	pub async fn suck(&mut self, side: ActionSide, count: u32) -> Result<u32, Error> {
		enum BoolOrU32 {
			Bool(bool),
			U32(u32),
		}
		impl<Context> Decode<'_, Context> for BoolOrU32 {
			fn decode(
				d: &mut minicbor::Decoder<'_>,
				_: &mut Context,
			) -> Result<Self, minicbor::decode::Error> {
				if d.datatype()? == minicbor::data::Type::Bool {
					Ok(Self::Bool(d.bool()?))
				} else {
					Ok(Self::U32(d.u32()?))
				}
			}
		}
		let ret: (BoolOrU32,) = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			"suck",
			Some(&(u8::from(side), count)),
		)
		.await?;
		match ret.0 {
			BoolOrU32::Bool(false) => Err(Error::Failed),
			BoolOrU32::Bool(true) => {
				// The OpenComputers robot component’s suck method never returns true. Therefore,
				// if we see true, we must be addressing a different component that also has a
				// method named suck.
				Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
			}
			BoolOrU32::U32(count) => Ok(count),
		}
	}

	/// Returns the number of internal fluid tanks.
	///
	/// This is typically equal to the number of tank upgrades (not tank controller upgrades)
	/// installed in the robot.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn tank_count(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"tankCount",
			None,
		)
		.await?
		.0)
	}

	/// Returns the currently selected internal fluid tank number.
	///
	/// If the robot has no tanks at all, this function returns 1.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn selected_tank(&mut self) -> Result<NonZeroU32, Error> {
		Ok(component_method::<(), (NonZeroU32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"selectTank",
			None,
		)
		.await?
		.0)
	}

	/// Selects an internal fluid tank.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn select_tank(&mut self, tank: NonZeroU32) -> Result<(), Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"selectTank",
			Some(&(tank,)),
		)
		.await;
		match ret {
			Ok(_) => Ok(()),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of millibuckets of fluid in an internal fluid tank.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn tank_level(&mut self, tank: NonZeroU32) -> Result<u32, Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"tankLevel",
			Some(&(tank,)),
		)
		.await;
		match ret {
			Ok((n,)) => Ok(n),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of millibuckets of fluid in the currently selected internal fluid tank.
	///
	/// If the robot does not have any tanks, this function returns 0.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn tank_level_selected(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"tankLevel",
			None,
		)
		.await?
		.0)
	}

	/// Returns the number of millibuckets of fluid that can be added to an internal fluid tank.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn tank_space(&mut self, tank: NonZeroU32) -> Result<u32, Error> {
		let ret = component_method::<_, (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"tankSpace",
			Some(&(tank,)),
		)
		.await;
		match ret {
			Ok((n,)) => Ok(n),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Returns the number of millibuckets of fluid that can be added to the currently selected
	/// internal fluid tank.
	///
	/// If the robot does not have any tanks, this function returns 0.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn tank_space_selected(&mut self) -> Result<u32, Error> {
		Ok(component_method::<(), (u32,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"tankSpace",
			None,
		)
		.await?
		.0)
	}

	/// Returns whether or not the currently selected internal fluid tank contains the same type of
	/// fluid as the specified internal fluid tank.
	///
	/// Two empty tanks are considered equal. An empty tank is not equal to any nonempty tank.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn compare_fluid_to(&mut self, other_tank: NonZeroU32) -> Result<bool, Error> {
		let ret = component_method::<_, (bool,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"compareFluidTo",
			Some(&(other_tank,)),
		)
		.await;
		match ret {
			Ok((f,)) => Ok(f),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(MethodCallError::TooManyDescriptors) => Err(Error::TooManyDescriptors),
			Err(e) => Err(Error::BadComponent(e.into())),
		}
	}

	/// Moves fluid from the currently selected internal fluid tank to the specified internal fluid
	/// tank.
	///
	/// The `amount` parameter indicates the maximum number of millibuckets to transfer. If the
	/// target tank is able to hold any fluid of the type held in the source tank, then the minimum
	/// of `amount`, the amount of fluid in the source tank, and the amount of space in the target
	/// tank is moved. If the target tank is not able to hold any of the fluid type held in the
	/// source tank (either because it is full or because the fluids are of different types), and
	/// if `amount` is greater than or equal to the amount of fluid in the source tank, then the
	/// two tanks’ contents are swapped. If the source tank is empty, the entire destination tank
	/// is moved to the source tank (i.e. the tanks’s contents are swapped).
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot)
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	/// * [`Failed`](Error::Failed) is returned if the requested tank number is greater than the
	///   tank count, or `amount` was nonzero but no fluid could be moved (because the source tank
	///   is empty, the target tank is of the same type and full, or the target tank is of a
	///   different type and `amount` is less than the size of the source tank and therefore the
	///   fluids cannot be swapped).
	pub async fn transfer_fluid_to(
		&mut self,
		target_tank: NonZeroU32,
		amount: u32,
	) -> Result<(), Error> {
		let ret = component_method::<_, NullAndStringOr<'_, bool>, _>(
			self.invoker,
			self.buffer,
			&self.address,
			"transferFluidTo",
			Some(&(target_tank, amount)),
		)
		.await;
		match ret {
			Ok(NullAndStringOr::Ok(true)) => Ok(()),
			Ok(NullAndStringOr::Err("incompatible or no fluid")) => Err(Error::Failed),
			Ok(NullAndStringOr::Err("invalid index")) | Err(MethodCallError::BadParameters(_)) => {
				Err(Error::BadInventorySlot)
			}
			Ok(NullAndStringOr::Ok(false) | NullAndStringOr::Err(_)) => {
				// The OpenComputers robot component’s transferFluidTo method never returns false.
				// Therefore, if we see true, we must be addressing a different component that also
				// has a method named transferFluidTo. Also do that for any other null-and-string
				// returns we don’t recognize.
				Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
			}
			Err(e) => Err(e.into()),
		}
	}

	/// Returns whether or not the currently selected internal fluid tank contains the same type of
	/// fluid as a tank in an external block.
	///
	/// The `tank` parameter selects the tank within the external block. An empty tank (or
	/// nonexistent tank, in the case of a robot without any internal tanks) is considered unequal
	/// to everything, including another empty tank.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadInventorySlot`](Error::BadInventorySlot) is returned if there is no fluid-containing
	///   block on the specified side or if the `tank` parameter is greater than the number of
	///   tanks in the block and the selected internal tank is nonempty, but only if the selected
	///   tank is non-empty.
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn compare_fluid(
		&mut self,
		side: ActionSide,
		tank: NonZeroU32,
	) -> Result<bool, Error> {
		let ret = component_method::<_, (bool,), _>(
			self.invoker,
			self.buffer,
			&self.address,
			"compareFluid",
			Some(&(u8::from(side), tank)),
		)
		.await;
		match ret {
			Ok((b,)) => Ok(b),
			Err(MethodCallError::BadParameters(_)) => Err(Error::BadInventorySlot),
			Err(e) => Err(e.into()),
		}
	}

	/// Moves fluid from an external block’s fluid tank to the currently selected internal fluid
	/// tank.
	///
	/// On success, the amount of fluid moved is the minimum of `amount`, the amount of fluid in
	/// the source tank, and the amount of space in the target tank, and this number is returned.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadItem`](Error::BadItem) is returned if the destination already contains an
	///   incompatible fluid.
	/// * [`InventoryFull`](Error::InventoryFull)
	/// * [`NoInventory`](Error::NoInventory) is returned if the robot does not contain any tanks.
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn drain(&mut self, side: ActionSide, amount: u32) -> Result<u32, Error> {
		self.drain_or_fill(side, amount, "drain").await
	}

	/// Moves fluid from the currently selected internal fluid tank to an external block.
	///
	/// On success, the amount of fluid moved is the minimum of `amount`, the amount of fluid in
	/// the source tank, and the amount of space in the target tank, and this number is returned.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadItem`](Error::BadItem) is returned if the destination already contains an
	///   incompatible fluid.
	/// * [`InventoryFull`](Error::InventoryFull)
	/// * [`NoInventory`](Error::NoInventory) is returned if the robot does not contain any tanks.
	/// * [`NoItem`](Error::NoItem) is returned if the source tank is empty and `amount` is
	///   nonzero.
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	pub async fn fill(&mut self, side: ActionSide, amount: u32) -> Result<u32, Error> {
		self.drain_or_fill(side, amount, "fill").await
	}

	/// Implements the [`drain`](drain) or [`fill`](fill) function.
	///
	/// # Errors
	/// * [`BadComponent`](Error::BadComponent)
	/// * [`BadItem`](Error::BadItem) is returned if the destination already contains an
	///   incompatible fluid.
	/// * [`InventoryFull`](Error::InventoryFull)
	/// * [`NoInventory`](Error::NoInventory) is returned if the robot does not contain any tanks.
	/// * [`NoItem`](Error::NoItem) is returned if the source tank is empty and `amount` is
	///   nonzero.
	/// * [`TooManyDescriptors`](Error::TooManyDescriptors)
	async fn drain_or_fill(
		&mut self,
		side: ActionSide,
		amount: u32,
		method: &str,
	) -> Result<u32, Error> {
		let ret: NullAndStringOr<'_, (bool, u32)> = component_method(
			self.invoker,
			self.buffer,
			&self.address,
			method,
			Some(&(u8::from(side), amount)),
		)
		.await?;
		match ret {
			NullAndStringOr::Ok((_, n)) => Ok(n),
			NullAndStringOr::Err("incompatible or no fluid") => Err(Error::BadItem),
			NullAndStringOr::Err("no space" | "tank is full") => Err(Error::InventoryFull),
			NullAndStringOr::Err("no tank selected") => Err(Error::NoInventory),
			NullAndStringOr::Err("tank is empty") => Err(Error::NoItem),
			NullAndStringOr::Err(_) => {
				Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
			}
		}
	}
}