diff --git a/.gitmodules b/.gitmodules index 080829e3..a41b7c98 100644 --- a/.gitmodules +++ b/.gitmodules @@ -120,3 +120,6 @@ [submodule "vendor/combparser"] path = vendor/combparser url = https://github.com/PMunch/combparser.git +[submodule "vendor/edn.nim"] + path = vendor/edn.nim + url = https://github.com/status-im/edn.nim diff --git a/status/private/stickers.nim b/status/private/stickers.nim new file mode 100644 index 00000000..8311680f --- /dev/null +++ b/status/private/stickers.nim @@ -0,0 +1,171 @@ +{.push raises: [Defect].} + +import # std libs + std/strutils + +import # vendor libs + chronicles, edn, libp2p/[multihash, multicodec, cid], nimcrypto, stint + +import + ./common, ./util + +export cid + +type + StickersError* = enum + CidV0InitFailure = "stickers: failed to init CIDv0 from codec and multihash" + EdnNodeTypeUnknown = "stickers: couldn't decode EDN, node type unknown" + EdnReadError = "stickers: error reading EDN string" + HashValueError = "stickers: provided hash must not be an empty string" + HexIntParseError = "stickers: error parsing string to hex int" + InvalidMultiCodec = "stickers: content hash contains an invalid codec" + MultiHashInitFailure = "stickers: failed to init MultiHash given the hash" + + StickersResult*[T] = Result[T, StickersError] + + Sticker* = object + hash*: string + packId*: int + + StickerPack* = object + author*: string + id*: int + name*: string + price*: Stuint[256] + preview*: string + stickers*: seq[Sticker] + thumbnail*: string + +# forward declaration: +proc parseNode[T](node: EdnNode, searchName: string): StickersResult[T] +proc parseMap[T](map: HMap, searchName: string,): StickersResult[T] + +proc getValueFromNode[T](node: EdnNode): StickersResult[T] {.raises: [].} = + if node.kind == EdnSymbol: + when T is string: + return ok node.symbol.name + elif node.kind == EdnKeyword: + when T is string: + return ok node.keyword.name + elif node.kind == EdnString: + when T is string: + return ok node.str + elif node.kind == EdnCharacter: + when T is string: + return ok node.character + elif node.kind == EdnBool: + when T is bool: + return ok node.boolVal + elif node.kind == EdnInt: + when T is int: + return ok node.num.int + else: + return err EdnNodeTypeUnknown + +proc parseVector[T: seq[Sticker]](node: EdnNode, searchName: string): + StickersResult[T] = + + var vector: T = @[] + + for i in 0.. 0: + for iChild in 0..