ink_ir/ir/storage_item/
config.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    ast,
17    utils::duplicate_config_err,
18};
19
20/// The ink! configuration.
21#[derive(Debug, Default, PartialEq, Eq)]
22pub struct StorageItemConfig {
23    /// If set to `true`, all storage related traits are implemented automatically,
24    /// this is the default value.
25    /// If set to `false`, implementing all storage traits is disabled. In some cases
26    /// this can be helpful to override the default implementation of the trait.
27    derive: bool,
28}
29
30impl TryFrom<ast::AttributeArgs> for StorageItemConfig {
31    type Error = syn::Error;
32
33    fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
34        let mut derive: Option<syn::LitBool> = None;
35        for arg in args.into_iter() {
36            if arg.name().is_ident("derive") {
37                if let Some(lit_bool) = derive {
38                    return Err(duplicate_config_err(
39                        lit_bool,
40                        arg,
41                        "derive",
42                        "storage item",
43                    ));
44                }
45                if let Some(lit_bool) = arg.value().and_then(ast::MetaValue::as_lit_bool)
46                {
47                    derive = Some(lit_bool.clone())
48                } else {
49                    return Err(format_err_spanned!(
50                        arg,
51                        "expected a bool literal value for `derive` ink! storage item configuration argument",
52                    ));
53                }
54            } else {
55                return Err(format_err_spanned!(
56                    arg,
57                    "encountered unknown or unsupported ink! storage item configuration argument",
58                ));
59            }
60        }
61        Ok(StorageItemConfig {
62            derive: derive.map(|lit_bool| lit_bool.value).unwrap_or(true),
63        })
64    }
65}
66
67impl StorageItemConfig {
68    /// Returns the derive configuration argument.
69    pub fn derive(&self) -> bool {
70        self.derive
71    }
72}