And this loop + match seems rather confusing.

Is this an accurate summary?

We run through the list of directives, finding the longest one that is a prefix of our current name, or defaulting to the global level otherwise.