-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
2154 lines (1979 loc) · 73.1 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::ffi::*;
use serde::Deserialize;
mod extern_functions;
use extern_functions::*;
pub mod error_codes;
pub use error_codes::*;
mod string_utils;
use string_utils::*;
/// Represents a license meter attribute.
#[derive(Debug)]
pub struct LicenseMeterAttribute {
/// The name of the meter attribute.
pub name: String,
/// The number of allowed uses for the meter attribute.
pub allowed_uses: i64,
/// The total number of uses recorded for the meter attribute.
pub total_uses: u64,
/// The gross number of uses for the meter attribute.
pub gross_uses: u64
}
/// Represents a product version feature flag.
#[derive(Debug)]
pub struct ProductVersionFeatureFlag {
/// The name of the feature flag.
pub name: String,
/// Indicates whether the feature flag is enabled.
pub enabled: bool,
/// Additional data associated with the feature flag.
pub data: String
}
/// Represents an activation mode.
#[derive(Debug)]
pub struct ActivationMode {
/// The initial activation mode.
pub initial_mode: String,
/// The current activation mode.
pub current_mode: String
}
/// Represents a metadata
#[derive(Debug, Deserialize, Default)]
pub struct Metadata {
/// The key of the metadata.
pub key: String,
/// The value of the metadata.
pub value: String,
}
/// Represents an organization address.
#[derive(Debug, Deserialize, Default)]
pub struct OrganizationAddress {
/// The first line of the address.
#[serde(rename = "addressLine1")]
pub address_line_1: String,
/// The second line of the address.
#[serde(rename = "addressLine2")]
pub address_line_2: String,
/// The city of the address.
pub city: String,
/// The state or region of the address.
pub state: String,
/// The country of the address.
pub country: String,
/// The postal code of the address.
#[serde(rename = "postalCode")]
pub postal_code: String
}
/// Represents a user license with information about various license parameters.
#[derive(Debug, Deserialize)]
pub struct UserLicense {
/// The allowed activations count of a license.
#[serde(rename = "allowedActivations")]
pub allowed_activations: i64,
/// The allowed deactivations count of a license.
#[serde(rename = "allowedDeactivations")]
pub allowed_deactivations: i64,
/// The license key.
pub key: String,
/// The license type.
#[serde(rename = "type")]
pub license_type: String,
/// Optional metadata associated with the license.
pub metadata: Vec<Metadata>
}
/// Represents various permission flags.
#[repr(u32)]
pub enum PermissionFlags {
/// This flag indicates that the application does not require admin or root permissions to run
LA_USER = 1,
/// This flag indicates that the application must be run with admin or root permissions.
LA_SYSTEM = 2,
/// This flag is specifically designed for Windows and should be used for system-wide activations.
LA_ALL_USERS = 3,
/// This flag will store activation data in memory. Thus, requires re-activation on every start of
/// the application and should only be used in floating licenses.
LA_IN_MEMORY = 4,
}
// --------------- Setter functions ------------------------
/// Embeds the Product.dat file in the application.
///
/// This function must be called on every start of your program before any other functions are called.
///
/// If this function fails to set the product data, none of the other functions will work.
///
/// # Arguments
///
/// * `product_data` - Content of the Product.dat file which you want to embed in your application.
///
/// # Returns
///
/// Returns `Ok(())` if the product data is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_product_data(product_data: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_product_data = to_utf16(product_data);
status = unsafe { SetProductData(c_product_data.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_product_data = string_to_cstring(product_data)?;
status = unsafe { SetProductData(c_product_data.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the product id of your application.
///
/// This function must be called on every start of your program before any other functions are called, with the exception of SetProductData() function.
///
/// # Arguments
///
/// * `product_id` - A `string` value representing the unique product id of your application as mentioned
/// on the product page in the dashboard.
///
/// * `permission_flags` - Depending on your application's requirements, choose one of
/// the following values: LA_SYSTEM, LA_USER, LA_IN_MEMORY, LA_ALL_USERS.
///
/// - `LA_USER`: This flag indicates that the application does not require
/// admin or root permissions to run.
///
/// - `LA_SYSTEM`: This flag indicates that the application must be run with admin or root permissions.
///
/// - `LA_ALL_USERS`: This flag is specifically designed for Windows and should be used for system-wide activations.
///
/// - `LA_IN_MEMORY`: This flag will store activation data in memory. Thus, requires re-activation
/// on every start of the application and should only be used in floating licenses.
///
/// # Returns
///
/// Returns `Ok(())` if the data directory is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_product_id(product_id: String, permission_flags: PermissionFlags) -> Result<(), LexActivatorError> {
let status: i32;
let c_flags: c_uint = permission_flags as u32 as c_uint;
#[cfg(windows)]
{
let c_product_id = to_utf16(product_id);
status = unsafe { SetProductId(c_product_id.as_ptr(), c_flags) };
}
#[cfg(not(windows))]
{
let c_product_id = string_to_cstring(product_id)?;
status = unsafe { SetProductId(c_product_id.as_ptr(), c_flags) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// In case you want to change the default directory used by LexActivator to
/// store the activation data on Linux and macOS, this function can be used to
/// set a different directory.
///
/// # Arguments
///
/// * `data_dir` - A `string` value representing the absolute path of the directory
/// where LexActivator should store the activation data.
///
/// # Returns
///
/// Returns `Ok(())` if the data directory is set successfully. If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_data_directory(data_dir: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_data_dir = to_utf16(data_dir);
status = unsafe { SetDataDirectory(c_data_dir.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_data_dir = string_to_cstring(data_dir)?;
status = unsafe { SetDataDirectory(c_data_dir.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Enables network logs.
///
/// This function should be used for network testing only in case of network errors. By default logging is disabled.
///
/// This function generates the lexactivator-logs.log file in the same directory where the application is running.
///
/// # Arguments
///
/// * `enable` - 0 or 1 to disable or enable logging.
///
/// Returns `Ok(())` if the debug mode is enabled successfully.
pub fn set_debug_mode(enable: u32) {
let c_enable: c_uint = enable as c_uint;
unsafe { SetDebugMode(c_enable) };
}
/// Enables or disables in-memory caching for LexActivator.
///
/// This function is designed to control caching behavior to suit specific application requirements.
///
/// Caching is enabled by default to enhance performance.
///
/// Disabling caching is recommended in environments where multiple processes access the same license on a
///
/// single machine and require real-time updates to the license state.
///
/// # Arguments
///
/// * `mode` - False or True to disable or enable caching.
///
/// Returns `Ok(())` if mode is set successfully.
pub fn set_cache_mode(mode: bool) -> Result<(), LexActivatorError> {
let c_mode: c_int = if mode { 1 } else { 0 };
let status = unsafe { SetCacheMode(c_mode) };
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// In case you don't want to use the LexActivator's advanced device fingerprinting algorithm, this function can be used to set a custom device fingerprint.
///
/// # Arguments
///
/// * `device_fingerprint` - A `string` value representing the custom device fingerprint of the user's device.
///
/// # Returns
///
/// Returns `Ok(())` if the custom device fingerprint is set successfully. If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_custom_device_fingerprint(device_fingerprint: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_device_fingerprint = to_utf16(device_fingerprint);
status = unsafe { SetCustomDeviceFingerprint(c_device_fingerprint.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_device_fingerprint = string_to_cstring(device_fingerprint)?;
status = unsafe { SetCustomDeviceFingerprint(c_device_fingerprint.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the license key for activation.
///
/// # Arguments
///
/// * `license_key` - The license key string.
///
/// # Returns
///
/// Returns `Ok(())` if the license key is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_license_key(license_key: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_license_key = to_utf16(license_key);
status = unsafe { SetLicenseKey(c_license_key.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_license_key = string_to_cstring(license_key)?;
status = unsafe { SetLicenseKey(c_license_key.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the license user credentials for activation.
///
/// # Arguments
///
/// * `email` - The email associated with the user.
/// * `password` - The password for the user.
///
/// # Returns
///
/// Returns `Ok(())` if the license user credentials are set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_license_user_credential(email: String, password: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_email = to_utf16(email);
let c_password = to_utf16(password);
status = unsafe { SetLicenseUserCredential(c_email.as_ptr(), c_password.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_email = string_to_cstring(email)?;
let c_password = string_to_cstring(password)?;
status = unsafe { SetLicenseUserCredential(c_email.as_ptr(), c_password.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the license callback function.
///
/// Whenever the server sync occurs in a separate thread, and server returns the response,
/// license callback function gets invoked with the following status codes:
/// LA_OK, LA_EXPIRED, LA_SUSPENDED, LA_E_REVOKED, LA_E_ACTIVATION_NOT_FOUND, LA_E_MACHINE_FINGERPRINT
/// LA_E_AUTHENTICATION_FAILED, LA_E_COUNTRY, LA_E_INET, LA_E_SERVER,LA_E_RATE_LIMIT, LA_E_IP,
/// LA_E_RELEASE_VERSION_NOT_ALLOWED, LA_E_RELEASE_VERSION_FORMAT
///
/// # Arguments
///
/// * `callback` - The callback function to be set.
///
/// # Returns
///
/// Returns `Ok(())` if the license callback is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_license_callback(callback: CallbackType) -> Result<(), LexActivatorError> {
let status: i32;
status = unsafe { SetLicenseCallback(callback) };
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the activation lease duration.
///
/// # Arguments
///
/// * `lease_duration` - The lease duration in seconds. A value of -1 indicates unlimited lease duration.
///
/// # Returns
///
/// Returns `Ok(())` if the activation lease duration is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_activation_lease_duration(lease_duration: i64) -> Result<(), LexActivatorError> {
let c_lease_duration: c_longlong = lease_duration as c_longlong;
let status = unsafe { SetActivationLeaseDuration(c_lease_duration) };
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the activation metadata.
///
/// # Arguments
///
/// * `key` - The key of the metadata.
/// * `value` - The value of the metadata.
///
/// # Returns
///
/// Returns `Ok(())` if the activation metadata is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_activation_metadata(key: String, value: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_key = to_utf16(key);
let c_value = to_utf16(value);
status = unsafe { SetActivationMetadata(c_key.as_ptr(), c_value.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_key = string_to_cstring(key)?;
let c_value = string_to_cstring(value)?;
status = unsafe { SetActivationMetadata(c_key.as_ptr(), c_value.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the trial activation metadata.
///
/// # Arguments
///
/// * `key` - The key of the metadata.
/// * `value` - The value of the metadata.
///
/// # Returns
///
/// Returns `Ok(())` if the trial activation metadata is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_trial_activation_metadata(key: String, value: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_key = to_utf16(key);
let c_value = to_utf16(value);
status = unsafe { SetTrialActivationMetadata(c_key.as_ptr(), c_value.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_key = string_to_cstring(key)?;
let c_value = string_to_cstring(value)?;
status = unsafe { SetTrialActivationMetadata(c_key.as_ptr(), c_value.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the current app version of your application.
/// The app version appears along with the activation details in dashboard.
/// It is also used to generate app analytics.
/// # Arguments
///
/// * `app_version` - string of maximum length 256 characters.
///
/// # Returns
///
/// Returns `Ok(())` if the release version is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_app_version(app_version: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_app_version = to_utf16(app_version);
status = unsafe { SetAppVersion(c_app_version.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_app_version = string_to_cstring(app_version)?;
status = unsafe { SetAppVersion(c_app_version.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the release version.
///
/// # Arguments
///
/// * `release_version` - The release version.
///
/// # Returns
///
/// Returns `Ok(())` if the release version is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_release_version(version: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_version = to_utf16(version);
status = unsafe { SetReleaseVersion(c_version.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_version = string_to_cstring(version)?;
status = unsafe { SetReleaseVersion(c_version.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the release published date.
///
/// # Arguments
///
/// * `release_published_date` - The release published date as a UNIX timestamp.
///
/// # Returns
///
/// Returns `Ok(())` if the release published date is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_release_published_date(release_published_date: u32) -> Result<(), LexActivatorError>{
let c_release_published_date: c_uint = release_published_date as c_uint;
let status = unsafe { SetReleasePublishedDate(c_release_published_date) };
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the release platform.
///
/// # Arguments
///
/// * `platform` - The release platform.
///
/// # Returns
///
/// Returns `Ok(())` if the release platform is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_release_platform(platform: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_platform = to_utf16(platform);
status = unsafe { SetReleasePlatform(c_platform.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_platform = string_to_cstring(platform)?;
status = unsafe { SetReleasePlatform(c_platform.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the release channel e.g. stable, beta
///
/// # Arguments
///
/// * `release_channel` - The release channel.
///
/// # Returns
///
/// Returns `Ok(())` if the release channel is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_release_channel(channel: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_channel = to_utf16(channel);
status = unsafe { SetReleaseChannel(c_channel.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_channel = string_to_cstring(channel)?;
status = unsafe { SetReleaseChannel(c_channel.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the offline activation request meter attribute uses.
///
/// # Arguments
///
/// * `name` - The name of the meter attribute.
/// * `uses` - The number of uses.
///
/// # Returns
///
/// Returns `Ok(())` if the offline activation request meter attribute uses are set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_offline_activation_request_meter_attribute_uses(name: String, uses: i32) -> Result<(), LexActivatorError>{
let status: i32;
let c_uses: c_uint = uses as c_uint;
#[cfg(windows)]
{
let c_name = to_utf16(name);
status = unsafe { SetOfflineActivationRequestMeterAttributeUses(c_name.as_ptr(), c_uses) };
}
#[cfg(not(windows))]
{
let c_name = string_to_cstring(name)?;
status = unsafe { SetOfflineActivationRequestMeterAttributeUses(c_name.as_ptr(), c_uses) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the network proxy.
///
/// # Arguments
///
/// * `proxy` - The network proxy.
///
/// # Returns
///
/// Returns `Ok(())` if the network proxy is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_network_proxy(proxy: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_proxy = to_utf16(proxy);
status = unsafe { SetNetworkProxy(c_proxy.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_proxy = string_to_cstring(proxy)?;
status = unsafe { SetNetworkProxy(c_proxy.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the Cryptlex host.
///
/// # Arguments
///
/// * `host` - The Cryptlex host.
///
/// # Returns
///
/// Returns `Ok(())` if the Cryptlex host is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_cryptlex_host(host: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_host = to_utf16(host);
status = unsafe { SetCryptlexHost(c_host.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_host = string_to_cstring(host)?;
status = unsafe { SetCryptlexHost(c_host.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
/// Sets the two-factor authentication code for the user authentication.
///
/// # Arguments
///
/// * `two_factor_authentication_code` - The 2FA code.
///
/// # Returns
///
/// Returns `Ok(())` if the two_factor_authentication_code is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_two_factor_authentication_code(two_factor_authentication_code: String) -> Result<(), LexActivatorError> {
let status: i32;
#[cfg(windows)]
{
let c_two_factor_authentication_code = to_utf16(two_factor_authentication_code);
status = unsafe { SetTwoFactorAuthenticationCode(c_two_factor_authentication_code.as_ptr()) };
}
#[cfg(not(windows))]
{
let c_two_factor_authentication_code = string_to_cstring(two_factor_authentication_code)?;
status = unsafe { SetTwoFactorAuthenticationCode(c_two_factor_authentication_code.as_ptr()) };
}
if status == 0 {
Ok(())
} else {
return Err(LexActivatorError::from(status));
}
}
// ------------------- Getter Functions --------------------
pub fn get_product_metadata(key: String) -> Result<String, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256;
let product_metadata_value: String;
#[cfg(windows)]
{
let mut buffer: [u16; LENGTH] = [0; LENGTH];
let utf16_ptr = to_utf16(key);
status = unsafe { GetProductMetadata(utf16_ptr.as_ptr(), buffer.as_mut_ptr(), LENGTH as c_uint) };
product_metadata_value = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
let key_cstring: CString = string_to_cstring(key)?;
status = unsafe { GetProductMetadata(key_cstring.as_ptr(), buffer.as_mut_ptr(), LENGTH as c_uint) };
product_metadata_value = c_char_to_string(&buffer);
}
if status == 0 {
Ok(product_metadata_value)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the name of the product version.
///
/// # Returns
///
/// Returns `Ok(String)` with the name of the product version if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_product_version_name() -> Result<String, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256;
let product_version_name: String;
#[cfg(windows)]
{
let mut buffer: [u16; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionName(buffer.as_mut_ptr(), LENGTH as c_uint) };
product_version_name = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionName(buffer.as_mut_ptr(), LENGTH as c_uint) };
product_version_name = c_char_to_string(&buffer);
}
if status == 0 {
Ok(product_version_name)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the display name of the product version.
///
/// # Returns
///
/// Returns `Ok(String)` with the display name of the product version if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_product_version_display_name() -> Result<String, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256; // Set the appropriate buffer length
let product_version_display_name: String;
#[cfg(windows)]
{
let mut buffer: [u16; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionDisplayName(buffer.as_mut_ptr(), LENGTH as c_uint) };
product_version_display_name = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionDisplayName(buffer.as_mut_ptr(), LENGTH as c_uint) };
product_version_display_name = c_char_to_string(&buffer);
}
if status == 0 {
Ok(product_version_display_name)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the feature flag of a specific product version.
///
/// # Arguments
///
/// * `name` - The name of the feature flag.
///
/// # Returns
///
/// Returns `Ok(ProductVersionFeatureFlag)` with the feature flag information if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_product_version_feature_flag(name: String) -> Result<ProductVersionFeatureFlag, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256; // Set the appropriate buffer length
let feature_name: String = name.clone();
let data: String;
let mut c_enabled: c_uint = 0;
#[cfg(windows)]
{
let c_name = to_utf16(name);
let mut buffer: [u16; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionFeatureFlag(c_name.as_ptr(), &mut c_enabled, buffer.as_mut_ptr(), LENGTH as c_uint) };
data = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let c_name = string_to_cstring(name)?;
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
status = unsafe { GetProductVersionFeatureFlag(c_name.as_ptr(), &mut c_enabled, buffer.as_mut_ptr(), LENGTH as c_uint) };
data = c_char_to_string(&buffer);
}
let product_version_feature_flag = ProductVersionFeatureFlag {
name: feature_name,
enabled: u32_to_bool(c_enabled),
data: data
};
if status == 0 {
Ok(product_version_feature_flag)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the metadata associated with a license.
///
/// # Arguments
///
/// * `key` - The metadata key.
///
/// # Returns
///
/// Returns `Ok(String)` with the metadata value if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_metadata(key: String) -> Result<String, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256; // Set the appropriate buffer length
let license_metadata: String;
#[cfg(windows)]
{
let mut buffer: [u16; LENGTH] = [0; LENGTH];
let c_key = to_utf16(key);
status = unsafe { GetLicenseMetadata(c_key.as_ptr(), buffer.as_mut_ptr(), LENGTH as c_uint) };
license_metadata = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
let c_key: CString = string_to_cstring(key)?;
status = unsafe { GetLicenseMetadata(c_key.as_ptr(), buffer.as_mut_ptr(), LENGTH as c_uint) };
license_metadata = c_char_to_string(&buffer);
}
if status == 0 {
Ok(license_metadata)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the meter attribute of a license.
///
/// # Arguments
///
/// * `name` - The name of the meter attribute.
///
/// # Returns
///
/// Returns `Ok(LicenseMeterAttribute)` with the meter attribute information if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_meterattribute(name: String) -> Result<LicenseMeterAttribute, LexActivatorError> {
let status: i32;
let meter_attribute_name: String = name.clone();
let mut c_allowed_uses: c_longlong = 0;
let mut c_total_uses: c_ulonglong = 0;
let mut c_gross_uses: c_ulonglong = 0;
#[cfg(windows)]
{
let c_name = to_utf16(name);
status = unsafe { GetLicenseMeterAttribute(c_name.as_ptr(), &mut c_allowed_uses, &mut c_total_uses, &mut c_gross_uses) };
}
#[cfg(not(windows))]
{
let c_name = string_to_cstring(name)?;
status = unsafe { GetLicenseMeterAttribute(c_name.as_ptr(), &mut c_allowed_uses, &mut c_total_uses, &mut c_gross_uses) };
}
let meter_attribute = LicenseMeterAttribute {
name: meter_attribute_name,
allowed_uses: c_allowed_uses,
total_uses: c_total_uses,
gross_uses: c_gross_uses,
};
if status == 0 {
Ok(meter_attribute)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the license key.
///
/// # Returns
///
/// Returns `Ok(String)` with the license key if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_key() -> Result<String, LexActivatorError> {
let status: i32;
const LENGTH: usize = 256; // Set the appropriate buffer length
let license_key: String;
#[cfg(windows)]
{
let mut buffer: [u16; LENGTH] = [0; LENGTH];
status = unsafe { GetLicenseKey(buffer.as_mut_ptr(), LENGTH as c_uint) };
license_key = utf16_to_string(&buffer);
}
#[cfg(not(windows))]
{
let mut buffer: [c_char; LENGTH] = [0; LENGTH];
status = unsafe { GetLicenseKey(buffer.as_mut_ptr(), LENGTH as c_uint) };
license_key = c_char_to_string(&buffer);
}
if status == 0 {
Ok(license_key)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the number of allowed activations for the license.
///
/// # Returns
///
/// Returns `Ok(i64)` with the number of allowed activations if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_allowed_activations() -> Result<i64, LexActivatorError> {
let mut allowed_activations: c_longlong = 0;
let status = unsafe { GetLicenseAllowedActivations(&mut allowed_activations) };
if status == 0 {
Ok(allowed_activations)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the total number of activations for the license.
///
/// # Returns
///
/// Returns `Ok(u32)` with the total number of activations if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_total_activations() -> Result<u32, LexActivatorError> {
let mut total_activations: c_uint = 0;
let status = unsafe { GetLicenseTotalActivations(&mut total_activations) };
if status == 0 {
Ok(total_activations)
} else {
return Err(LexActivatorError::from(status));
}
}
/// Retrieves the number of allowed deactivations for the license.
///
/// # Returns
///
/// Returns `Ok(i64)` with the number of allowed deactivations if it is retrieved successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn get_license_allowed_deactivations() -> Result<i64, LexActivatorError> {
let mut allowed_deactivations: c_longlong = 0;
let status = unsafe { GetLicenseAllowedDeactivations(&mut allowed_deactivations) };