Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions packages/sane/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 9 additions & 7 deletions packages/sane/example/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: avoid_print

import 'dart:io';
import 'dart:typed_data';

Expand All @@ -10,15 +12,15 @@ void main(List<String> 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}');
}

final handle = await sane.openDevice(devices.first);

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,
Expand Down Expand Up @@ -50,10 +52,10 @@ void main(List<String> args) async {
sane.kill();

Uint8List mergeUint8Lists(List<Uint8List> 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;
}
Expand All @@ -63,7 +65,7 @@ void main(List<String> 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);
Expand Down
2 changes: 1 addition & 1 deletion packages/sane/lib/src/dylib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
38 changes: 18 additions & 20 deletions packages/sane/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,33 @@ import 'package:sane/src/dylib.dart';
/// - [SaneNoMemoryException]
/// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
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);

return exception;
}

const SaneException._();
SANE_Status get _status;

String get message {
return dylib.sane_strstatus(_status).cast<Utf8>().toDartString();
}
Expand Down
51 changes: 28 additions & 23 deletions packages/sane/lib/src/sane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Sane {
}) {
final completer = Completer<int>();

authCallbackAdapter(
void authCallbackAdapter(
SANE_String_Const resource,
ffi.Pointer<SANE_Char> username,
ffi.Pointer<SANE_Char> password,
Expand Down Expand Up @@ -79,7 +79,7 @@ class Sane {

Future<List<SaneDevice>> getDevices({
required bool localOnly,
}) async {
}) {
final completer = Completer<List<SaneDevice>>();

Future(() {
Expand Down Expand Up @@ -107,7 +107,7 @@ class Sane {
return completer.future;
}

Future<SaneHandle> open(String deviceName) async {
Future<SaneHandle> open(String deviceName) {
final completer = Completer<SaneHandle>();

Future(() {
Expand All @@ -132,7 +132,7 @@ class Sane {
return completer.future;
}

Future<SaneHandle> openDevice(SaneDevice device) async {
Future<SaneHandle> openDevice(SaneDevice device) {
return open(device.name);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ class Sane {
valuePointer = ffi.nullptr;

case SaneOptionValueType.group:
throw SaneInvalidDataException();
throw const SaneInvalidDataException();
}

if (action == SaneAction.setValue) {
Expand Down Expand Up @@ -267,7 +267,7 @@ class Sane {

invalid:
default:
throw SaneInvalidDataException();
throw const SaneInvalidDataException();
}
}

Expand All @@ -287,7 +287,8 @@ class Sane {
switch (optionType) {
case SaneOptionValueType.bool:
result = dartBoolFromSaneBool(
(valuePointer as ffi.Pointer<SANE_Bool>).value);
(valuePointer as ffi.Pointer<SANE_Bool>).value,
);

case SaneOptionValueType.int:
result = (valuePointer as ffi.Pointer<SANE_Int>).value;
Expand All @@ -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;
Expand All @@ -326,8 +329,8 @@ class Sane {
required int index,
required SaneAction action,
bool? value,
}) async {
return await _controlOption<bool>(
}) {
return _controlOption<bool>(
handle: handle,
index: index,
action: action,
Expand All @@ -340,8 +343,8 @@ class Sane {
required int index,
required SaneAction action,
int? value,
}) async {
return await _controlOption<int>(
}) {
return _controlOption<int>(
handle: handle,
index: index,
action: action,
Expand All @@ -354,8 +357,8 @@ class Sane {
required int index,
required SaneAction action,
double? value,
}) async {
return await _controlOption<double>(
}) {
return _controlOption<double>(
handle: handle,
index: index,
action: action,
Expand All @@ -368,8 +371,8 @@ class Sane {
required int index,
required SaneAction action,
String? value,
}) async {
return await _controlOption<String>(
}) {
return _controlOption<String>(
handle: handle,
index: index,
action: action,
Expand All @@ -380,8 +383,8 @@ class Sane {
Future<SaneOptionResult<Null>> controlButtonOption({
required SaneHandle handle,
required int index,
}) async {
return await _controlOption<Null>(
}) {
return _controlOption<Null>(
handle: handle,
index: index,
action: SaneAction.setValue,
Expand All @@ -395,7 +398,9 @@ class Sane {
Future(() {
final nativeParametersPointer = ffi.calloc<SANE_Parameters>();
final status = dylib.sane_get_parameters(
_getNativeHandle(handle), nativeParametersPointer);
_getNativeHandle(handle),
nativeParametersPointer,
);
print('sane_get_parameters() -> ${status.name}');

status.check();
Expand Down
Loading