notation_dsl/
helper.rs

1use crate::prelude::{GetTabDsl, TabDsl};
2use anyhow::Error;
3use quote::ToTokens;
4use std::fs::File;
5use std::io::Read;
6use syn;
7
8use notation_proto::prelude::*;
9
10pub fn parse_get_tab(content: &str) -> Result<Tab, Error> {
11    let ast = syn::parse_file(content)?;
12    //println!("Last Item: {:#?}", ast.items.last().unwrap().as);
13    let tokens = ast.items.last().unwrap().to_token_stream();
14    //println!("{:#?}", tokens);
15    let get_tab: GetTabDsl = syn::parse2(tokens)?;
16    let tab = get_tab.tab;
17    //println!("Tab: T:{}, S:{}", tab.tracks.len(), tab.sections.len());
18    Ok(tab.to_proto())
19}
20pub fn parse_get_tab_file(path: &str) -> Result<Tab, Error> {
21    let mut file = File::open(path)?;
22    let mut content = String::new();
23    file.read_to_string(&mut content)?;
24    println!("parse_get_tab_file: {} -> [{}]", path, content.len());
25    parse_get_tab(&content)
26}
27
28pub fn parse_tab(content: &str) -> Result<Tab, Error> {
29    let tab = syn::parse_str::<TabDsl>(content)?;
30    //println!("Tab: T:{}, S:{}", tab.tracks.len(), tab.sections.len());
31    Ok(tab.to_proto())
32}
33pub fn parse_tab_file(path: &str) -> Result<Tab, Error> {
34    let mut file = File::open(path)?;
35    let mut content = String::new();
36    file.read_to_string(&mut content)?;
37    println!("parse_tab_file: {} -> [{}]", path, content.len());
38    parse_tab(&content)
39}