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