To make the purpose clearer, this could be renamed to 'maybe_to_entity'. Furthermore, the fact that this can return an error tuple rather than 'ok' on a case means some of your specs are wrong.