marine_build_rs_generator/
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
17#![doc(html_root_url = "https://docs.rs/marine-build-rs-generator/0.8.1")]
18#![deny(
19    dead_code,
20    nonstandard_style,
21    unused_imports,
22    unused_mut,
23    unused_variables,
24    unused_unsafe,
25    unreachable_patterns
26)]
27#![warn(rust_2018_idioms)]
28#![recursion_limit = "1024"]
29
30use marine_test_macro_impl::generate_marine_test_env_impl;
31pub use marine_test_macro_impl::ServiceDescription;
32
33use std::path::{PathBuf, Path};
34use std::{fs, env};
35
36pub fn generate_marine_test_env(
37    services: impl IntoIterator<Item = (String, ServiceDescription)>,
38    filename: &str,
39    build_rs_file_path: &str,
40) {
41    // build_rs_file_path expected to be obtained from file!() macro, which returns path with filename,
42    // but underlying code expects path without filename, so we are removing last part
43    let mut build_rs_file_path = PathBuf::from(build_rs_file_path);
44    let _ = build_rs_file_path.pop();
45
46    match generate_marine_test_env_impl(services, &build_rs_file_path) {
47        Ok(stream) => {
48            let out_dir = env::var_os("OUT_DIR")
49                .expect("cannot write marine_test_env: OUT_DIR env var must be set");
50            let dest_path = Path::new(&out_dir).join(filename);
51
52            match fs::write(dest_path, stream.to_string()) {
53                Ok(result) => result,
54                Err(e) => {
55                    std::panic::panic_any(format!("cannot write marine_test_env on disk: {}", e))
56                }
57            }
58        }
59        Err(error) => std::panic::panic_any(format!("marine_test_env generation error: {}", error)),
60    }
61}
62
63#[macro_export]
64macro_rules! include_test_env {
65    ($filename:expr) => {
66        include!(concat!(env!("OUT_DIR"), $filename));
67    };
68}