diff --git a/packages/sane/analysis_options.yaml b/packages/sane/analysis_options.yaml index dee8927..139ca46 100644 --- a/packages/sane/analysis_options.yaml +++ b/packages/sane/analysis_options.yaml @@ -1,30 +1,33 @@ -# This file configures the static analysis results for your project (errors, -# warnings, and lints). -# -# This enables the 'recommended' set of lints from `package:lints`. -# This set helps identify many issues that may lead to problems when running -# or consuming Dart code, and enforces writing Dart using a single, idiomatic -# style and format. -# -# If you want a smaller set of lints you can change this to specify -# 'package:lints/core.yaml'. These are just the most critical lints -# (the recommended set includes the core lints). -# The core lints are also what is used by pub.dev for scoring packages. +include: package:flutter_lints/flutter.yaml -include: package:lints/recommended.yaml +analyzer: + errors: + avoid_print: ignore -# Uncomment the following section to specify additional rules. - -# linter: -# rules: -# - camel_case_types - -# analyzer: -# exclude: -# - path/to/excluded/files/** - -# For more information about the core and recommended set of lints, see -# https://dart.dev/go/core-lints - -# For additional information about configuring this file, see -# https://dart.dev/guides/language/analysis-options +linter: + rules: + prefer_single_quotes: true + require_trailing_commas: true + always_declare_return_types: true + avoid_catches_without_on_clauses: true + avoid_equals_and_hash_code_on_mutable_classes: true + avoid_types_on_closure_parameters: true + cancel_subscriptions: true + directives_ordering: true + eol_at_end_of_file: true + omit_local_variable_types: true + prefer_asserts_in_initializer_lists: true + prefer_const_constructors: true + prefer_final_in_for_each: true + prefer_final_locals: true + prefer_null_aware_method_calls: true + prefer_null_aware_operators: true + sort_constructors_first: true + sort_unnamed_constructors_first: true + sort_pub_dependencies: true + type_annotate_public_apis: true + unawaited_futures: true + unnecessary_lambdas: true + unnecessary_parenthesis: true + use_named_constants: true + use_super_parameters: true diff --git a/packages/sane/example/main.dart b/packages/sane/example/main.dart index d96ed0d..4e92837 100644 --- a/packages/sane/example/main.dart +++ b/packages/sane/example/main.dart @@ -1,3 +1,5 @@ +// ignore_for_file: avoid_print + import 'dart:io'; import 'dart:typed_data'; @@ -10,7 +12,7 @@ void main(List args) async { await sane.init(); final devices = await sane.getDevices(localOnly: true); - for (var device in devices) { + for (final device in devices) { print('Device found: ${device.name}'); } @@ -18,7 +20,7 @@ void main(List args) async { final optionDescriptors = await sane.getAllOptionDescriptors(handle); - for (var optionDescriptor in optionDescriptors) { + for (final optionDescriptor in optionDescriptors) { if (optionDescriptor.name == 'mode') { await sane.controlStringOption( handle: handle, @@ -50,10 +52,10 @@ void main(List args) async { sane.kill(); Uint8List mergeUint8Lists(List lists) { - int totalLength = lists.fold(0, (length, list) => length + list.length); - Uint8List result = Uint8List(totalLength); - int offset = 0; - for (var list in lists) { + final totalLength = lists.fold(0, (length, list) => length + list.length); + final result = Uint8List(totalLength); + var offset = 0; + for (final list in lists) { result.setRange(offset, offset + list.length, list); offset += list.length; } @@ -63,7 +65,7 @@ void main(List args) async { final file = File('./output.ppm'); file.writeAsStringSync( - "P6\n${parameters.pixelsPerLine} ${parameters.lines}\n255\n", + 'P6\n${parameters.pixelsPerLine} ${parameters.lines}\n255\n', mode: FileMode.write, ); final rawPixelData = mergeUint8Lists(rawPixelDataList); diff --git a/packages/sane/lib/src/dylib.dart b/packages/sane/lib/src/dylib.dart index a4738be..0b75836 100644 --- a/packages/sane/lib/src/dylib.dart +++ b/packages/sane/lib/src/dylib.dart @@ -4,5 +4,5 @@ import 'package:sane/src/bindings.g.dart'; LibSane? _dylib; LibSane get dylib { - return _dylib ??= LibSane(ffi.DynamicLibrary.open("libsane.so")); + return _dylib ??= LibSane(ffi.DynamicLibrary.open('libsane.so')); } diff --git a/packages/sane/lib/src/exceptions.dart b/packages/sane/lib/src/exceptions.dart index e6a2730..2863818 100644 --- a/packages/sane/lib/src/exceptions.dart +++ b/packages/sane/lib/src/exceptions.dart @@ -18,28 +18,23 @@ import 'package:sane/src/dylib.dart'; /// - [SaneNoMemoryException] /// - sealed class SaneException implements Exception { - SANE_Status get _status; - - const SaneException._(); - factory SaneException(SANE_Status status) { final exception = switch (status) { - SANE_Status.STATUS_GOOD => - throw ArgumentError( - 'Cannot create SaneException with status STATUS_GOOD', - 'status', - ), - SANE_Status.STATUS_UNSUPPORTED => SaneUnsupportedException(), - SANE_Status.STATUS_CANCELLED => SaneCancelledException(), - SANE_Status.STATUS_DEVICE_BUSY => SaneDeviceBusyException(), - SANE_Status.STATUS_INVAL => SaneInvalidDataException(), - SANE_Status.STATUS_EOF => SaneEofException(), - SANE_Status.STATUS_JAMMED => SaneJammedException(), - SANE_Status.STATUS_NO_DOCS => SaneNoDocumentsException(), - SANE_Status.STATUS_COVER_OPEN => SaneCoverOpenException(), - SANE_Status.STATUS_IO_ERROR => SaneIoException(), - SANE_Status.STATUS_NO_MEM => SaneNoMemoryException(), - SANE_Status.STATUS_ACCESS_DENIED => SaneAccessDeniedException(), + SANE_Status.STATUS_GOOD => throw ArgumentError( + 'Cannot create SaneException with status STATUS_GOOD', + 'status', + ), + SANE_Status.STATUS_UNSUPPORTED => const SaneUnsupportedException(), + SANE_Status.STATUS_CANCELLED => const SaneCancelledException(), + SANE_Status.STATUS_DEVICE_BUSY => const SaneDeviceBusyException(), + SANE_Status.STATUS_INVAL => const SaneInvalidDataException(), + SANE_Status.STATUS_EOF => const SaneEofException(), + SANE_Status.STATUS_JAMMED => const SaneJammedException(), + SANE_Status.STATUS_NO_DOCS => const SaneNoDocumentsException(), + SANE_Status.STATUS_COVER_OPEN => const SaneCoverOpenException(), + SANE_Status.STATUS_IO_ERROR => const SaneIoException(), + SANE_Status.STATUS_NO_MEM => const SaneNoMemoryException(), + SANE_Status.STATUS_ACCESS_DENIED => const SaneAccessDeniedException(), }; assert(exception._status == status); @@ -47,6 +42,9 @@ sealed class SaneException implements Exception { return exception; } + const SaneException._(); + SANE_Status get _status; + String get message { return dylib.sane_strstatus(_status).cast().toDartString(); } diff --git a/packages/sane/lib/src/sane.dart b/packages/sane/lib/src/sane.dart index 0b219f9..c951b80 100644 --- a/packages/sane/lib/src/sane.dart +++ b/packages/sane/lib/src/sane.dart @@ -22,7 +22,7 @@ class Sane { }) { final completer = Completer(); - authCallbackAdapter( + void authCallbackAdapter( SANE_String_Const resource, ffi.Pointer username, ffi.Pointer password, @@ -79,7 +79,7 @@ class Sane { Future> getDevices({ required bool localOnly, - }) async { + }) { final completer = Completer>(); Future(() { @@ -107,7 +107,7 @@ class Sane { return completer.future; } - Future open(String deviceName) async { + Future open(String deviceName) { final completer = Completer(); Future(() { @@ -132,7 +132,7 @@ class Sane { return completer.future; } - Future openDevice(SaneDevice device) async { + Future openDevice(SaneDevice device) { return open(device.name); } @@ -231,7 +231,7 @@ class Sane { valuePointer = ffi.nullptr; case SaneOptionValueType.group: - throw SaneInvalidDataException(); + throw const SaneInvalidDataException(); } if (action == SaneAction.setValue) { @@ -267,7 +267,7 @@ class Sane { invalid: default: - throw SaneInvalidDataException(); + throw const SaneInvalidDataException(); } } @@ -287,7 +287,8 @@ class Sane { switch (optionType) { case SaneOptionValueType.bool: result = dartBoolFromSaneBool( - (valuePointer as ffi.Pointer).value); + (valuePointer as ffi.Pointer).value, + ); case SaneOptionValueType.int: result = (valuePointer as ffi.Pointer).value; @@ -306,16 +307,18 @@ class Sane { result = null; default: - throw SaneInvalidDataException(); + throw const SaneInvalidDataException(); } ffi.calloc.free(valuePointer); ffi.calloc.free(infoPointer); - completer.complete(SaneOptionResult( - result: result, - infos: infos, - )); + completer.complete( + SaneOptionResult( + result: result, + infos: infos, + ), + ); }); return completer.future; @@ -326,8 +329,8 @@ class Sane { required int index, required SaneAction action, bool? value, - }) async { - return await _controlOption( + }) { + return _controlOption( handle: handle, index: index, action: action, @@ -340,8 +343,8 @@ class Sane { required int index, required SaneAction action, int? value, - }) async { - return await _controlOption( + }) { + return _controlOption( handle: handle, index: index, action: action, @@ -354,8 +357,8 @@ class Sane { required int index, required SaneAction action, double? value, - }) async { - return await _controlOption( + }) { + return _controlOption( handle: handle, index: index, action: action, @@ -368,8 +371,8 @@ class Sane { required int index, required SaneAction action, String? value, - }) async { - return await _controlOption( + }) { + return _controlOption( handle: handle, index: index, action: action, @@ -380,8 +383,8 @@ class Sane { Future> controlButtonOption({ required SaneHandle handle, required int index, - }) async { - return await _controlOption( + }) { + return _controlOption( handle: handle, index: index, action: SaneAction.setValue, @@ -395,7 +398,9 @@ class Sane { Future(() { final nativeParametersPointer = ffi.calloc(); final status = dylib.sane_get_parameters( - _getNativeHandle(handle), nativeParametersPointer); + _getNativeHandle(handle), + nativeParametersPointer, + ); print('sane_get_parameters() -> ${status.name}'); status.check(); diff --git a/packages/sane/lib/src/sane_isolate.dart b/packages/sane/lib/src/sane_isolate.dart index e3398c2..74d148c 100644 --- a/packages/sane/lib/src/sane_isolate.dart +++ b/packages/sane/lib/src/sane_isolate.dart @@ -16,12 +16,12 @@ class SaneIsolate implements Sane { Future spawn() async { final receivePort = ReceivePort(); _isolate = await Isolate.spawn( - _isolateEntryPoint, - _IsolateEntryPointArgs( - mainSendPort: receivePort.sendPort, - sane: _sane, - )); - print('Spawn SaneIsolate'); + _isolateEntryPoint, + _IsolateEntryPointArgs( + mainSendPort: receivePort.sendPort, + sane: _sane, + ), + ); _sendPort = await receivePort.first as SendPort; } @@ -311,7 +311,8 @@ class _SaneMessageHandler { final Sane _sane; Future<_SaneIsolateResponse> handleMessage( - _SaneIsolateMessage message) async { + _SaneIsolateMessage message, + ) async { switch (message) { case _SaneInitMessage _: return await _handleSaneInitMessage(); @@ -319,49 +320,49 @@ class _SaneMessageHandler { case _SaneExitMessage _: return await _handleSaneExitMessage(); - case _SaneGetDevicesMessage message: + case final _SaneGetDevicesMessage message: return await _handleSaneGetDevicesMessages(message); - case _SaneOpenMessage message: + case final _SaneOpenMessage message: return await _handleSaneOpenMessage(message); - case _SaneCloseMessage message: + case final _SaneCloseMessage message: return await _handleSaneCloseMessage(message); - case _SaneGetOptionDescriptorMessage message: + case final _SaneGetOptionDescriptorMessage message: return await _handleSaneGetOptionDescriptorMessage(message); - case _SaneGetAllOptionDescriptorsMessage message: + case final _SaneGetAllOptionDescriptorsMessage message: return await _handleSaneGetAllOptionDescriptorsMessage(message); - case _SaneControlOptionMessage message: + case final _SaneControlOptionMessage message: return await _handleSaneControlBoolOptionMessage(message); - case _SaneControlOptionMessage message: + case final _SaneControlOptionMessage message: return await _handleSaneControlIntOptionMessage(message); - case _SaneControlOptionMessage message: + case final _SaneControlOptionMessage message: return await _handleSaneControlFixedOptionMessage(message); - case _SaneControlOptionMessage message: + case final _SaneControlOptionMessage message: return await _handleSaneControlStringOptionMessage(message); - case _SaneControlButtonOptionMessage message: + case final _SaneControlButtonOptionMessage message: return await _handleSaneControlButtonOptionMessage(message); - case _SaneGetParametersMessage message: + case final _SaneGetParametersMessage message: return await _handleSaneGetParametersMessage(message); - case _SaneStartMessage message: + case final _SaneStartMessage message: return await _handleSaneStartMessage(message); - case _SaneReadMessage message: + case final _SaneReadMessage message: return await _handleSaneReadMessage(message); - case _SaneCancelMessage message: + case final _SaneCancelMessage message: return await _handleSaneCancelMessage(message); - case _SaneSetIOModeMessage message: + case final _SaneSetIOModeMessage message: return await _handleSaneSetIOModeMessage(message); default: diff --git a/packages/sane/lib/src/structures.dart b/packages/sane/lib/src/structures.dart index c0e3f0a..e199a96 100644 --- a/packages/sane/lib/src/structures.dart +++ b/packages/sane/lib/src/structures.dart @@ -1,3 +1,5 @@ +import 'package:meta/meta.dart'; + class SaneCredentials { SaneCredentials({ required this.username, @@ -22,12 +24,14 @@ class SaneDevice { final String type; } +@immutable class SaneHandle { - SaneHandle({required this.deviceName}); + const SaneHandle({required this.deviceName}); final String deviceName; @override - operator ==(other) => other is SaneHandle && other.deviceName == deviceName; + bool operator ==(Object other) => + other is SaneHandle && other.deviceName == deviceName; @override int get hashCode => deviceName.hashCode; diff --git a/packages/sane/pubspec.lock b/packages/sane/pubspec.lock index 789c73f..b2a2893 100644 --- a/packages/sane/pubspec.lock +++ b/packages/sane/pubspec.lock @@ -110,6 +110,14 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.1" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c" + url: "https://pub.dev" + source: hosted + version: "4.0.0" frontend_server_client: dependency: transitive description: @@ -159,7 +167,7 @@ packages: source: hosted version: "0.7.1" lints: - dependency: "direct dev" + dependency: transitive description: name: lints sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235" diff --git a/packages/sane/pubspec.yaml b/packages/sane/pubspec.yaml index 418b99f..a76c63c 100644 --- a/packages/sane/pubspec.yaml +++ b/packages/sane/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: dev_dependencies: ffigen: ^14.0.1 - lints: ^4.0.0 + flutter_lints: ^4.0.0 test: ^1.24.0 ffigen: