notation_dsl/proto/
track.rs

1use fehler::throws;
2
3use notation_proto::prelude::{Track, TrackKind};
4use proc_macro2::TokenStream;
5use quote::{quote, ToTokens};
6use syn::parse::{Error, ParseStream};
7use syn::Ident;
8
9use crate::proto::entry::EntryDsl;
10
11use super::id::IdDsl;
12
13pub struct TrackDsl {
14    pub id: IdDsl,
15    pub kind: Ident,
16    pub entries: Vec<EntryDsl>,
17}
18
19impl TrackDsl {
20    #[throws(Error)]
21    pub fn parse_without_brace(input: ParseStream) -> Self {
22        let id = input.parse()?;
23        let kind = input.parse()?;
24        let entries = EntryDsl::parse_vec(input)?;
25        TrackDsl { id, kind, entries }
26    }
27}
28
29impl ToTokens for TrackDsl {
30    fn to_tokens(&self, tokens: &mut TokenStream) {
31        let TrackDsl { id, kind, entries } = self;
32        let kind_quote = kind.to_string();
33        let entries_quote = EntryDsl::quote_vec(entries);
34        tokens.extend(quote! {
35            Track::new(#id.into(), TrackKind::from_ident(#kind_quote), #entries_quote)
36        });
37    }
38}
39
40impl TrackDsl {
41    pub fn to_proto(&self) -> Track {
42        let mut entries = Vec::new();
43        for entry in self.entries.iter() {
44            entry.add_proto(&mut entries);
45        }
46        Track::new(
47            self.id.id.clone(),
48            TrackKind::from_ident(self.kind.to_string().as_str()),
49            entries,
50        )
51    }
52}