chore: Decompose large functions of parser

This commit is contained in:
NaiJi ✨ 2023-02-04 10:40:05 +04:00
parent 963fd4bc1a
commit a1cc611eff
1 changed files with 59 additions and 50 deletions

View File

@ -216,6 +216,64 @@ StageResult _onPathStage(
return StageResult(result: result, stage: currentStage); 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( StageResult _onNothingStage(
final String arg, final String arg,
final ParseResult previousResult, final ParseResult previousResult,
@ -254,56 +312,7 @@ StageResult _onNothingStage(
currentStage = ParsingStage.nothing; currentStage = ParsingStage.nothing;
break; break;
default: default:
try { result = _parseUrl(arg, result);
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;
}
break; break;
} }