1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 */
15
16#include "ast/ast_attribute.h"
17#include "util/string_builder.h"
18
19namespace OHOS {
20namespace Idl {
21std::string ASTAttr::ToString() const
22{
23    std::vector<std::string> attrs;
24    if (value_ & ASTAttr::MINI) {
25        attrs.push_back("mini");
26    }
27
28    if (value_ & ASTAttr::LITE) {
29        attrs.push_back("lite");
30    }
31
32    if (value_ & ASTAttr::FULL) {
33        attrs.push_back("full");
34    }
35
36    if (value_ & ASTAttr::ONEWAY) {
37        attrs.push_back("oneway");
38    }
39
40    if (value_ & ASTAttr::CALLBACK) {
41        attrs.push_back("callback");
42    }
43
44    StringBuilder sb;
45    sb.Append("[");
46    for (size_t i = 0; i < attrs.size(); i++) {
47        sb.Append(attrs[i]);
48        if (i + 1 < attrs.size()) {
49            sb.Append(", ");
50        }
51    }
52    sb.Append("] ");
53    return sb.ToString();
54}
55
56std::string ASTAttr::Dump(const std::string &prefix)
57{
58    return prefix + ToString();
59}
60
61bool ASTAttr::Match(SystemLevel level) const
62{
63    switch (level) {
64        case SystemLevel::MINI:
65            return HasValue(ASTAttr::MINI);
66        case SystemLevel::LITE:
67            return HasValue(ASTAttr::LITE);
68        case SystemLevel::FULL:
69            return HasValue(ASTAttr::FULL);
70        default:
71            return false;
72    }
73}
74
75std::string ASTParamAttr::ToString() const
76{
77    std::vector<std::string> attrs;
78    if (value_ & ASTParamAttr::PARAM_IN) {
79        attrs.push_back("in");
80    }
81
82    if (value_ & ASTParamAttr::PARAM_OUT) {
83        attrs.push_back("out");
84    }
85
86    StringBuilder sb;
87    sb.Append("[");
88    for (size_t i = 0; i < attrs.size(); i++) {
89        sb.Append(attrs[i]);
90        if (i + 1 < attrs.size()) {
91            sb.Append(", ");
92        }
93    }
94    sb.Append("]");
95    return sb.ToString();
96}
97
98std::string ASTParamAttr::Dump(const std::string &prefix)
99{
100    return prefix + ToString();
101}
102
103bool ASTAttr::CacheableStrToInt()
104{
105    if (!HasValue(ASTAttr::CACHEABLE)) {
106        return false;
107    }
108    try {
109        cacheableTime_ = static_cast<int32_t>(std::stoi(cacheableTimeString_));
110    } catch(...) {
111        return false;
112    }
113    return true;
114}
115} // namespace Idl
116} // namespace OHOS