def _resolve_toplevel_var(
    dom: "r.Domain",
    state: "state_def.State",
    limits: dict["t.Variable", int] = None,
) -> typing.Iterator[str]:
    """
    Find and yield all valid substitutions of a toplevel VariableTag in a given dom

    ASSUMES: there is at most one variable in the domain, and it is at the top level
    """

    if limits is None:
        limits = {}

    vars = [ti for ti in dom.tags if isinstance(ti, t.Variable)]
    if len(vars) == 0:
        yield dom
        return
    elif len(vars) > 1:
        raise ValueError(f"More than one variable in domain {dom}")

    # valid assignments for the var are any objs satisfying everything else in the domain
    vartag = vars[0]
    result = copy.deepcopy(dom)
    result.tags.remove(vartag)
    objkeys = objkeys_in_dom(result, state)
    logger.debug(
        f"Found {len(objkeys)} valid assignments for {repr(vartag)} via {result} on "
    )

    # if the user says limit "room" to 3 and we are doing "room", apply the limit
    name_limit = limits.get(vartag, None)
    if name_limit is not None:
        objkeys = objkeys[:name_limit]

    for objkey in objkeys:
        logger.debug(f"Assigning {objkey} for {vartag}")
        yield result.with_tags(state.objs[objkey].tags)

    Find and yield all valid substitutions of a toplevel VariableTag in a given dom

    ASSUMES: there is at most one variable in the domain, and it is at the top level
    
infinigen/core/constraints/example_solver/greedy/all_substitutions.py