You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: filter.rs
    path of file: ./repos/crunchy-cli/crunchy-cli-core/src/download
    
    the code of the file until where you have to start completion: use crate::download::Download;
use crate::utils::filter::Filter;
use crate::utils::format::{Format, SingleFormat, SingleFormatCollection};
use crate::utils::interactive_select::{check_for_duplicated_seasons, get_duplicated_seasons};
use crate::utils::parse::{fract, UrlFilter};
use anyhow::{bail, Result};
use crunchyroll_rs::{Concert, Episode, Movie, MovieListing, MusicVideo, Season, Series};
use log::{error, info, warn};
use std::collections::HashMap;

pub(crate) struct DownloadFilter {
    url_filter: UrlFilter,
    download: Download,
    interactive_input: bool,
    skip_special: bool,
    season_episodes: HashMap<u32, Vec<Episode>>,
    season_subtitles_missing: Vec<u32>,
    season_visited: bool,
}

impl DownloadFilter {
    pub(crate) fn new(
        url_filter: UrlFilter,
        download: Download,
        interactive_input: bool,
        skip_special: bool,
    ) -> Self {
        Self {
            url_filter,
            download,
            interactive_input,
            skip_special,
            season_episodes: HashMap::new(),
            season_subtitles_missing: vec![],
            season_visited: false,
        }
    }
}

impl Filter for DownloadFilter {
    type T = SingleFormat;
    type Output = SingleFormatCollection;

    async fn visit_series(&mut self, series: Series) -> Result<Vec<Season>> {
        // `series.audio_locales` isn't always populated b/c of crunchyrolls api. so check if the
        // audio is matching only if the field is populated
        if !series.audio_locales.is_empty() && !series.audio_locales.contains(&self.download.audio)
        {
            error!(
                "Series {} is not available with {} audio",
                series.title, self.download.audio
            );
            return Ok(vec![]);
        }

        let mut seasons = vec![];
        for mut season in series.seasons().await? {
            if !self.url_filter.is_season_valid(season.season_number) {
                continue;
            }

            if !season
                .audio_locales
                .iter()
                .any(|l| l =
    
    async fn visit_episode(&mut self, mut episode: Episode) -> Result<Option<Self::T>> {
        if !self
            .url_filter
            .is_episode_valid(episode.sequence_number, episode.season_number)
        {
            return Ok(None);
        }

        // skip the episode if it's a special
        if self.skip_special
            && (episode.sequence_number == 0.0 || episode.sequence_number.fract() != 0.0)
        {
            return Ok(None);
        }

        // check if the audio locale is correct.
        // should only be incorrect if the console input was a episode url. otherwise
        // `DownloadFilter::visit_season` returns the correct episodes with matching audio
        if episode.audio_locale != self.download.audio {
            // check if any other version (same episode, other language) of this episode is available
            // with the requested audio. if not, return an error
            if !episode
                .available_versions()
                .await?
                .contains(&self.download.audio)
            {
                let error_message = format!(
                    "Episode {} ({}) of {} season {} is not available with {} audio",
                    episode.sequence_number,
                    episode.title,
                    episode.series_title,
                    episode.season_number,
                    self.download.audio
                );
                // sometimes a series randomly has episode in an other language. if this is the case,
                // only error if the input url was a episode url
                if self.season_visited {
                    warn!("{}", error_message);
                    return Ok(None);
                } else {
                    bail!("{}", error_message)
                }
            }
            // overwrite the current episode with the other version episode
            episode = episode
                .version(vec![self.download.audio.clone()])
                .await?
                .remove(0)
        }

        // check if the subtitles are supported
        if let Some(subtitle_locale) = &self.download.subtitle {
            if !episode.subtitle_locales.contains(subtitle_locale) {
                // if the episode doesn't have the requested subtitles, print a error. to print this
                // error only once per season, it's checked if an error got printed before by looking
                // up if the season id is present in `self.season_subtitles_missing`. if not, print
                // the error and add the season id to `self.season_subtitles_missing`. if it is
                // present, skip the error printing
                if !self
                    .season_subtitles_missing
                    .contains(&episode.season_number)
                {
                    self.season_subtitles_missing.push(episode.season_number);
                    error!(
                        "{} season {} is not available with {} subtitles",
                        episode.series_title, episode.season_number, subtitle_locale
                    );
                }
                return Ok(None);
            }
        }

        let mut relative_episode_number = None;
        let mut relative_sequence_number = None;
        // get the relative episode number. only done if the output string has the pattern to include
        // the relative episode number as this requires some extra fetching
        if Format::has_relative_fmt(&self.download.output) {
            let season_eps = match self.season_episodes.get(&episode.season_number) {
                Some(eps) => eps,
                None => {
                    self.season_episodes.insert(
                        episode.season_number,
                        episode.season().await?.episodes().await?,
                    );
                    self.season_episodes.get(&episode.season_number).unwrap()
                }
            };
            let mut non_integer_sequence_number_count = 0;
            for (i, ep) in season_eps.iter().enumerate() {
                if ep.sequence_number.fract() != 0.0 || ep.sequence_number == 0.0 {
                    non_integer_sequence_number_count += 1;
                }
                if ep.id == episode.id {
                    relative_episode_number = Some(i + 1);
                    relative_sequence_number = Some(
                        (i + 1 - non_integer_sequence_number_count) as f32
                            + fract(ep.sequence_number),
                    );
                    break;
                }
            }
            if relative_episode_number.is_none() || relative_sequence_number.is_none() {
                warn!(
                    "Failed to get relative episode number for episode {} ({}) of {} season {}",
                    episode.sequence_number,
                    episode.title,
                    episode.series_title,
                    episode.season_number,
                )
            }
        }

        Ok(Some(SingleFormat::new_from_episode(
            episode.clone(),
            self.download.subtitle.clone().map_or(vec![], |s| {
                if episode.subtitle_locales.contains(&s) {
                    vec![s]
                } else {
                    vec![]
                }
            }),
            relative_episode_number.map(|n| n as u32),
            relative_sequence_number,
        )))
    }