Debian Codegraph @

src/internal.rs

raw symbols SCIP
1 // Items that only pertain to internal usage of lofty_attr
2
3 use crate::lofty_file::FieldContents;
4
5 use std::collections::HashMap;
6
7 use quote::quote;
8
9 pub(crate) fn opt_internal_file_type(
10 struct_name: String,
11 ) -> Option<(proc_macro2::TokenStream, bool)> {
12 const LOFTY_FILE_TYPES: [&str; 12] = [
13 "Aac", "Aiff", "Ape", "Flac", "Mpeg", "Mp4", "Mpc", "Opus", "Vorbis", "Speex", "Wav",
14 "WavPack",
15 ];
16
17 const ID3V2_STRIPPABLE: [&str; 2] = ["Flac", "Ape"];
18
19 let stripped = struct_name.strip_suffix("File");
20 if let Some(prefix) = stripped {
21 if let Some(pos) = LOFTY_FILE_TYPES
22 .iter()
23 .position(|p| p.eq_ignore_ascii_case(prefix))
24 {
25 let file_ty = LOFTY_FILE_TYPES[pos];
26 let tt = file_ty.parse::<proc_macro2::TokenStream>().unwrap();
27
28 return Some((tt, ID3V2_STRIPPABLE.contains(&file_ty)));
29 }
30 }
31
32 None
33 }
34
35 pub(crate) fn init_write_lookup(
36 id3v2_strippable: bool,
37 ) -> HashMap<&'static str, proc_macro2::TokenStream> {
38 let mut map = HashMap::new();
39
40 macro_rules! insert {
41 ($map:ident, $key:path, $val:block) => {
42 $map.insert(stringify!($key), quote! { $val })
43 };
44 }
45
46 insert!(map, Ape, {
47 lofty::ape::tag::ApeTagRef {
48 read_only: false,
49 items: lofty::ape::tag::tagitems_into_ape(tag),
50 }
51 .write_to(file, write_options)
52 });
53
54 insert!(map, Id3v1, {
55 Into::<lofty::id3::v1::tag::Id3v1TagRef<'_>>::into(tag).write_to(file, write_options)
56 });
57
58 if id3v2_strippable {
59 insert!(map, Id3v2, {
60 lofty::id3::v2::tag::Id3v2TagRef::empty().write_to(file, write_options)
61 });
62 } else {
63 insert!(map, Id3v2, {
64 lofty::id3::v2::tag::Id3v2TagRef {
65 flags: lofty::id3::v2::Id3v2TagFlags::default(),
66 frames: lofty::id3::v2::tag::tag_frames(tag).peekable(),
67 }
68 .write_to(file, write_options)
69 });
70 }
71
72 insert!(map, RiffInfo, {
73 lofty::iff::wav::tag::RIFFInfoListRef::new(lofty::iff::wav::tag::tagitems_into_riff(
74 tag.items(),
75 ))
76 .write_to(file, write_options)
77 });
78
79 insert!(map, AiffText, {
80 lofty::iff::aiff::tag::AiffTextChunksRef {
81 name: tag.get_string(&lofty::prelude::ItemKey::TrackTitle),
82 author: tag.get_string(&lofty::prelude::ItemKey::TrackArtist),
83 copyright: tag.get_string(&lofty::prelude::ItemKey::CopyrightMessage),
84 annotations: Some(tag.get_strings(&lofty::prelude::ItemKey::Comment)),
85 comments: None,
86 }
87 .write_to(file, write_options)
88 });
89
90 map
91 }
92
93 pub(crate) fn write_module(
94 fields: &[FieldContents],
95 lookup: HashMap<&'static str, proc_macro2::TokenStream>,
96 ) -> proc_macro2::TokenStream {
97 let applicable_formats = fields.iter().map(|f| {
98 let tag_ty =
99 syn::parse_str::<syn::Path>(&format!("::lofty::tag::TagType::{}", &f.tag_type))
100 .unwrap();
101
102 let cfg_features = f.get_cfg_features();
103
104 let block = lookup.get(&*tag_ty.segments[3].ident.to_string()).unwrap();
105
106 quote! {
107 #( #cfg_features )*
108 #tag_ty => #block,
109 }
110 });
111
112 quote! {
113 pub(crate) mod write {
114 #[allow(unused_variables)]
115 pub(crate) fn write_to<F>(file: &mut F, tag: &::lofty::tag::Tag, write_options: ::lofty::config::WriteOptions) -> ::lofty::error::Result<()>
116 where
117 F: ::lofty::io::FileLike,
118 ::lofty::error::LoftyError: ::std::convert::From<<F as ::lofty::io::Truncate>::Error>,
119 ::lofty::error::LoftyError: ::std::convert::From<<F as ::lofty::io::Length>::Error>,
120 {
121 match tag.tag_type() {
122 #( #applicable_formats )*
123 _ => crate::macros::err!(UnsupportedTag),
124 }
125 }
126 }
127 }
128 }