diff --git a/README.md b/README.md index e2c219e..17f5ba9 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Rust's functionalities are carefully adapted to Dart's paradigms, focusing on a ## Highlights ### Libraries -| [Array] | [Cell] | [Error] | [Iter] | [Option] | [Panic] | [Result] | [Slice] | [Typedefs] | +| [Array] | [Cell] | [Iter] | [Option] | [Panic] | [Result] | [Slice] | [Typedefs] | 🔥 **Extensive Extensions:** Dozens of additional extensions with hundreds of methods tailored for Dart. These extensions are designed for maximum composability, addressing specific scenarios. @@ -34,7 +34,6 @@ it's also available for `T?`. [Cell]: https://github.com/mcmah309/rust_core/tree/master/lib/src/cell -[Error]: https://github.com/mcmah309/rust_core/tree/master/lib/src/error [Option]: https://github.com/mcmah309/rust_core/tree/master/lib/src/option [Panic]: https://github.com/mcmah309/rust_core/tree/master/lib/src/panic [Result]: https://github.com/mcmah309/rust_core/tree/master/lib/src/result diff --git a/lib/error.dart b/lib/error.dart deleted file mode 100644 index 414fad7..0000000 --- a/lib/error.dart +++ /dev/null @@ -1,3 +0,0 @@ -library error; - -export 'src/error/error.dart'; \ No newline at end of file diff --git a/lib/src/error/README.md b/lib/src/error/README.md deleted file mode 100644 index 3142953..0000000 --- a/lib/src/error/README.md +++ /dev/null @@ -1,50 +0,0 @@ -# Error - -Error contains abstractions for working with errors (aka exceptions). - -## ErrorKind - -`ErrorKind` is used to represent an error. This is usually used as the Error/Failure type of a `Result`. -It similar in functionality to a Rust enum with "thiserror" crate functionality, yet unfortunately not as expressive -due to Dart language constraints. - -### How To Use -To use `ErrorKind`, have an enum implement `ErrorEnum`. Each template {number} will be replaced by the corresponding -value at the index provided to `ErrorKind` if `toString()` is called. Don't worry this will never have a runtime error if you forget to provide a value. -```dart - enum IoError implements ErrorEnum { - diskRead("Could not read '{0}' from disk."), - diskWrite("Could not write '{0}' to '{1}' on disk."), - unknown; - - @override - final String? template; - - const IoError([this.template]); -} - - void main(){ - // can be any object, here a string is used. - final diskpath = "/home/user/file"; - final ioError = ErrorKind(IoError.diskRead, [diskpath]); - // toString() -> "IoError: Could not read '/home/user/file' from disk. - switch(ioError.type){ - case IoError.diskRead: - // code here - case IoError.diskWrite: - // code here - case IoError.unkown: - // code here - } - } -``` - -### Advantage vs A Rust Enum -`ErrorKind` does have some advantages over the Rust enum. It _can_ remain untyped (`ErrorKind`) and allow you to compose errors of -different types if you so desire. Although some may warn against doing so as you lose the explicit error type until you -check. - -### Alternatives -An alternative to using `ErrorKind` is hand rolling your own sealed classes. That is totally valid yet more verbose. - -You can also use the [anyhow](https://pub.dev/packages/anyhow) package. \ No newline at end of file diff --git a/lib/src/error/error.dart b/lib/src/error/error.dart deleted file mode 100644 index 3528300..0000000 --- a/lib/src/error/error.dart +++ /dev/null @@ -1,94 +0,0 @@ -/// An error enum with an optional template. e.g. "Could not write {0} to {1} on disk". -/// To be implemented by an [Enum] and used by [ErrorKind]. -abstract class ErrorEnum implements Enum { - final String? template; - - ErrorEnum([this.template]); -} - -/// An abstraction to write concise errors. e.g. -/// ```dart -/// enum IoError implements ErrorEnum { -/// diskRead("Could not read '{0}' from disk."), -/// diskWrite("Could not write '{0}' to '{1}' on disk."), -/// unknown; -/// -/// @override -/// final String? template; -/// -/// const IoError([this.template]); -///} -/// -/// void main(){ -/// final diskpath = "/home/user/file"; -/// final ioError = ErrorKind(IoError.diskRead, [diskpath]); -/// // toString() -> "IoError: Could not read '/home/user/file' from disk. -/// switch(ioError.type){ -/// case IoError.diskRead: -/// ... -/// } -/// } -/// ``` -class ErrorKind { - static final _innerNumberCapture = RegExp(r'{(\d+)}'); - - final E type; - final List? values; - - ErrorKind(this.type, [this.values]) - // : assert(() { - // final format = type.template; - // if(format != null){ - // Iterable matches = innerNumberCapture.allMatches(format); - - // for (var match in matches) { - // int index = int.parse(match.group(1)!); - // if(values == null){ - // throw "An $ErrorKind class with type ${type.runtimeType}, expects a value at index '$index' but values was null."; - // } - // if(values.length - 1 > index){ - // throw "An $ErrorKind class with type ${type.runtimeType}, expects a value at index '$index' but values only ${values.length} values were provided."; - // } - // } - // } - // return true; - // }()) - ; - - @override - String toString() { - final template = type.template; - if (values == null) { - if (template == null) { - return "${type.runtimeType}"; - } else { - return "${type.runtimeType}: $template"; - } - } - if (template == null) { - return "${type.runtimeType}"; - } - final replacedTemplate = template.replaceAllMapped(_innerNumberCapture, (Match match) { - int index = int.parse(match.group(1)!); - if (index < values!.length) { - return values![index].toString(); - } else { - // If index is out of bounds, keep the original placeholder - return match.group(0)!; - } - }).trim(); - if (replacedTemplate.isEmpty) { - return "${type.runtimeType}"; - } else { - return "${type.runtimeType}: $replacedTemplate"; - } - } - - @override - bool operator ==(Object other) { - return other is ErrorKind && other.type == type; - } - - @override - int get hashCode => type.hashCode; -} diff --git a/test/error/error_test.dart b/test/error/error_test.dart deleted file mode 100644 index 2e5ddea..0000000 --- a/test/error/error_test.dart +++ /dev/null @@ -1,209 +0,0 @@ -import 'package:rust_core/error.dart'; -import 'package:test/test.dart'; - -enum IoError implements ErrorEnum { - oneTemplateVariable("Could not read '{0}' from disk."), - twoTemplateVariables("Could not write '{0}' to '{1}' on disk."), - oneTemplateVariableTwoTimes("Make sure that {0} is correct {0}"), - noTemplateVariable("An unknown error occurred."), - empty, - twoTemplateVariablesOutOfOrder("Could not write '{1}' to '{0}' on disk."); - - @override - final String? template; - - const IoError([this.template]); -} - -void main() { - group("oneTemplateVariable", () { - test("one template variable with value", () { - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.oneTemplateVariable, [diskpath]); - switch (x.type) { - case IoError.oneTemplateVariable: - case IoError.twoTemplateVariables: - case IoError.oneTemplateVariableTwoTimes: - case IoError.noTemplateVariable: - case IoError.empty: - case IoError.twoTemplateVariablesOutOfOrder: - } - expect(x.toString(), "IoError: Could not read '/home/user/file' from disk."); - }); - - test("one template variable without value", () { - final x = ErrorKind(IoError.oneTemplateVariable, []); - expect(x.toString(), "IoError: Could not read '{0}' from disk."); - }); - - test("one template variable with too many values value", () { - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.oneTemplateVariable, [diskpath]); - expect(x.toString(), "IoError: Could not read '/home/user/file' from disk."); - }); - - test("no value", () { - final x = ErrorKind(IoError.oneTemplateVariable); - expect(x.toString(), "IoError: Could not read '{0}' from disk."); - }); - }); - - group("twoTemplateVariables", () { - test("two template variable with value", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.twoTemplateVariables, [value, diskpath]); - expect(x.toString(), "IoError: Could not write 'bing bong' to '/home/user/file' on disk."); - }); - - test("two template variable without value", () { - final x = ErrorKind(IoError.twoTemplateVariables, []); - expect(x.toString(), "IoError: Could not write '{0}' to '{1}' on disk."); - }); - - test("two template variable with one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.twoTemplateVariables, [value]); - expect(x.toString(), "IoError: Could not write 'bing bong' to '{1}' on disk."); - }); - - test("two template variable with too many values value", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final extraValue = "too much"; - final x = ErrorKind(IoError.twoTemplateVariables, [value, diskpath, extraValue]); - expect(x.toString(), "IoError: Could not write 'bing bong' to '/home/user/file' on disk."); - }); - - test("no value", () { - final x = ErrorKind(IoError.twoTemplateVariables); - expect(x.toString(), "IoError: Could not write '{0}' to '{1}' on disk."); - }); - }); - - group("oneTemplateVariableTwoTimes", () { - test("one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.oneTemplateVariableTwoTimes, [value]); - expect(x.toString(), "IoError: Make sure that bing bong is correct bing bong"); - }); - - test("two values", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.oneTemplateVariableTwoTimes, [value, diskpath]); - expect(x.toString(), "IoError: Make sure that bing bong is correct bing bong"); - }); - - test("no values", () { - final x = ErrorKind(IoError.oneTemplateVariableTwoTimes, []); - expect(x.toString(), "IoError: Make sure that {0} is correct {0}"); - }); - - test("too many values", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final extraValue = "too much"; - final x = ErrorKind(IoError.oneTemplateVariableTwoTimes, [value, diskpath, extraValue]); - expect(x.toString(), "IoError: Make sure that bing bong is correct bing bong"); - }); - - test("no value", () { - final x = ErrorKind(IoError.oneTemplateVariableTwoTimes); - expect(x.toString(), "IoError: Make sure that {0} is correct {0}"); - }); - }); - - group("noTemplateVariable", () { - test("one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.noTemplateVariable, [value]); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - - test("two values", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.noTemplateVariable, [value, diskpath]); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - - test("no values", () { - final x = ErrorKind(IoError.noTemplateVariable, []); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - }); - - group("noTemplateVariable", () { - test("one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.noTemplateVariable, [value]); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - - test("two values", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.noTemplateVariable, [value, diskpath]); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - - test("no values", () { - final x = ErrorKind(IoError.noTemplateVariable, []); - expect(x.toString(), "IoError: An unknown error occurred."); - }); - }); - - group("empty", () { - test("one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.empty, [value]); - expect(x.toString(), "IoError"); - }); - - test("two values", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.empty, [value, diskpath]); - expect(x.toString(), "IoError"); - }); - - test("no values", () { - final x = ErrorKind(IoError.empty, []); - expect(x.toString(), "IoError"); - }); - }); - - group("twoTemplateVariables", () { - test("two template variable with value", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final x = ErrorKind(IoError.twoTemplateVariablesOutOfOrder, [value, diskpath]); - expect(x.toString(), "IoError: Could not write '/home/user/file' to 'bing bong' on disk."); - }); - - test("two template variable without value", () { - final x = ErrorKind(IoError.twoTemplateVariablesOutOfOrder, []); - expect(x.toString(), "IoError: Could not write '{1}' to '{0}' on disk."); - }); - - test("two template variable with one value", () { - final value = "bing bong"; - final x = ErrorKind(IoError.twoTemplateVariablesOutOfOrder, [value]); - expect(x.toString(), "IoError: Could not write '{1}' to 'bing bong' on disk."); - }); - - test("two template variable with too many values value", () { - final value = "bing bong"; - final diskpath = "/home/user/file"; - final extraValue = "too much"; - final x = ErrorKind(IoError.twoTemplateVariablesOutOfOrder, [value, diskpath, extraValue]); - expect(x.toString(), "IoError: Could not write '/home/user/file' to 'bing bong' on disk."); - }); - - test("no value", () { - final x = ErrorKind(IoError.twoTemplateVariablesOutOfOrder); - expect(x.toString(), "IoError: Could not write '{1}' to '{0}' on disk."); - }); - }); -}