rust_like_result 1.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 63

Result type like Rust std::result.

Created from templates made available by Stagehand under a BSD-style license.

Usage #

See Example for usage sample.

1.0.0 #

  • Initial version, created by Stagehand

1.0.1 #

  • Fix homepage

1.0.2 #

  • Add example

1.0.3 #

  • Change example

1.1.0 #

  • tryAsResultSync, tryAsResultAsync, testResult - functions added

1.1.1 #

  • example improved

1.2.0 #

  • pipe operator for Result

example/example.dart

import 'dart:io';

import 'package:rust_like_result/rust_like_result.dart';

void main() async {
  
  // Ok:
  Result<Map> res = Ok({'Forward': 'yes', 'Back': 'no'});
  print(res); // Ok({Forward: yes, Back: no})
  print(res is Ok ? 'Ok' : 'Err'); // Ok
  print(res.unwrap); // {Forward: yes, Back: no}
  print(res.unwrapOrDefault({})); // {Forward: yes, Back: no}
  print(res.unwrapOrElse( (e) => {'error':e} )); // {Forward: yes, Back: no}
  

  // Err:
  res = Err(ArgumentError('Bad name'));
  print(res); // Err(Invalid argument(s): Bad name)
  print(res is Ok ? 'Ok' : 'Err'); // Err
  // print(res.unwrap); // raises exception 'Invalid argument(s): Bad name'
  print(res.unwrapOrDefault({})); // {}
  print(res.unwrapOrElse( (e) => {'Got error':e} )); // {Got error: Invalid argument(s): Bad name}


  // tryAsResult...() - calls function and return result as Ok, or Err if exception was raised:
  Result ok = await tryAsResultAsync( () => File('notExistsFile').rename('newPath')); 
  print(ok); // Err(Bad state: FileSystemException: Cannot rename ...
  
  ok = await tryAsResultAsync( () => Directory('.').list()); 
  print(ok); // Ok(Instance of '_ControllerStream<FileSystemEntity>')
  
  ok = tryAsResultSync(() => File('notExistsFile').renameSync('newPath'));
  print(ok); // Err(Bad state: FileSystemException ...
  
  ok = tryAsResultSync(() => Directory('.').listSync());
  print(ok); // Ok([File:...

  // Pipe operator stays Err as is, but calls right-side function on val of the left-side Ok.
  var r = Err<int>(ArgumentError('Lalala')) | (x)=>x*2;
  print(r); // Err(Invalid argument(s): Lalala)

  r = Ok(123) | (x)=>x*2;
  print(r); // Ok(246)


}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  rust_like_result: ^1.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:rust_like_result/rust_like_result.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
26
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Health suggestions

Format lib/src/result_base.dart.

Run dartfmt to format lib/src/result_base.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0