Repository: astral-sh/ruff
File: crates/ruff_linter/src/settings/types.rs (Rust)

Code before:
    }
}

impl From<ExtensionPair> for (String, Language) {
    fn from(value: ExtensionPair) -> Self {
        (value.extension, value.language)
    }
}

#[derive(Debug, Clone, Default, CacheKey)]
pub struct ExtensionMapping(FxHashMap<String, Language>);

impl ExtensionMapping {
    /// Return the [`Language`] for the given file.
    pub fn get(&self, path: &Path) -> Option<Language> {
        let ext = path.extension()?.to_str()?;
        self.0.get(ext).copied()
    }

    /// Return the [`Language`] for a given file extension.
    pub fn get_extension(&self, ext: &str) -> Option<Language> {
        self.0.get(ext).copied()
    }

    /// Return a mapped [`SourceType`] for a given path.
    pub fn mapped_path(&self, path: &Path) -> SourceType {
        match self.get(path) {
            None => SourceType::from(path),
            Some(language) => SourceType::from(language),
        }
    }

    /// Return a mapped [`SourceType`] for a given file extension.
    pub fn mapped_extension(&self, ext: &str) -> SourceType {
        match self.get_extension(ext) {
            None => SourceType::from_extension(ext),
            Some(language) => SourceType::from(language),
        }
    }
}

impl From<FxHashMap<String, Language>> for ExtensionMapping {
    fn from(value: FxHashMap<String, Language>) -> Self {
        Self(value)
    }
}

impl FromIterator<ExtensionPair> for ExtensionMapping {
    fn from_iter<T: IntoIterator<Item = ExtensionPair>>(iter: T) -> Self {
        Self(
            iter.into_iter()

Review comment (suggestion):
Not sure about the best name either. I would probably call this `get_source_type`, but that leaves the pretty unwieldy `get_source_type_by_extension` or similar for the other variant.

Code after:
    }
}

impl From<ExtensionPair> for (String, Language) {
    fn from(value: ExtensionPair) -> Self {
        (value.extension, value.language)
    }
}

#[derive(Debug, Clone, Default, CacheKey)]
pub struct ExtensionMapping(FxHashMap<String, Language>);

impl ExtensionMapping {
    /// Return the [`Language`] for the given file.
    pub fn get(&self, path: &Path) -> Option<Language> {
        let ext = path.extension()?.to_str()?;
        self.0.get(ext).copied()
    }

    /// Return the [`Language`] for a given file extension.
    pub fn get_extension(&self, ext: &str) -> Option<Language> {
        self.0.get(ext).copied()
    }

    /// Return a mapped [`SourceType`] for a given path.
    pub fn get_source_type(&self, path: &Path) -> SourceType {
        match self.get(path) {
            None => SourceType::from(path),
            Some(language) => SourceType::from(language),
        }
    }

    /// Return a mapped [`SourceType`] for a given file extension.
    pub fn get_source_type_by_extension(&self, ext: &str) -> SourceType {
        match self.get_extension(ext) {
            None => SourceType::from_extension(ext),
            Some(language) => SourceType::from(language),
        }
    }
}

impl From<FxHashMap<String, Language>> for ExtensionMapping {
    fn from(value: FxHashMap<String, Language>) -> Self {
        Self(value)
    }
}

impl FromIterator<ExtensionPair> for ExtensionMapping {
    fn from_iter<T: IntoIterator<Item = ExtensionPair>>(iter: T) -> Self {
        Self(
            iter.into_iter()