from typing import List
from typing import Dict
from typing import Optional


def filter_feeds(
    feeds: List[Dict[str, str]],
    only_feeds: Optional[List[str]],
) -> List[Dict[str, str]]:
    """
    Filter items in ``feeds`` to just those specified in ``only_feeds``, if any,
    taking care to ensure that all items in ``only_feeds`` are also in ``feeds``.
    """
    if not only_feeds:
        return feeds
    else:
        all_feed_names = set(feed["name"] for feed in feeds)
        only_feed_names = set(only_feeds)
        if not only_feed_names.issubset(all_feed_names):
            missing_feeds = sorted(only_feed_names.difference(all_feed_names))
            raise ValueError(
                "not all feeds specified in `only` are found in `feeds_fpath`; "
                f"specifically: {missing_feeds}"
            )
        else:
            return [feed for feed in feeds if feed["name"] in only_feed_names]