From be74cec48ad64f4d9d0b0f84eece5b239b084b12 Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 7 Jun 2020 07:53:40 +0200 Subject: [PATCH] Refactor instruction parsing --- src/parser.nim | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/parser.nim b/src/parser.nim index e102078..6f5b077 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -347,6 +347,22 @@ proc parseConversation*(js: JsonNode; tweetId: string): Conversation = elif "cursor-bottom" in entry: result.replies.bottom = e.getCursor +proc parseInstructions[T](res: var Result[T]; global: GlobalObjects; js: JsonNode) = + if js.kind != JArray or js.len == 0: + return + + for i in js: + when T is Tweet: + if res.beginning and i{"pinEntry"}.notNull: + with pin, parsePin(i, global): + res.content.add pin + + with r, i{"replaceEntry", "entry"}: + if "top" in r{"entryId"}.getStr: + res.top = r.getCursor + elif "bottom" in r{"entryId"}.getStr: + res.bottom = r.getCursor + proc parseUsers*(js: JsonNode; after=""): Result[Profile] = result = Result[Profile](beginning: after.len == 0) let global = parseGlobalObjects(? js) @@ -354,12 +370,7 @@ proc parseUsers*(js: JsonNode; after=""): Result[Profile] = let instructions = ? js{"timeline", "instructions"} if instructions.len == 0: return - for i in instructions: - with r, i{"replaceEntry", "entry"}: - if "top" in r{"entryId"}.getStr: - result.top = r.getCursor - elif "bottom" in r{"entryId"}.getStr: - result.bottom = r.getCursor + result.parseInstructions(global, instructions) for e in instructions[0]{"addEntries", "entries"}: let entry = e{"entryId"}.getStr @@ -379,16 +390,7 @@ proc parseTimeline*(js: JsonNode; after=""): Timeline = let instructions = ? js{"timeline", "instructions"} if instructions.len == 0: return - for i in instructions: - if result.beginning and i{"pinEntry"}.notNull: - with pin, parsePin(i, global): - result.content.add pin - else: - with r, i{"replaceEntry", "entry"}: - if "top" in r{"entryId"}.getStr: - result.top = r.getCursor - elif "bottom" in r{"entryId"}.getStr: - result.bottom = r.getCursor + result.parseInstructions(global, instructions) for e in instructions[0]{"addEntries", "entries"}: let entry = e{"entryId"}.getStr