chore: Decompose large functions of parser
This commit is contained in:
parent
963fd4bc1a
commit
a1cc611eff
|
@ -216,6 +216,64 @@ StageResult _onPathStage(
|
|||
return StageResult(result: result, stage: currentStage);
|
||||
}
|
||||
|
||||
ParseResult _makeParseResultFromEntityInfo(
|
||||
final FunkEntity kind,
|
||||
final String host,
|
||||
final String id,
|
||||
) =>
|
||||
ParseResult(
|
||||
action: Action.download,
|
||||
success: true,
|
||||
object: FunkObject(
|
||||
kind: kind,
|
||||
id: id,
|
||||
domain: host,
|
||||
),
|
||||
);
|
||||
|
||||
ParseResult _parseUrl(final String url, final ParseResult previousResult) {
|
||||
ParseResult result = previousResult;
|
||||
try {
|
||||
final Uri uri = Uri.parse(url);
|
||||
|
||||
final segments = uri.pathSegments;
|
||||
for (int i = 0; i < segments.length; ++i) {
|
||||
switch (segments[i]) {
|
||||
case 'artists':
|
||||
result = _makeParseResultFromEntityInfo(
|
||||
FunkEntity.artist,
|
||||
uri.host,
|
||||
segments[i + 1],
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
|
||||
case 'tracks':
|
||||
result = _makeParseResultFromEntityInfo(
|
||||
FunkEntity.track,
|
||||
uri.host,
|
||||
segments[i + 1],
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
|
||||
case 'albums':
|
||||
result = _makeParseResultFromEntityInfo(
|
||||
FunkEntity.album,
|
||||
uri.host,
|
||||
segments[i + 1],
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e.toString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
StageResult _onNothingStage(
|
||||
final String arg,
|
||||
final ParseResult previousResult,
|
||||
|
@ -254,56 +312,7 @@ StageResult _onNothingStage(
|
|||
currentStage = ParsingStage.nothing;
|
||||
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 'tracks':
|
||||
result = ParseResult(
|
||||
action: Action.download,
|
||||
success: true,
|
||||
object: FunkObject(
|
||||
kind: FunkEntity.track,
|
||||
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;
|
||||
}
|
||||
result = _parseUrl(arg, result);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue