1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 pub(crate) use ffi::Reason;
15 
16 #[cxx::bridge(namespace = "OHOS::Request")]
17 mod ffi {
18     #[derive(Clone, Copy, PartialEq, Debug)]
19     #[repr(u8)]
20     pub(crate) enum Reason {
21         Default = 0,
22         TaskSurvivalOneMonth,
23         RunningTaskMeetLimits = 4,
24         UserOperation,
25         AppBackgroundOrTerminate,
26         NetworkOffline,
27         UnsupportedNetworkType,
28         BuildRequestFailed = 10,
29         GetFileSizeFailed,
30         ContinuousTaskTimeout = 12,
31         RequestError = 14,
32         UploadFileError,
33         RedirectError,
34         ProtocolError,
35         IoError,
36         UnsupportedRangeRequest,
37         OthersError,
38         AccountStopped,
39         Dns = 23,
40         Tcp,
41         Ssl,
42         InsufficientSpace,
43         NetworkApp = 27,
44         NetworkAccount = 28,
45         AppAccount = 29,
46         NetworkAppAccount = 30,
47     }
48 }
49 
50 impl From<u8> for Reason {
from(value: u8) -> Self51     fn from(value: u8) -> Self {
52         match value {
53             0 => Reason::Default,
54             1 => Reason::TaskSurvivalOneMonth,
55             4 => Reason::RunningTaskMeetLimits,
56             5 => Reason::UserOperation,
57             6 => Reason::AppBackgroundOrTerminate,
58             7 => Reason::NetworkOffline,
59             8 => Reason::UnsupportedNetworkType,
60             10 => Reason::BuildRequestFailed,
61             11 => Reason::GetFileSizeFailed,
62             12 => Reason::ContinuousTaskTimeout,
63             14 => Reason::RequestError,
64             15 => Reason::UploadFileError,
65             16 => Reason::RedirectError,
66             17 => Reason::ProtocolError,
67             18 => Reason::IoError,
68             19 => Reason::UnsupportedRangeRequest,
69             21 => Reason::AccountStopped,
70             23 => Reason::Dns,
71             24 => Reason::Tcp,
72             25 => Reason::Ssl,
73             26 => Reason::InsufficientSpace,
74             27 => Reason::NetworkApp,
75             28 => Reason::NetworkAccount,
76             29 => Reason::AppAccount,
77             30 => Reason::NetworkAppAccount,
78             _ => Reason::OthersError,
79         }
80     }
81 }
82 
83 impl Reason {
to_str(self) -> &'static str84     pub(crate) fn to_str(self) -> &'static str {
85         match self {
86             Reason::Default => "",
87             Reason::TaskSurvivalOneMonth => "The task has not been completed for a month yet",
88             Reason::RunningTaskMeetLimits => "Too many task in running state",
89             Reason::UserOperation => "User operation",
90             Reason::AppBackgroundOrTerminate => "The app is background or terminate",
91             Reason::NetworkOffline => "NetWork is offline",
92             Reason::UnsupportedNetworkType => "NetWork type not meet the task config",
93             Reason::BuildRequestFailed => "Build request error",
94             Reason::GetFileSizeFailed => "Failed because cannot get the file size from the server and the precise is setted true by user",
95             Reason::ContinuousTaskTimeout => "Continuous processing task time out",
96             Reason::RequestError => "Request error",
97             Reason::UploadFileError => "There are some files upload failed",
98             Reason::RedirectError => "Redirect error",
99             Reason::ProtocolError => "Http protocol error",
100             Reason::IoError => "Io Error",
101             Reason::UnsupportedRangeRequest => "The server is not support range request",
102             Reason::OthersError => "Some other error occured",
103             Reason::AccountStopped => "Account stopped",
104             Reason::Dns => "DNS error",
105             Reason::Tcp => "TCP error",
106             Reason::Ssl => "TSL/SSL error",
107             Reason::InsufficientSpace => "Insufficient space",
108             Reason::NetworkApp => "NetWork is offline and the app is background or terminate",
109             Reason::NetworkAccount => "NetWork is offline and the account is stopped",
110             Reason::AppAccount => "The app is background or terminate and the account is stopped",
111             Reason::NetworkAppAccount => "NetWork is offline and the app is background or terminate and the account is stopped",
112             _ => "unknown error",
113         }
114     }
115 }
116 
117 #[cfg(test)]
118 mod test {
119     use super::*;
120     #[test]
ut_enum_reason()121     fn ut_enum_reason() {
122         assert_eq!(Reason::Default.repr, 0);
123         assert_eq!(Reason::TaskSurvivalOneMonth.repr, 1);
124         assert_eq!(Reason::RunningTaskMeetLimits.repr, 4);
125         assert_eq!(Reason::UserOperation.repr, 5);
126         assert_eq!(Reason::AppBackgroundOrTerminate.repr, 6);
127         assert_eq!(Reason::NetworkOffline.repr, 7);
128         assert_eq!(Reason::UnsupportedNetworkType.repr, 8);
129         assert_eq!(Reason::BuildRequestFailed.repr, 10);
130         assert_eq!(Reason::GetFileSizeFailed.repr, 11);
131         assert_eq!(Reason::ContinuousTaskTimeout.repr, 12);
132         assert_eq!(Reason::RequestError.repr, 14);
133         assert_eq!(Reason::UploadFileError.repr, 15);
134         assert_eq!(Reason::RedirectError.repr, 16);
135         assert_eq!(Reason::ProtocolError.repr, 17);
136         assert_eq!(Reason::IoError.repr, 18);
137         assert_eq!(Reason::UnsupportedRangeRequest.repr, 19);
138         assert_eq!(Reason::OthersError.repr, 20);
139         assert_eq!(Reason::AccountStopped.repr, 21);
140         assert_eq!(Reason::Dns.repr, 23);
141         assert_eq!(Reason::Tcp.repr, 24);
142         assert_eq!(Reason::Ssl.repr, 25);
143         assert_eq!(Reason::InsufficientSpace.repr, 26);
144         assert_eq!(Reason::NetworkApp.repr, 27);
145         assert_eq!(Reason::NetworkAccount.repr, 28);
146         assert_eq!(Reason::AppAccount.repr, 29);
147         assert_eq!(Reason::NetworkAppAccount.repr, 30);
148     }
149 }
150