From dea3f1bd6778c59cf42cf3434a1d0d71cd41ec39 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 31 Jan 2023 10:40:30 +0400 Subject: [PATCH] feat: Implement flexible args parsing --- bin/main.dart | 9 ++- lib/parsing/parser.dart | 125 +++++++++++++++++++++++++++++++++ lib/parsing/parsing_stage.dart | 8 +++ 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 lib/parsing/parser.dart create mode 100644 lib/parsing/parsing_stage.dart diff --git a/bin/main.dart b/bin/main.dart index 8d21919..a26f977 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -2,15 +2,14 @@ import 'dart:core'; import 'package:funkblubber/funkblubber.dart' as funkblubber; import 'package:funkblubber/console.dart' as console; -import 'package:funkblubber/funkentity.dart'; -import 'package:funkblubber/parser.dart' as parser; +import 'package:funkblubber/parsing/parser.dart' as parser; void main(final List arguments) async { - final object = parser.extract(arguments); - if (object.kind == FunkEntity.error) { + final result = parser.extract(arguments); + if (!result.success) { console.error("'funkblubber --help 'for more"); return; } - await funkblubber.download(object: object); + await funkblubber.download(object: result.object!); } diff --git a/lib/parsing/parser.dart b/lib/parsing/parser.dart new file mode 100644 index 0000000..7960001 --- /dev/null +++ b/lib/parsing/parser.dart @@ -0,0 +1,125 @@ +import 'package:funkblubber/funkentity.dart'; +import 'package:funkblubber/console.dart' as console; +import 'package:funkblubber/parsing/parsing_stage.dart'; + +enum Action { + download, + upload, +} + +class StageResult { + StageResult({required this.result, required this.stage}); + final ParseResult result; + final ParsingStage stage; +} + +class ParseResult { + ParseResult({ + required this.success, + required this.action, + this.object, + this.localPath, + }); + + final FunkObject? object; + final String? localPath; + final Action action; + final bool success; +} + +ParseResult extract(final List args) { + ParseResult result = ParseResult( + success: false, + action: Action.download, + ); + + if (args.isEmpty) { + console.error('no arguments provided'); + return result; + } + + ParsingStage currentStage = ParsingStage.nothing; + + for (final String arg in args) { + switch (currentStage) { + case ParsingStage.nothing: + final stageResult = _onNothingStage(arg, result, currentStage); + currentStage = stageResult.stage; + result = stageResult.result; + break; + default: + console.error('not implemented yet'); + break; + } + } + + return result; +} + +StageResult _onNothingStage( + final String arg, + final ParseResult previousResult, + final ParsingStage previousStage, +) { + ParsingStage currentStage = previousStage; + ParseResult result = previousResult; + switch (arg) { + case '-A': + case '--artist': + currentStage = ParsingStage.artist; + break; + case '-a': + case '--album': + currentStage = ParsingStage.album; + break; + case '-u': + case '--upload': + currentStage = ParsingStage.upload; + break; + case '-p': + case '--path': + currentStage = ParsingStage.path; + break; + default: + try { + final Uri uri = Uri.parse(arg); + + final segments = uri.pathSegments; + for (int i = 0; i < segments.length; ++i) { + switch (segments[i]) { + case 'artists': + result = ParseResult( + action: Action.download, + success: true, + object: FunkObject( + kind: FunkEntity.artist, + id: segments[i + 1], + domain: uri.host, + ), + ); + ++i; + break; + + case 'albums': + result = ParseResult( + action: Action.download, + success: true, + object: FunkObject( + kind: FunkEntity.album, + id: segments[i + 1], + domain: uri.host, + ), + ); + ++i; + break; + } + } + } catch (e) { + console.error(e.toString()); + currentStage = ParsingStage.nothing; + } + break; + } + + return StageResult(result: result, stage: currentStage); +} diff --git a/lib/parsing/parsing_stage.dart b/lib/parsing/parsing_stage.dart new file mode 100644 index 0000000..706b41d --- /dev/null +++ b/lib/parsing/parsing_stage.dart @@ -0,0 +1,8 @@ +enum ParsingStage { + nothing, + album, + artist, + song, + path, + upload, +}