Skip to content

Commit

Permalink
Refactor instruction parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 7, 2020
1 parent 67313ac commit be74cec
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,30 @@ 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)

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
Expand All @@ -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
Expand Down

0 comments on commit be74cec

Please sign in to comment.