mallard 1.0.0 copy "mallard: ^1.0.0" to clipboard
mallard: ^1.0.0 copied to clipboard

Railway Oriented Programming for Dart. Functional Result and Task types for type-safe error handling.

example/mallard_example.dart

import 'dart:convert';
import 'dart:io';

import 'package:mallard/mallard.dart';

enum SettingsLoadException { notFound, invalidJson, permissionDenied }

class Settings {
  Settings.fromJson(Map<String, dynamic> json) : name = json['name'] as String;

  final String name;
}

Task<Settings, SettingsLoadException> loadSettings() =>
    Task.attempt(
          run: () => File('settings.json').readAsString(),
          handle: (exception) => exception is PathNotFoundException
              ? SettingsLoadException.notFound
              : SettingsLoadException.permissionDenied,
        )
        .thenAttempt(
          run: (jsonString) => jsonDecode(jsonString) as Map<String, dynamic>,
          handle: (_) => SettingsLoadException.invalidJson,
        )
        .ensure(
          check: (json) => json.containsKey('name') && json['name'] is String,
          otherwise: (_) => SettingsLoadException.invalidJson,
        )
        .convert(Settings.fromJson);

void main() async {
  Mallard.onTaskFailure = (failure, exception, stackTrace) =>
      print('[Mallard callback] Task failed with exception: $exception');

  final result = await loadSettings().run();

  final message = result.resolve(
    onSuccess: (settings) => 'Settings loaded: ${settings.name}',
    onFailure: (error) => switch (error) {
      .notFound => 'Settings file not found.',
      .invalidJson => 'Settings file is corrupted.',
      .permissionDenied => 'Check your app storage permissions.',
    },
  );

  print('\nmessage: $message');
}
0
likes
160
points
103
downloads

Publisher

verified publisherjakesmd.dev

Weekly Downloads

Railway Oriented Programming for Dart. Functional Result and Task types for type-safe error handling.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

equatable

More

Packages that depend on mallard