# torrent file: https://cdimage.debian.org/debian-cd/current/amd64/bt-cd/debian-13.1.0-amd64-netinst.iso.torrent
# lib file: https://raku.land/zef:4zv4l/Bencode
grammar Bencode {
token elem {
| <integer> {make $<integer>.Int}
| <list> {make $<list><elem>.List.map(*.made)}
| <dictionary> {make %($<dictionary><entry>.map({$_<key>.Str,$_<value>.made}).flat)}
| <string> {make $<string>.Str}
}
token integer { i <(\-?\d+)> e }
token string { $<len> = \d+ {say "len: $<len>"} ':' <(. ** {$<len>})> {say "done ({$/.Str.encode('latin-1').bytes}) !"} }
token list { l <( <elem>* )> e }
token dictionary { d <( $<entry> = (<key=string> <value=elem>)* )> e }
}
say Bencode.parsefile('debian.torrent', :rule('elem'), :enc('latin-1'));