Grcov report - file.rs

1

    use std::ffi::OsStr;

2

    use std::path::Path;

3

4

    /// This has been adapted from cross-rs file.rs source

5

    /// https://github.com/cross-rs/cross/blob/4090beca3cfffa44371a5bba524de3a578aa46c3/src/file.rs#L12

6

    pub trait ToUtf8 {

7

      fn to_utf8(&self) -> anyhow::Result<&str>;

8

    }

9

10

    impl ToUtf8 for OsStr {

11
28

      fn to_utf8(&self) -> anyhow::Result<&str> {

12
28

        self

13
28

          .to_str()

14
28

          .ok_or_else(|| anyhow::anyhow!("Unable to convert `{self:?}` to UTF-8 string"))

15
28

      }

16

    }

17

18

    impl ToUtf8 for Path {

19
14

      fn to_utf8(&self) -> anyhow::Result<&str> {

20
14

        self.as_os_str().to_utf8()

21
14

      }

22

    }

23

24

    #[cfg(test)]

25

    mod tests {

26

      use super::*;

27

28

      #[test]

29
10

      fn test_os_str_to_utf8() -> anyhow::Result<()> {

30
10

        let os_str = OsStr::new("hello");

31
10

        assert_eq!(os_str.to_utf8()?, "hello");

32
10

        Ok(())

33
10

      }

34

35

      #[test]

36
10

      fn test_path_to_utf8() -> anyhow::Result<()> {

37
10

        let path = Path::new("hello");

38
10

        assert_eq!(path.to_utf8()?, "hello");

39
10

        Ok(())

40
10

      }

41

    }