1use core::ptr;
2
3use crate::error::StoreKitError;
4use crate::ffi;
5use crate::private::{error_from_status, take_string};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum AppStoreEnvironment {
10 Production,
12 Sandbox,
14 Xcode,
16 Unknown(String),
18}
19
20impl AppStoreEnvironment {
21 pub fn as_str(&self) -> &str {
23 match self {
24 Self::Production => "production",
25 Self::Sandbox => "sandbox",
26 Self::Xcode => "xcode",
27 Self::Unknown(value) => value.as_str(),
28 }
29 }
30
31 pub(crate) fn from_raw(raw: String) -> Self {
32 match raw.as_str() {
33 "production" => Self::Production,
34 "sandbox" => Self::Sandbox,
35 "xcode" => Self::Xcode,
36 _ => Self::Unknown(raw),
37 }
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum AppStorePlatform {
44 IOS,
46 MacOS,
48 TvOS,
50 VisionOS,
52 Unknown(String),
54}
55
56impl AppStorePlatform {
57 pub fn as_str(&self) -> &str {
59 match self {
60 Self::IOS => "iOS",
61 Self::MacOS => "macOS",
62 Self::TvOS => "tvOS",
63 Self::VisionOS => "visionOS",
64 Self::Unknown(value) => value.as_str(),
65 }
66 }
67
68 pub(crate) fn from_raw(raw: String) -> Self {
69 match raw.as_str() {
70 "iOS" => Self::IOS,
71 "macOS" => Self::MacOS,
72 "tvOS" => Self::TvOS,
73 "visionOS" => Self::VisionOS,
74 _ => Self::Unknown(raw),
75 }
76 }
77}
78
79#[derive(Debug, Clone, Copy, Default)]
81pub struct AppStore;
82
83impl AppStore {
84 pub fn can_make_payments() -> Result<bool, StoreKitError> {
86 let mut raw_value = 0;
87 let mut error_message = ptr::null_mut();
88 let status = unsafe {
89 ffi::sk_app_store_can_make_payments(&raw mut raw_value, &raw mut error_message)
90 };
91 if status == ffi::status::OK {
92 Ok(raw_value != 0)
93 } else {
94 Err(unsafe { error_from_status(status, error_message) })
95 }
96 }
97
98 pub fn device_verification_id() -> Result<Option<String>, StoreKitError> {
100 let mut uuid_ptr = ptr::null_mut();
101 let mut error_message = ptr::null_mut();
102 let status = unsafe {
103 ffi::sk_app_store_device_verification_id(&raw mut uuid_ptr, &raw mut error_message)
104 };
105 if status == ffi::status::OK {
106 Ok(unsafe { take_string(uuid_ptr) })
107 } else {
108 Err(unsafe { error_from_status(status, error_message) })
109 }
110 }
111
112 pub fn sync() -> Result<(), StoreKitError> {
114 let mut error_message = ptr::null_mut();
115 let status = unsafe { ffi::sk_app_store_sync(&raw mut error_message) };
116 if status == ffi::status::OK {
117 Ok(())
118 } else {
119 Err(unsafe { error_from_status(status, error_message) })
120 }
121 }
122
123 pub fn show_manage_subscriptions() -> Result<(), StoreKitError> {
125 let mut error_message = ptr::null_mut();
126 let status = unsafe { ffi::sk_app_store_show_manage_subscriptions(&raw mut error_message) };
127 if status == ffi::status::OK {
128 Ok(())
129 } else {
130 Err(unsafe { error_from_status(status, error_message) })
131 }
132 }
133
134 pub fn request_review() -> Result<(), StoreKitError> {
136 let mut error_message = ptr::null_mut();
137 let status = unsafe { ffi::sk_app_store_request_review(&raw mut error_message) };
138 if status == ffi::status::OK {
139 Ok(())
140 } else {
141 Err(unsafe { error_from_status(status, error_message) })
142 }
143 }
144
145 pub fn present_offer_code_redeem_sheet() -> Result<(), StoreKitError> {
147 let mut error_message = ptr::null_mut();
148 let status =
149 unsafe { ffi::sk_app_store_present_offer_code_redeem_sheet(&raw mut error_message) };
150 if status == ffi::status::OK {
151 Ok(())
152 } else {
153 Err(unsafe { error_from_status(status, error_message) })
154 }
155 }
156}