LCOV - code coverage report
Current view: top level - internal/src - filename.rs (source / functions) Hit Total Coverage
Test: unnamed Lines: 22 22 100.0 %
Date: 2024-10-23 19:20:50 Functions: 4 4 100.0 %

          Line data    Source code
       1             : use std::{env::consts, path::Path};
       2             : 
       3             : /// Returns the filename of a Dylint library.
       4             : ///
       5             : /// # Examples
       6             : ///
       7             : /// ```
       8             : /// use dylint_internal::library_filename;
       9             : ///
      10             : /// #[cfg(target_os = "linux")]
      11             : /// assert_eq!(
      12             : ///     library_filename("foo", "stable-x86_64-unknown-linux-gnu"),
      13             : ///     "libfoo@stable-x86_64-unknown-linux-gnu.so"
      14             : /// );
      15             : ///
      16             : /// #[cfg(target_os = "macos")]
      17             : /// assert_eq!(
      18             : ///     library_filename("foo", "stable-x86_64-apple-darwin"),
      19             : ///     "libfoo@stable-x86_64-apple-darwin.dylib"
      20             : /// );
      21             : ///
      22             : /// #[cfg(target_os = "windows")]
      23             : /// assert_eq!(
      24             : ///     library_filename("foo", "stable-x86_64-pc-windows-msvc"),
      25             : ///     "foo@stable-x86_64-pc-windows-msvc.dll"
      26             : /// );
      27             : /// ```
      28             : // smoelius: Build a standard rlib, and the filename will use snake case. `library_filename`'s
      29             : // behavior is consistent with that.
      30             : #[allow(clippy::module_name_repetitions, clippy::uninlined_format_args)]
      31             : #[must_use]
      32          25 : pub fn library_filename(lib_name: &str, toolchain: &str) -> String {
      33          25 :     format!(
      34          25 :         "{}{}@{}{}",
      35          25 :         consts::DLL_PREFIX,
      36          25 :         lib_name.replace('-', "_"),
      37          25 :         toolchain,
      38          25 :         consts::DLL_SUFFIX
      39          25 :     )
      40          25 : }
      41             : 
      42             : /// Parses the filename of a Dylint library path into a tuple of (name, toolchain).
      43             : ///
      44             : /// # Examples
      45             : ///
      46             : /// ```
      47             : /// use dylint_internal::parse_path_filename;
      48             : /// use std::path::Path;
      49             : ///
      50             : /// #[cfg(target_os = "linux")]
      51             : /// assert_eq!(
      52             : ///     parse_path_filename(Path::new("libfoo@stable-x86_64-unknown-linux-gnu.so")),
      53             : ///     Some((
      54             : ///         String::from("foo"),
      55             : ///         String::from("stable-x86_64-unknown-linux-gnu")
      56             : ///     ))
      57             : /// );
      58             : ///
      59             : /// #[cfg(target_os = "macos")]
      60             : /// assert_eq!(
      61             : ///     parse_path_filename(Path::new("libfoo@stable-x86_64-apple-darwin.dylib")),
      62             : ///     Some((
      63             : ///         String::from("foo"),
      64             : ///         String::from("stable-x86_64-apple-darwin")
      65             : ///     ))
      66             : /// );
      67             : ///
      68             : /// #[cfg(target_os = "windows")]
      69             : /// assert_eq!(
      70             : ///     parse_path_filename(Path::new("foo@stable-x86_64-pc-windows-msvc.dll")),
      71             : ///     Some((
      72             : ///         String::from("foo"),
      73             : ///         String::from("stable-x86_64-pc-windows-msvc")
      74             : ///     ))
      75             : /// );
      76             : /// ```
      77             : #[allow(clippy::module_name_repetitions)]
      78             : #[must_use]
      79         235 : pub fn parse_path_filename(path: &Path) -> Option<(String, String)> {
      80         235 :     let filename = path.file_name()?;
      81         235 :     parse_filename(&filename.to_string_lossy())
      82         235 : }
      83             : 
      84             : #[allow(clippy::module_name_repetitions)]
      85             : #[must_use]
      86         235 : pub fn parse_filename(filename: &str) -> Option<(String, String)> {
      87         235 :     let file_stem = filename.strip_suffix(consts::DLL_SUFFIX)?;
      88         127 :     let target_name = file_stem.strip_prefix(consts::DLL_PREFIX)?;
      89         127 :     parse_target_name(target_name)
      90         235 : }
      91             : 
      92         127 : fn parse_target_name(target_name: &str) -> Option<(String, String)> {
      93         127 :     let (lib_name, toolchain) = target_name.split_once('@')?;
      94          83 :     Some((lib_name.to_owned(), toolchain.to_owned()))
      95         127 : }

Generated by: LCOV version 1.14