Hi.
I wrote this sitemap parser as a way to learn more about Nim and it's ecosystem.
I would appreciate a code review and some general feedback, if anyone has time. Also it may be useful someone out there.
https://github.com/coneonthefloor/nim-sitemap-parser
Thank you,
Brian
Looking at the code there are a few things I'd suggest although relatively opinionated.
Modules imported from stdlib should be prefixed with std/[a, b, c,].
proc hasURLSet(sitemapContent: string): bool =
let doc = q(sitemapContent)
let urlset = doc.select("urlset")
return len(urlset) > 0
Is 'better' written as
proc hasURLSet(sitemapContent: string): bool =
let doc = q(sitemapContent)
doc.select("urlset").len > 0
There is a error template inside std/logging so you do not need to do log(lvlError,... you can just do error(...).
len(x) is pretty bleh x.len is more idiomatic.
I'd suggest using dot syntax whenever you want something to look like it would in OOP, or if it's a property. For example:
var player = getPlayer("Robert")
player.kill()
# kill(player)
# ^ This wouldn't be as idiomatic since this is an action on the player, and in OOP would be a method on a Player class
echo "Killed player " & player.fullName
# echo "Killed player " & fullName(player)
# ^ Assuming fullName is a proc that combines a player's first and last name but basically acts like a computed property, using a dot accessor without trailing () would make sense
# Keep in mind that object fields (not procs) need to use a dot accessor regardless, since they are not procs and cannot be called
In Nim, there are multiple styles for calling procs:
# Classic C-like
doThing(obj)
# Command-style (or whatever you want to call it), used often for `echo`
doThing obj
# OOP-style (the value on the left of the dot is fed to the proc as its first argument)
obj.doThing()
# Property-style (the value on the left of the dot is fed to the proc as its first and only argument)
obj.doThing
Different styles make sense for different contexts, and your own style preferences. I personally never use the command style outside of logging procs like echo.
Given these rules, you can do weird things like this:
"some text".uppercase().echo
# Would print "SOME TEXT"
# Equivalent to
echo(uppercase("some text"))
# or
echo("some text".uppercase)
# or any other combination of syntax
None of these styles are semantically significant, they all do the same things. The dot syntax can be helpful for things that would otherwise require nesting calls, but it's still just the same as calling with any other style.