marine_test_macro_impl/
attributes.rs

1/*
2 * Copyright 2020 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 darling::FromMeta;
18use darling::ast::NestedMeta;
19
20use std::collections::HashMap;
21
22/// Describes attributes of `marine_test` macro.
23#[derive(Debug, Clone)]
24pub(crate) enum MTestAttributes {
25    SingleService(ServiceDescription),
26    MultipleServices(HashMap<String, ServiceDescription>),
27}
28
29#[derive(Debug, Default, Clone, FromMeta)]
30pub struct ServiceDescription {
31    /// Path to a config file of a tested service.
32    pub config_path: String,
33
34    /// Path to compiled modules of a service.
35    #[darling(default)]
36    pub modules_dir: Option<String>,
37}
38
39impl FromMeta for MTestAttributes {
40    fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
41        let single_service = ServiceDescription::from_list(items);
42        let multiple_services = HashMap::<String, ServiceDescription>::from_list(items);
43        match (single_service, multiple_services) {
44            (Ok(modules), Err(_)) => Ok(Self::SingleService(modules)),
45            (Err(_), Ok(services)) if !services.is_empty() => Ok(Self::MultipleServices(services)),
46            (Err(_), Ok(_)) => Err(darling::Error::custom(
47                r#"Need to specify "config_path" and "modules_dir" or several named services with these fields "#,
48            )),
49            (Err(error_single), Err(error_multiple)) => Err(darling::error::Error::multiple(vec![
50                error_single,
51                error_multiple,
52            ])),
53            (Ok(_), Ok(_)) => Err(darling::Error::custom(
54                "internal sdk error: marine_test attributes are ambiguous",
55            )),
56        }
57    }
58}