diff --git a/lib/moxlib.dart b/lib/moxlib.dart index 97206dd..05af3a1 100644 --- a/lib/moxlib.dart +++ b/lib/moxlib.dart @@ -1,4 +1,5 @@ library moxlib; -export 'awaitabledatasender.dart'; -export 'math.dart'; +export 'src/awaitabledatasender.dart'; +export 'src/math.dart'; +export 'src/result.dart'; diff --git a/lib/awaitabledatasender.dart b/lib/src/awaitabledatasender.dart similarity index 100% rename from lib/awaitabledatasender.dart rename to lib/src/awaitabledatasender.dart diff --git a/lib/math.dart b/lib/src/math.dart similarity index 100% rename from lib/math.dart rename to lib/src/math.dart diff --git a/lib/src/result.dart b/lib/src/result.dart new file mode 100644 index 0000000..d93f69f --- /dev/null +++ b/lib/src/result.dart @@ -0,0 +1,24 @@ +/// Holds a value of either [T] or [V]. +class Result { + /// Constructs a result. [_data] must be either of type [T] or [V]. + const Result(this._data) + : assert( + _data is T || _data is V, + 'Invalid data type $_data: Must be either $T or $V', + ); + final dynamic _data; + + /// Returns true if the data contained within is of type [S]. If not, returns false. + bool isType() => _data is S; + + /// Returns the data contained within cast to [S]. Before doing this call, it's recommended + /// to check isType() first. + S get() { + assert(_data is S, 'Data is not $S'); + + return _data as S; + } + + /// Returns the runtime type of the data. + Object get dataRuntimeType => _data.runtimeType; +} diff --git a/pubspec.yaml b/pubspec.yaml index 295685c..263b423 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: moxlib description: A collection of code for sharing between various moxxy libraries. Not intended for outside use. -version: 0.1.5 +version: 0.2.0 homepage: https://codeberg.org/moxxy/moxlib publish_to: https://git.polynom.me/api/packages/Moxxy/pub diff --git a/test/awaitabledatasender_test.dart b/test/awaitabledatasender_test.dart index c444063..8410822 100644 --- a/test/awaitabledatasender_test.dart +++ b/test/awaitabledatasender_test.dart @@ -1,5 +1,4 @@ -import 'package:moxlib/awaitabledatasender.dart'; - +import 'package:moxlib/moxlib.dart'; import 'package:test/test.dart'; class TestDataType implements JsonImplementation { diff --git a/test/math_test.dart b/test/math_test.dart index 40c5fb8..7b7a36c 100644 --- a/test/math_test.dart +++ b/test/math_test.dart @@ -1,5 +1,4 @@ -import 'package:moxlib/math.dart'; - +import 'package:moxlib/moxlib.dart'; import 'package:test/test.dart'; void main() {