Skip to content

Commit 33d13d0

Browse files
authored
Add analysis options and format (#4)
- Copy analysis_options.yaml from yaru.dart ; - Fix and format code ; - Allow print() until we replace it with a proper logging library.
1 parent 3aa3f9c commit 33d13d0

File tree

9 files changed

+126
-105
lines changed

9 files changed

+126
-105
lines changed
Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
# This file configures the static analysis results for your project (errors,
2-
# warnings, and lints).
3-
#
4-
# This enables the 'recommended' set of lints from `package:lints`.
5-
# This set helps identify many issues that may lead to problems when running
6-
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7-
# style and format.
8-
#
9-
# If you want a smaller set of lints you can change this to specify
10-
# 'package:lints/core.yaml'. These are just the most critical lints
11-
# (the recommended set includes the core lints).
12-
# The core lints are also what is used by pub.dev for scoring packages.
1+
include: package:flutter_lints/flutter.yaml
132

14-
include: package:lints/recommended.yaml
3+
analyzer:
4+
errors:
5+
avoid_print: ignore
156

16-
# Uncomment the following section to specify additional rules.
17-
18-
# linter:
19-
# rules:
20-
# - camel_case_types
21-
22-
# analyzer:
23-
# exclude:
24-
# - path/to/excluded/files/**
25-
26-
# For more information about the core and recommended set of lints, see
27-
# https://dart.dev/go/core-lints
28-
29-
# For additional information about configuring this file, see
30-
# https://dart.dev/guides/language/analysis-options
7+
linter:
8+
rules:
9+
prefer_single_quotes: true
10+
require_trailing_commas: true
11+
always_declare_return_types: true
12+
avoid_catches_without_on_clauses: true
13+
avoid_equals_and_hash_code_on_mutable_classes: true
14+
avoid_types_on_closure_parameters: true
15+
cancel_subscriptions: true
16+
directives_ordering: true
17+
eol_at_end_of_file: true
18+
omit_local_variable_types: true
19+
prefer_asserts_in_initializer_lists: true
20+
prefer_const_constructors: true
21+
prefer_final_in_for_each: true
22+
prefer_final_locals: true
23+
prefer_null_aware_method_calls: true
24+
prefer_null_aware_operators: true
25+
sort_constructors_first: true
26+
sort_unnamed_constructors_first: true
27+
sort_pub_dependencies: true
28+
type_annotate_public_apis: true
29+
unawaited_futures: true
30+
unnecessary_lambdas: true
31+
unnecessary_parenthesis: true
32+
use_named_constants: true
33+
use_super_parameters: true

packages/sane/example/main.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: avoid_print
2+
13
import 'dart:io';
24
import 'dart:typed_data';
35

@@ -10,15 +12,15 @@ void main(List<String> args) async {
1012
await sane.init();
1113

1214
final devices = await sane.getDevices(localOnly: true);
13-
for (var device in devices) {
15+
for (final device in devices) {
1416
print('Device found: ${device.name}');
1517
}
1618

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

1921
final optionDescriptors = await sane.getAllOptionDescriptors(handle);
2022

21-
for (var optionDescriptor in optionDescriptors) {
23+
for (final optionDescriptor in optionDescriptors) {
2224
if (optionDescriptor.name == 'mode') {
2325
await sane.controlStringOption(
2426
handle: handle,
@@ -50,10 +52,10 @@ void main(List<String> args) async {
5052
sane.kill();
5153

5254
Uint8List mergeUint8Lists(List<Uint8List> lists) {
53-
int totalLength = lists.fold(0, (length, list) => length + list.length);
54-
Uint8List result = Uint8List(totalLength);
55-
int offset = 0;
56-
for (var list in lists) {
55+
final totalLength = lists.fold(0, (length, list) => length + list.length);
56+
final result = Uint8List(totalLength);
57+
var offset = 0;
58+
for (final list in lists) {
5759
result.setRange(offset, offset + list.length, list);
5860
offset += list.length;
5961
}
@@ -63,7 +65,7 @@ void main(List<String> args) async {
6365

6466
final file = File('./output.ppm');
6567
file.writeAsStringSync(
66-
"P6\n${parameters.pixelsPerLine} ${parameters.lines}\n255\n",
68+
'P6\n${parameters.pixelsPerLine} ${parameters.lines}\n255\n',
6769
mode: FileMode.write,
6870
);
6971
final rawPixelData = mergeUint8Lists(rawPixelDataList);

packages/sane/lib/src/dylib.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import 'package:sane/src/bindings.g.dart';
44

55
LibSane? _dylib;
66
LibSane get dylib {
7-
return _dylib ??= LibSane(ffi.DynamicLibrary.open("libsane.so"));
7+
return _dylib ??= LibSane(ffi.DynamicLibrary.open('libsane.so'));
88
}

packages/sane/lib/src/exceptions.dart

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,33 @@ import 'package:sane/src/dylib.dart';
1818
/// - [SaneNoMemoryException]
1919
/// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
2020
sealed class SaneException implements Exception {
21-
SANE_Status get _status;
22-
23-
const SaneException._();
24-
2521
factory SaneException(SANE_Status status) {
2622
final exception = switch (status) {
27-
SANE_Status.STATUS_GOOD =>
28-
throw ArgumentError(
29-
'Cannot create SaneException with status STATUS_GOOD',
30-
'status',
31-
),
32-
SANE_Status.STATUS_UNSUPPORTED => SaneUnsupportedException(),
33-
SANE_Status.STATUS_CANCELLED => SaneCancelledException(),
34-
SANE_Status.STATUS_DEVICE_BUSY => SaneDeviceBusyException(),
35-
SANE_Status.STATUS_INVAL => SaneInvalidDataException(),
36-
SANE_Status.STATUS_EOF => SaneEofException(),
37-
SANE_Status.STATUS_JAMMED => SaneJammedException(),
38-
SANE_Status.STATUS_NO_DOCS => SaneNoDocumentsException(),
39-
SANE_Status.STATUS_COVER_OPEN => SaneCoverOpenException(),
40-
SANE_Status.STATUS_IO_ERROR => SaneIoException(),
41-
SANE_Status.STATUS_NO_MEM => SaneNoMemoryException(),
42-
SANE_Status.STATUS_ACCESS_DENIED => SaneAccessDeniedException(),
23+
SANE_Status.STATUS_GOOD => throw ArgumentError(
24+
'Cannot create SaneException with status STATUS_GOOD',
25+
'status',
26+
),
27+
SANE_Status.STATUS_UNSUPPORTED => const SaneUnsupportedException(),
28+
SANE_Status.STATUS_CANCELLED => const SaneCancelledException(),
29+
SANE_Status.STATUS_DEVICE_BUSY => const SaneDeviceBusyException(),
30+
SANE_Status.STATUS_INVAL => const SaneInvalidDataException(),
31+
SANE_Status.STATUS_EOF => const SaneEofException(),
32+
SANE_Status.STATUS_JAMMED => const SaneJammedException(),
33+
SANE_Status.STATUS_NO_DOCS => const SaneNoDocumentsException(),
34+
SANE_Status.STATUS_COVER_OPEN => const SaneCoverOpenException(),
35+
SANE_Status.STATUS_IO_ERROR => const SaneIoException(),
36+
SANE_Status.STATUS_NO_MEM => const SaneNoMemoryException(),
37+
SANE_Status.STATUS_ACCESS_DENIED => const SaneAccessDeniedException(),
4338
};
4439

4540
assert(exception._status == status);
4641

4742
return exception;
4843
}
4944

45+
const SaneException._();
46+
SANE_Status get _status;
47+
5048
String get message {
5149
return dylib.sane_strstatus(_status).cast<Utf8>().toDartString();
5250
}

packages/sane/lib/src/sane.dart

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Sane {
2222
}) {
2323
final completer = Completer<int>();
2424

25-
authCallbackAdapter(
25+
void authCallbackAdapter(
2626
SANE_String_Const resource,
2727
ffi.Pointer<SANE_Char> username,
2828
ffi.Pointer<SANE_Char> password,
@@ -79,7 +79,7 @@ class Sane {
7979

8080
Future<List<SaneDevice>> getDevices({
8181
required bool localOnly,
82-
}) async {
82+
}) {
8383
final completer = Completer<List<SaneDevice>>();
8484

8585
Future(() {
@@ -107,7 +107,7 @@ class Sane {
107107
return completer.future;
108108
}
109109

110-
Future<SaneHandle> open(String deviceName) async {
110+
Future<SaneHandle> open(String deviceName) {
111111
final completer = Completer<SaneHandle>();
112112

113113
Future(() {
@@ -132,7 +132,7 @@ class Sane {
132132
return completer.future;
133133
}
134134

135-
Future<SaneHandle> openDevice(SaneDevice device) async {
135+
Future<SaneHandle> openDevice(SaneDevice device) {
136136
return open(device.name);
137137
}
138138

@@ -231,7 +231,7 @@ class Sane {
231231
valuePointer = ffi.nullptr;
232232

233233
case SaneOptionValueType.group:
234-
throw SaneInvalidDataException();
234+
throw const SaneInvalidDataException();
235235
}
236236

237237
if (action == SaneAction.setValue) {
@@ -267,7 +267,7 @@ class Sane {
267267

268268
invalid:
269269
default:
270-
throw SaneInvalidDataException();
270+
throw const SaneInvalidDataException();
271271
}
272272
}
273273

@@ -287,7 +287,8 @@ class Sane {
287287
switch (optionType) {
288288
case SaneOptionValueType.bool:
289289
result = dartBoolFromSaneBool(
290-
(valuePointer as ffi.Pointer<SANE_Bool>).value);
290+
(valuePointer as ffi.Pointer<SANE_Bool>).value,
291+
);
291292

292293
case SaneOptionValueType.int:
293294
result = (valuePointer as ffi.Pointer<SANE_Int>).value;
@@ -306,16 +307,18 @@ class Sane {
306307
result = null;
307308

308309
default:
309-
throw SaneInvalidDataException();
310+
throw const SaneInvalidDataException();
310311
}
311312

312313
ffi.calloc.free(valuePointer);
313314
ffi.calloc.free(infoPointer);
314315

315-
completer.complete(SaneOptionResult(
316-
result: result,
317-
infos: infos,
318-
));
316+
completer.complete(
317+
SaneOptionResult(
318+
result: result,
319+
infos: infos,
320+
),
321+
);
319322
});
320323

321324
return completer.future;
@@ -326,8 +329,8 @@ class Sane {
326329
required int index,
327330
required SaneAction action,
328331
bool? value,
329-
}) async {
330-
return await _controlOption<bool>(
332+
}) {
333+
return _controlOption<bool>(
331334
handle: handle,
332335
index: index,
333336
action: action,
@@ -340,8 +343,8 @@ class Sane {
340343
required int index,
341344
required SaneAction action,
342345
int? value,
343-
}) async {
344-
return await _controlOption<int>(
346+
}) {
347+
return _controlOption<int>(
345348
handle: handle,
346349
index: index,
347350
action: action,
@@ -354,8 +357,8 @@ class Sane {
354357
required int index,
355358
required SaneAction action,
356359
double? value,
357-
}) async {
358-
return await _controlOption<double>(
360+
}) {
361+
return _controlOption<double>(
359362
handle: handle,
360363
index: index,
361364
action: action,
@@ -368,8 +371,8 @@ class Sane {
368371
required int index,
369372
required SaneAction action,
370373
String? value,
371-
}) async {
372-
return await _controlOption<String>(
374+
}) {
375+
return _controlOption<String>(
373376
handle: handle,
374377
index: index,
375378
action: action,
@@ -380,8 +383,8 @@ class Sane {
380383
Future<SaneOptionResult<Null>> controlButtonOption({
381384
required SaneHandle handle,
382385
required int index,
383-
}) async {
384-
return await _controlOption<Null>(
386+
}) {
387+
return _controlOption<Null>(
385388
handle: handle,
386389
index: index,
387390
action: SaneAction.setValue,
@@ -395,7 +398,9 @@ class Sane {
395398
Future(() {
396399
final nativeParametersPointer = ffi.calloc<SANE_Parameters>();
397400
final status = dylib.sane_get_parameters(
398-
_getNativeHandle(handle), nativeParametersPointer);
401+
_getNativeHandle(handle),
402+
nativeParametersPointer,
403+
);
399404
print('sane_get_parameters() -> ${status.name}');
400405

401406
status.check();

0 commit comments

Comments
 (0)