marine_macro_testing_utils/
lib.rs

1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::io::Read;
18use std::path::Path;
19
20pub fn stream_from_file<P>(path: P) -> proc_macro2::TokenStream
21where
22    P: AsRef<Path>,
23{
24    let items = items_from_file(path);
25    quote::quote! { #(#items)* }
26}
27
28pub fn items_from_file<P>(path: P) -> Vec<syn::Item>
29where
30    P: AsRef<Path>,
31{
32    let mut file = std::fs::File::open(path).expect("Unable to open file");
33
34    let mut src = String::new();
35    file.read_to_string(&mut src).expect("Unable to read file");
36
37    let token_file = syn::parse_file(&src).expect("Unable to parse file");
38    token_file.items
39}
40
41pub fn to_syn_item(token_stream: proc_macro2::TokenStream) -> Vec<syn::Item> {
42    let file: syn::File = syn::parse2(token_stream).expect("token stream should be parsed");
43    file.items
44}