1 /*
2  * Copyright (c) 2023 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 "cloud_value_util.h"
17 
18 namespace OHOS::CloudData {
19 namespace SharingUtil {
Convert(const Privilege & privilege)20 SharingPlege Convert(const Privilege &privilege)
21 {
22     SharingPlege result;
23     result.writable = privilege.writable;
24     result.readable = privilege.readable ;
25     result.creatable = privilege.creatable;
26     result.deletable = privilege.deletable;
27     result.shareable = privilege.shareable;
28     return result;
29 }
30 
Convert(const Participant & participant)31 SharingPtpant Convert(const Participant &participant)
32 {
33     SharingPtpant result;
34     result.identity = participant.identity;
35     result.role = participant.role;
36     result.state = participant.state;
37     result.privilege = Convert(participant.privilege);
38     result.attachInfo = participant.attachInfo;
39     return result;
40 }
41 
Convert(const Confirmation & cfm)42 SharingCfm Convert(const Confirmation &cfm)
43 {
44     switch (cfm) {
45         case Confirmation::CFM_UNKNOWN:
46             return SharingCfm::CFM_UNKNOWN;
47         case Confirmation::CFM_ACCEPTED:
48             return SharingCfm::CFM_ACCEPTED;
49         case Confirmation::CFM_REJECTED:
50             return SharingCfm::CFM_REJECTED;
51         case Confirmation::CFM_SUSPENDED:
52             return SharingCfm::CFM_SUSPENDED;
53         case Confirmation::CFM_UNAVAILABLE:
54             return SharingCfm::CFM_UNAVAILABLE;
55         default:
56             break;
57     }
58     return SharingCfm::CFM_UNKNOWN;
59 }
60 
Convert(const SharingPtpant & participant)61 Participant Convert(const SharingPtpant &participant)
62 {
63     Participant result;
64     result.identity = participant.identity;
65     result.role = participant.role;
66     result.state = participant.state;
67     result.privilege = Convert(participant.privilege);
68     result.attachInfo = participant.attachInfo;
69     return result;
70 }
71 
Convert(const SharingCfm & cfm)72 Confirmation Convert(const SharingCfm &cfm)
73 {
74     switch (cfm) {
75         case SharingCfm::CFM_UNKNOWN:
76             return Confirmation::CFM_UNKNOWN;
77         case SharingCfm::CFM_ACCEPTED:
78             return Confirmation::CFM_ACCEPTED;
79         case SharingCfm::CFM_REJECTED:
80             return Confirmation::CFM_REJECTED;
81         case SharingCfm::CFM_SUSPENDED:
82             return Confirmation::CFM_SUSPENDED;
83         case SharingCfm::CFM_UNAVAILABLE:
84             return Confirmation::CFM_UNAVAILABLE;
85         default:
86             break;
87     }
88     return Confirmation::CFM_UNKNOWN;
89 }
90 
Convert(const SharingPlege & privilege)91 Privilege Convert(const SharingPlege &privilege)
92 {
93     Privilege result;
94     result.writable = privilege.writable;
95     result.readable = privilege.readable ;
96     result.creatable = privilege.creatable;
97     result.deletable = privilege.deletable;
98     result.shareable = privilege.shareable;
99     return result;
100 }
101 
Convert(const SharingResults & results)102 QueryResults Convert(const SharingResults &results)
103 {
104     return { std::get<0>(results), std::get<1>(results),
105         Convert<SharingPtpant, Participant>(std::get<2>(results)) };
106 }
107 
Convert(const std::vector<Participant> & participants)108 std::vector<SharingPtpant> Convert(const std::vector<Participant> &participants)
109 {
110     return Convert<Participant, SharingPtpant>(participants);
111 }
112 
Convert(const std::vector<SharingPtpant> & input)113 std::vector<Participant> Convert(const std::vector<SharingPtpant> &input)
114 {
115     return Convert<SharingPtpant, Participant>(input);
116 }
117 
118 template<typename T, typename O>
Convert(const std::vector<T> & data)119 std::vector<O> Convert(const std::vector<T> &data)
120 {
121     std::vector<O> out;
122     out.reserve(data.size());
123     for (const auto &v : data) {
124         out.emplace_back(Convert(v));
125     }
126     return out;
127 }
128 
Convert(CenterCode code)129 Status Convert(CenterCode code)
130 {
131     switch (code) {
132         case CenterCode::IPC_ERROR:
133             return Status::IPC_ERROR;
134         default:
135             break;
136     }
137     return Status::SUCCESS;
138 }
139 
Convert(GenErr code)140 Status Convert(GenErr code)
141 {
142     switch (code) {
143         case GenErr::E_OK:
144             return Status::SUCCESS;
145         case GenErr::E_ERROR:
146             return Status::ERROR;
147         case GenErr::E_INVALID_ARGS:
148             return Status::INVALID_ARGUMENT;
149         case GenErr::E_BLOCKED_BY_NETWORK_STRATEGY:
150             return Status::STRATEGY_BLOCKING;
151         case GenErr::E_CLOUD_DISABLED:
152             return Status::CLOUD_DISABLE;
153         case GenErr::E_NETWORK_ERROR:
154             return Status::NETWORK_ERROR;
155         default:
156             break;
157     }
158     return Status::ERROR;
159 }
160 } // namespace::SharingUtil
161 } // namespace OHOS::CloudData
162