enquo-core 0.7.0.2.gb37f667-arm64-darwin → 0.7.0.5.gb281772-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf44a44c99eefe4b458f10218f74116dd93c23be3d818cb3f3cd24af939e7202
4
- data.tar.gz: 7e10d69b31ed73549bf51fc46243a4934592cb41a04d7d0218e1a477af593d0d
3
+ metadata.gz: b564bb5e80a48726d995bfea4bd8d71bac34dd2d6bf4f5d6a5a1ae5cbf6b0fd3
4
+ data.tar.gz: 8b1f74c4cbf9ab1f8c7db8563ff0155f2eff5dcea586d2bd090ab46b4aac7822
5
5
  SHA512:
6
- metadata.gz: 83cdb79a3bd8762622c6f14f9d8a5e2fe87f83467f4ae9025c5fd62e7414b821d22593f60d793e2452087cc53d3c57cff0bd05547e7426e7bface9ace1bf950e
7
- data.tar.gz: 672d247dd1daa0deed654e938e0a9b0d236bb0e824231db115c350b33c9f412df8b93b0608d7db004f770473ef1aa3c5bee6c20877ac6c6bedc6c9bfabe11d42
6
+ metadata.gz: 7bba5236716eab5272b53d55224ffb76988b45f106a694a63009fbb1d454553c2944a00c9614fd1cde97bc4700a977983a583843974700f68b62b9c241bc128d
7
+ data.tar.gz: 737b6f8ef5b3dbe58d28e491647f68b17b4dc582b51fc9ca2ba899a7dfd3d80acabf059c4e576335b96994cc1c01c9bb90c06d392930a6fbd04439a39452d825
data/ext/enquo/Cargo.lock CHANGED
@@ -56,26 +56,6 @@ dependencies = [
56
56
  "libc",
57
57
  ]
58
58
 
59
- [[package]]
60
- name = "ansi_term"
61
- version = "0.12.1"
62
- source = "registry+https://github.com/rust-lang/crates.io-index"
63
- checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
64
- dependencies = [
65
- "winapi",
66
- ]
67
-
68
- [[package]]
69
- name = "atty"
70
- version = "0.2.14"
71
- source = "registry+https://github.com/rust-lang/crates.io-index"
72
- checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
73
- dependencies = [
74
- "hermit-abi",
75
- "libc",
76
- "winapi",
77
- ]
78
-
79
59
  [[package]]
80
60
  name = "autocfg"
81
61
  version = "1.1.0"
@@ -90,25 +70,21 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
90
70
 
91
71
  [[package]]
92
72
  name = "bindgen"
93
- version = "0.59.2"
73
+ version = "0.60.1"
94
74
  source = "registry+https://github.com/rust-lang/crates.io-index"
95
- checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8"
75
+ checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6"
96
76
  dependencies = [
97
77
  "bitflags",
98
78
  "cexpr",
99
79
  "clang-sys",
100
- "clap",
101
- "env_logger",
102
80
  "lazy_static",
103
81
  "lazycell",
104
- "log",
105
82
  "peeking_take_while",
106
83
  "proc-macro2",
107
84
  "quote",
108
85
  "regex",
109
86
  "rustc-hash",
110
87
  "shlex",
111
- "which",
112
88
  ]
113
89
 
114
90
  [[package]]
@@ -214,21 +190,6 @@ dependencies = [
214
190
  "libloading",
215
191
  ]
216
192
 
217
- [[package]]
218
- name = "clap"
219
- version = "2.34.0"
220
- source = "registry+https://github.com/rust-lang/crates.io-index"
221
- checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
222
- dependencies = [
223
- "ansi_term",
224
- "atty",
225
- "bitflags",
226
- "strsim 0.8.0",
227
- "textwrap",
228
- "unicode-width",
229
- "vec_map",
230
- ]
231
-
232
193
  [[package]]
233
194
  name = "cmac"
234
195
  version = "0.7.1"
@@ -366,7 +327,7 @@ dependencies = [
366
327
  "ident_case",
367
328
  "proc-macro2",
368
329
  "quote",
369
- "strsim 0.10.0",
330
+ "strsim",
370
331
  "syn",
371
332
  ]
372
333
 
@@ -401,20 +362,14 @@ dependencies = [
401
362
  "subtle",
402
363
  ]
403
364
 
404
- [[package]]
405
- name = "either"
406
- version = "1.8.0"
407
- source = "registry+https://github.com/rust-lang/crates.io-index"
408
- checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
409
-
410
365
  [[package]]
411
366
  name = "enquo"
412
367
  version = "0.0.0"
413
368
  dependencies = [
414
369
  "enquo-core",
370
+ "hex",
415
371
  "lazy_static",
416
- "rb-sys",
417
- "rutie",
372
+ "magnus",
418
373
  "serde_json",
419
374
  ]
420
375
 
@@ -435,19 +390,6 @@ dependencies = [
435
390
  "unicode-normalization",
436
391
  ]
437
392
 
438
- [[package]]
439
- name = "env_logger"
440
- version = "0.9.0"
441
- source = "registry+https://github.com/rust-lang/crates.io-index"
442
- checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
443
- dependencies = [
444
- "atty",
445
- "humantime",
446
- "log",
447
- "regex",
448
- "termcolor",
449
- ]
450
-
451
393
  [[package]]
452
394
  name = "fnv"
453
395
  version = "1.0.7"
@@ -493,15 +435,6 @@ version = "0.12.3"
493
435
  source = "registry+https://github.com/rust-lang/crates.io-index"
494
436
  checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
495
437
 
496
- [[package]]
497
- name = "hermit-abi"
498
- version = "0.1.19"
499
- source = "registry+https://github.com/rust-lang/crates.io-index"
500
- checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
501
- dependencies = [
502
- "libc",
503
- ]
504
-
505
438
  [[package]]
506
439
  name = "hex"
507
440
  version = "0.4.3"
@@ -517,12 +450,6 @@ dependencies = [
517
450
  "digest",
518
451
  ]
519
452
 
520
- [[package]]
521
- name = "humantime"
522
- version = "2.1.0"
523
- source = "registry+https://github.com/rust-lang/crates.io-index"
524
- checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
525
-
526
453
  [[package]]
527
454
  name = "iana-time-zone"
528
455
  version = "0.1.54"
@@ -634,6 +561,28 @@ dependencies = [
634
561
  "cfg-if",
635
562
  ]
636
563
 
564
+ [[package]]
565
+ name = "magnus"
566
+ version = "0.5.3"
567
+ source = "registry+https://github.com/rust-lang/crates.io-index"
568
+ checksum = "c8dc14463c2552e753ef562961f486ca76f17a857c121db40e9f3ade3f35ab81"
569
+ dependencies = [
570
+ "magnus-macros",
571
+ "rb-sys",
572
+ "rb-sys-env",
573
+ ]
574
+
575
+ [[package]]
576
+ name = "magnus-macros"
577
+ version = "0.4.1"
578
+ source = "registry+https://github.com/rust-lang/crates.io-index"
579
+ checksum = "6cc17af1d45442c011aa579d727ec6cff8a69aea8a6bbad26736e7112d749bfb"
580
+ dependencies = [
581
+ "proc-macro2",
582
+ "quote",
583
+ "syn",
584
+ ]
585
+
637
586
  [[package]]
638
587
  name = "memchr"
639
588
  version = "2.5.0"
@@ -750,12 +699,6 @@ version = "0.1.2"
750
699
  source = "registry+https://github.com/rust-lang/crates.io-index"
751
700
  checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
752
701
 
753
- [[package]]
754
- name = "pkg-config"
755
- version = "0.3.25"
756
- source = "registry+https://github.com/rust-lang/crates.io-index"
757
- checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
758
-
759
702
  [[package]]
760
703
  name = "polyval"
761
704
  version = "0.6.0"
@@ -776,18 +719,18 @@ checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
776
719
 
777
720
  [[package]]
778
721
  name = "proc-macro2"
779
- version = "1.0.43"
722
+ version = "1.0.56"
780
723
  source = "registry+https://github.com/rust-lang/crates.io-index"
781
- checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
724
+ checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
782
725
  dependencies = [
783
726
  "unicode-ident",
784
727
  ]
785
728
 
786
729
  [[package]]
787
730
  name = "quote"
788
- version = "1.0.21"
731
+ version = "1.0.26"
789
732
  source = "registry+https://github.com/rust-lang/crates.io-index"
790
- checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
733
+ checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
791
734
  dependencies = [
792
735
  "proc-macro2",
793
736
  ]
@@ -824,15 +767,34 @@ dependencies = [
824
767
 
825
768
  [[package]]
826
769
  name = "rb-sys"
827
- version = "0.8.1"
770
+ version = "0.9.72"
771
+ source = "registry+https://github.com/rust-lang/crates.io-index"
772
+ checksum = "3e36bdb8be5f395264fb4345a5f6c13dac307ed3be3bccf6305b57835981c605"
773
+ dependencies = [
774
+ "rb-sys-build",
775
+ ]
776
+
777
+ [[package]]
778
+ name = "rb-sys-build"
779
+ version = "0.9.72"
828
780
  source = "registry+https://github.com/rust-lang/crates.io-index"
829
- checksum = "4db52af35291b64ce86f23f755b4576c20e3063b517269252b2f8cc883ca00e6"
781
+ checksum = "b56f8993adac385ed6208f0dc62f99181eb0676dea50bac7bc3d36a86bb9429b"
830
782
  dependencies = [
831
783
  "bindgen",
832
- "libc",
833
- "pkg-config",
784
+ "lazy_static",
785
+ "proc-macro2",
786
+ "quote",
787
+ "regex",
788
+ "shell-words",
789
+ "syn",
834
790
  ]
835
791
 
792
+ [[package]]
793
+ name = "rb-sys-env"
794
+ version = "0.1.2"
795
+ source = "registry+https://github.com/rust-lang/crates.io-index"
796
+ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
797
+
836
798
  [[package]]
837
799
  name = "regex"
838
800
  version = "1.6.0"
@@ -856,16 +818,6 @@ version = "1.1.0"
856
818
  source = "registry+https://github.com/rust-lang/crates.io-index"
857
819
  checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
858
820
 
859
- [[package]]
860
- name = "rutie"
861
- version = "0.8.4"
862
- source = "git+https://github.com/mpalmer/rutie?branch=fixes#b798b15ddfc3ed011de72702bd0124c91fd67c78"
863
- dependencies = [
864
- "lazy_static",
865
- "libc",
866
- "rb-sys",
867
- ]
868
-
869
821
  [[package]]
870
822
  name = "ryu"
871
823
  version = "1.0.11"
@@ -958,16 +910,16 @@ dependencies = [
958
910
  ]
959
911
 
960
912
  [[package]]
961
- name = "shlex"
913
+ name = "shell-words"
962
914
  version = "1.1.0"
963
915
  source = "registry+https://github.com/rust-lang/crates.io-index"
964
- checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
916
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
965
917
 
966
918
  [[package]]
967
- name = "strsim"
968
- version = "0.8.0"
919
+ name = "shlex"
920
+ version = "1.1.0"
969
921
  source = "registry+https://github.com/rust-lang/crates.io-index"
970
- checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
922
+ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
971
923
 
972
924
  [[package]]
973
925
  name = "strsim"
@@ -1013,15 +965,6 @@ dependencies = [
1013
965
  "winapi-util",
1014
966
  ]
1015
967
 
1016
- [[package]]
1017
- name = "textwrap"
1018
- version = "0.11.0"
1019
- source = "registry+https://github.com/rust-lang/crates.io-index"
1020
- checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
1021
- dependencies = [
1022
- "unicode-width",
1023
- ]
1024
-
1025
968
  [[package]]
1026
969
  name = "thiserror"
1027
970
  version = "1.0.34"
@@ -1127,12 +1070,6 @@ dependencies = [
1127
1070
  "subtle",
1128
1071
  ]
1129
1072
 
1130
- [[package]]
1131
- name = "vec_map"
1132
- version = "0.8.2"
1133
- source = "registry+https://github.com/rust-lang/crates.io-index"
1134
- checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
1135
-
1136
1073
  [[package]]
1137
1074
  name = "version_check"
1138
1075
  version = "0.9.4"
@@ -1199,17 +1136,6 @@ version = "0.2.84"
1199
1136
  source = "registry+https://github.com/rust-lang/crates.io-index"
1200
1137
  checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
1201
1138
 
1202
- [[package]]
1203
- name = "which"
1204
- version = "4.3.0"
1205
- source = "registry+https://github.com/rust-lang/crates.io-index"
1206
- checksum = "1c831fbbee9e129a8cf93e7747a82da9d95ba8e16621cae60ec2cdc849bacb7b"
1207
- dependencies = [
1208
- "either",
1209
- "libc",
1210
- "once_cell",
1211
- ]
1212
-
1213
1139
  [[package]]
1214
1140
  name = "winapi"
1215
1141
  version = "0.3.9"
data/ext/enquo/Cargo.toml CHANGED
@@ -7,8 +7,8 @@ edition = "2021"
7
7
  enquo-core = { path = "../../../rust" }
8
8
  lazy_static = "1.0"
9
9
  serde_json = "1.0"
10
- rb-sys = "0.8"
11
- rutie = { git = "https://github.com/mpalmer/rutie", branch = "fixes" }
10
+ magnus = "0.5"
11
+ hex = "0.4"
12
12
 
13
13
  [lib]
14
14
  crate-type = ["cdylib"]
@@ -0,0 +1,342 @@
1
+ use enquo_core::{Boolean, Date, Error, Text, I64};
2
+ use magnus::{
3
+ class, eval, exception, function, method,
4
+ prelude::*,
5
+ scan_args::{get_kwargs, scan_args},
6
+ RClass, RModule, RString, TryConvert,
7
+ };
8
+ use std::ops::Deref;
9
+
10
+ use crate::maybe_raise;
11
+
12
+ #[magnus::wrap(class = "Enquo::Field")]
13
+ pub struct Field(pub enquo_core::Field);
14
+
15
+ impl Deref for Field {
16
+ type Target = enquo_core::Field;
17
+
18
+ fn deref(&self) -> &Self::Target {
19
+ &self.0
20
+ }
21
+ }
22
+
23
+ struct EncryptOpts<T>
24
+ where
25
+ T: TryConvert,
26
+ {
27
+ input: T,
28
+ context: Vec<u8>,
29
+ unsafe_ok: bool,
30
+ no_query: bool,
31
+ order_prefix_length: Option<u8>,
32
+ }
33
+
34
+ fn strict_bool(value: Option<magnus::Value>, e: &str) -> Result<Option<bool>, magnus::Error> {
35
+ match value {
36
+ None => Ok(None),
37
+ Some(v) => {
38
+ if v.is_kind_of(class::true_class()) {
39
+ Ok(Some(true))
40
+ } else if v.is_kind_of(class::false_class()) {
41
+ Ok(Some(false))
42
+ } else {
43
+ Err(magnus::Error::new(
44
+ exception::type_error(),
45
+ format!("{e} (got an instance of {})", v.class().inspect()),
46
+ ))
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ fn strict_int<T: TryConvert>(
53
+ value: Option<magnus::Value>,
54
+ e: &str,
55
+ ) -> Result<Option<T>, magnus::Error> {
56
+ match value {
57
+ None => Ok(None),
58
+ Some(v) => {
59
+ if v.is_kind_of(class::integer()) {
60
+ Ok(Some(v.try_convert::<T>()?))
61
+ } else {
62
+ Err(magnus::Error::new(
63
+ exception::type_error(),
64
+ format!("{e} (got an instance of {})", v.class().inspect()),
65
+ ))
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ fn parse_encrypt_args<T>(args: &[magnus::Value]) -> Result<EncryptOpts<T>, magnus::Error>
72
+ where
73
+ T: TryConvert,
74
+ {
75
+ let args = scan_args::<_, (), (), (), _, ()>(args)?;
76
+ let (input, context_str): (T, RString) = args.required;
77
+
78
+ let kwargs = get_kwargs::<_, (), _, ()>(
79
+ args.keywords,
80
+ &[],
81
+ &["unsafe", "no_query", "order_prefix_length"],
82
+ )?;
83
+
84
+ let (unsafe_ok_val, no_query_val, order_prefix_length_val): (
85
+ Option<magnus::Value>,
86
+ Option<magnus::Value>,
87
+ Option<magnus::Value>,
88
+ ) = kwargs.optional;
89
+
90
+ let unsafe_ok = strict_bool(unsafe_ok_val, "unsafe can only accept booleans")?.unwrap_or(false);
91
+ let no_query = strict_bool(no_query_val, "no_query can only accept booleans")?.unwrap_or(false);
92
+ let order_prefix_length = strict_int(
93
+ order_prefix_length_val,
94
+ "order_prefix_length must be an Integer",
95
+ )?;
96
+
97
+ Ok(EncryptOpts::<T> {
98
+ input,
99
+ // Safe because we immediately copy away the value
100
+ context: unsafe { context_str.as_slice().to_vec() },
101
+ unsafe_ok,
102
+ no_query,
103
+ order_prefix_length,
104
+ })
105
+ }
106
+
107
+ impl Field {
108
+ fn key_id(&self) -> Result<String, magnus::Error> {
109
+ maybe_raise(self.0.key_id().map(hex::encode), None)
110
+ }
111
+
112
+ fn encrypt_boolean(&self, args: &[magnus::Value]) -> Result<String, magnus::Error> {
113
+ let opts = parse_encrypt_args::<magnus::Value>(args)?;
114
+
115
+ let b = strict_bool(
116
+ Some(opts.input),
117
+ "Enquo::Field#encrypt_boolean can only encrypt booleans",
118
+ )?
119
+ .expect("CAN'T HAPPEN: got None from strict_bool(Some(opts.input)) !!!");
120
+
121
+ let mut res = maybe_raise(
122
+ if opts.unsafe_ok {
123
+ Boolean::new_with_unsafe_parts(b, &opts.context, self)
124
+ } else {
125
+ Boolean::new(b, &opts.context, self)
126
+ },
127
+ None,
128
+ )?;
129
+
130
+ if opts.no_query {
131
+ maybe_raise(res.make_unqueryable(), None)?;
132
+ }
133
+
134
+ maybe_raise(
135
+ serde_json::to_string(&res),
136
+ Some("failed to encode ciphertext"),
137
+ )
138
+ }
139
+
140
+ fn decrypt_boolean(&self, ciphertext: String, context: String) -> Result<bool, magnus::Error> {
141
+ let ct: Boolean = maybe_raise(
142
+ serde_json::from_str(&ciphertext),
143
+ Some("failed to decode ciphertext"),
144
+ )?;
145
+
146
+ maybe_raise(ct.decrypt(context.as_bytes(), self), None)
147
+ }
148
+
149
+ fn encrypt_i64(&self, args: &[magnus::Value]) -> Result<String, magnus::Error> {
150
+ let opts = parse_encrypt_args::<magnus::Value>(args)?;
151
+
152
+ // Yes, I am aware that Magnus will turn anything that responds to
153
+ // `#to_int` into an i64, but that includes instances of Float, and...
154
+ // well, nope.
155
+ let i: i64 = if opts.input.is_kind_of(class::integer()) {
156
+ opts.input.try_convert()
157
+ } else {
158
+ Err(magnus::Error::new(
159
+ exception::type_error(),
160
+ format!(
161
+ "Enquo::Field#encrypt_i64 can only encrypt Integers (got an instance of {})",
162
+ opts.input.class().inspect()
163
+ ),
164
+ ))
165
+ }?;
166
+
167
+ let mut res = maybe_raise(
168
+ if opts.unsafe_ok {
169
+ I64::new_with_unsafe_parts(i, &opts.context, self)
170
+ } else {
171
+ I64::new(i, &opts.context, self)
172
+ },
173
+ None,
174
+ )?;
175
+
176
+ if opts.no_query {
177
+ maybe_raise(res.make_unqueryable(), None)?;
178
+ }
179
+
180
+ maybe_raise(
181
+ serde_json::to_string(&res),
182
+ Some("failed to encode ciphertext"),
183
+ )
184
+ }
185
+
186
+ fn decrypt_i64(&self, ciphertext: String, context: String) -> Result<i64, magnus::Error> {
187
+ let ct: I64 = maybe_raise(
188
+ serde_json::from_str(&ciphertext),
189
+ Some("failed to decode ciphertext"),
190
+ )?;
191
+
192
+ maybe_raise(ct.decrypt(context.as_bytes(), self), None)
193
+ }
194
+
195
+ fn encrypt_date(&self, args: &[magnus::Value]) -> Result<String, magnus::Error> {
196
+ let opts = parse_encrypt_args::<magnus::Value>(args)?;
197
+
198
+ // Safe as we're not storing the result of classname
199
+ if opts.input.class().inspect() != "Date" {
200
+ return Err(magnus::Error::new(
201
+ exception::type_error(),
202
+ format!(
203
+ "Enquo::Field#encrypt_date can only encrypt Date objects (got instance of {})",
204
+ opts.input.class().inspect()
205
+ ),
206
+ ));
207
+ }
208
+
209
+ let y: i16 = opts.input.funcall("year", ())?;
210
+ let m: u8 = opts.input.funcall("month", ())?;
211
+ let d: u8 = opts.input.funcall("day", ())?;
212
+
213
+ let mut res = maybe_raise(
214
+ if opts.unsafe_ok {
215
+ Date::new_with_unsafe_parts((y, m, d), &opts.context, self)
216
+ } else {
217
+ Date::new((y, m, d), &opts.context, self)
218
+ },
219
+ None,
220
+ )?;
221
+
222
+ if opts.no_query {
223
+ maybe_raise(res.make_unqueryable(), None)?;
224
+ }
225
+
226
+ maybe_raise(
227
+ serde_json::to_string(&res),
228
+ Some("failed to encode ciphertext"),
229
+ )
230
+ }
231
+
232
+ fn decrypt_date(
233
+ &self,
234
+ ciphertext: String,
235
+ context: String,
236
+ ) -> Result<magnus::Value, magnus::Error> {
237
+ let ct: Date = maybe_raise(
238
+ serde_json::from_str(&ciphertext),
239
+ Some("failed to decode ciphertext"),
240
+ )?;
241
+
242
+ let (y, m, d) = maybe_raise(ct.decrypt(context.as_bytes(), self), None)?;
243
+
244
+ let date_class = maybe_raise(
245
+ RClass::from_value(eval("::Date")?).ok_or_else(|| {
246
+ Error::OperationError("failed to get RClass from Date value".to_string())
247
+ }),
248
+ None,
249
+ )?;
250
+
251
+ date_class.new_instance((y, m, d))
252
+ }
253
+
254
+ fn encrypt_text(&self, args: &[magnus::Value]) -> Result<String, magnus::Error> {
255
+ let opts = parse_encrypt_args::<magnus::Value>(args)?;
256
+
257
+ // Yes, I am aware that Magnus will turn anything that responds to
258
+ // `#to_s` into a String if we ask it to, but we want to be far more strict,
259
+ // and only allow actual Ruby String objects to be passed in.
260
+ let t: String = if opts.input.is_kind_of(class::string()) {
261
+ opts.input.try_convert::<RString>()?.to_string()
262
+ } else {
263
+ Err(magnus::Error::new(
264
+ exception::type_error(),
265
+ format!(
266
+ "Enquo::Field#encrypt_text can only encrypt Strings (got an instance of {})",
267
+ opts.input.class().inspect()
268
+ ),
269
+ ))
270
+ }?;
271
+
272
+ let mut res = maybe_raise(
273
+ if opts.unsafe_ok {
274
+ Text::new_with_unsafe_parts(&t, &opts.context, self, opts.order_prefix_length)
275
+ } else if opts.order_prefix_length.is_some() {
276
+ return Err(magnus::Error::new(exception::arg_error(), "Cannot specify an order_prefix_length unless reduced_security_operations is set"));
277
+ } else {
278
+ Text::new(&t, &opts.context, self)
279
+ },
280
+ None,
281
+ )?;
282
+
283
+ if opts.no_query {
284
+ maybe_raise(res.make_unqueryable(), None)?;
285
+ }
286
+
287
+ maybe_raise(
288
+ serde_json::to_string(&res),
289
+ Some("failed to encode ciphertext"),
290
+ )
291
+ }
292
+
293
+ fn decrypt_text(&self, ciphertext: String, context: String) -> Result<String, magnus::Error> {
294
+ let ct: Text = maybe_raise(
295
+ serde_json::from_str(&ciphertext),
296
+ Some("failed to decode ciphertext"),
297
+ )?;
298
+
299
+ maybe_raise(ct.decrypt(context.as_bytes(), self), None)
300
+ }
301
+
302
+ fn encrypt_text_length_query(&self, len: u32) -> Result<String, magnus::Error> {
303
+ let value_set = maybe_raise(Text::query_length(len, self), None)?;
304
+ maybe_raise(
305
+ serde_json::to_string(&value_set),
306
+ Some("failed to encode value set"),
307
+ )
308
+ }
309
+ }
310
+
311
+ pub fn init(base: &RModule) -> Result<(), magnus::Error> {
312
+ let class = base.define_class("Field", class::object())?;
313
+
314
+ class.define_singleton_method(
315
+ "new",
316
+ function!(
317
+ || -> Result<(), _> {
318
+ Err(magnus::Error::new(
319
+ exception::no_method_error(),
320
+ "Enquo::Field.new should not be called directly; use Enquo::Root#field instead"
321
+ .to_string(),
322
+ ))
323
+ },
324
+ 0
325
+ ),
326
+ )?;
327
+ class.define_method("key_id", method!(Field::key_id, 0))?;
328
+ class.define_method("encrypt_boolean", method!(Field::encrypt_boolean, -1))?;
329
+ class.define_method("decrypt_boolean", method!(Field::decrypt_boolean, 2))?;
330
+ class.define_method("encrypt_date", method!(Field::encrypt_date, -1))?;
331
+ class.define_method("decrypt_date", method!(Field::decrypt_date, 2))?;
332
+ class.define_method("encrypt_i64", method!(Field::encrypt_i64, -1))?;
333
+ class.define_method("decrypt_i64", method!(Field::decrypt_i64, 2))?;
334
+ class.define_method("encrypt_text", method!(Field::encrypt_text, -1))?;
335
+ class.define_method("decrypt_text", method!(Field::decrypt_text, 2))?;
336
+ class.define_method(
337
+ "encrypt_text_length_query",
338
+ method!(Field::encrypt_text_length_query, 1),
339
+ )?;
340
+
341
+ Ok(())
342
+ }