openlibspot_core/
file_id.rs

1use std::fmt;
2
3use openlibspot_protocol as protocol;
4
5use crate::{spotify_id::to_base16, Error};
6
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct FileId(pub [u8; 20]);
9
10impl FileId {
11    pub fn from_raw(src: &[u8]) -> FileId {
12        let mut dst = [0u8; 20];
13        dst.clone_from_slice(src);
14        FileId(dst)
15    }
16
17    #[allow(clippy::wrong_self_convention)]
18    pub fn to_base16(&self) -> Result<String, Error> {
19        to_base16(&self.0, &mut [0u8; 40])
20    }
21}
22
23impl fmt::Debug for FileId {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.debug_tuple("FileId").field(&self.to_base16()).finish()
26    }
27}
28
29impl fmt::Display for FileId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.write_str(&self.to_base16().unwrap_or_default())
32    }
33}
34
35impl From<&[u8]> for FileId {
36    fn from(src: &[u8]) -> Self {
37        Self::from_raw(src)
38    }
39}
40impl From<&protocol::metadata::Image> for FileId {
41    fn from(image: &protocol::metadata::Image) -> Self {
42        Self::from(image.file_id())
43    }
44}
45
46impl From<&protocol::metadata::AudioFile> for FileId {
47    fn from(file: &protocol::metadata::AudioFile) -> Self {
48        Self::from(file.file_id())
49    }
50}
51
52impl From<&protocol::metadata::VideoFile> for FileId {
53    fn from(video: &protocol::metadata::VideoFile) -> Self {
54        Self::from(video.file_id())
55    }
56}