field_struct-avro-native 1.0.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ //! Avro binary encoding of datums for field_struct-avro, from a plan compiled once per writer schema.
2
+ //!
3
+ //! The encoder only writes what it is sure the pure-Ruby path (the avro gem's `DatumWriter`) would write
4
+ //! byte for byte. Anything else makes `Plan#encode` return `nil`, and the Ruby side runs the pure path,
5
+ //! which then succeeds or raises its own error.
6
+
7
+ mod plan;
8
+ mod write;
9
+
10
+ use magnus::{function, method, prelude::*, Error, RArray, RString, Ruby, Value};
11
+
12
+ use plan::Plan;
13
+
14
+ #[magnus::wrap(class = "FieldStruct::Avro::Native::Plan", free_immediately, size)]
15
+ struct RbPlan(Plan);
16
+
17
+ impl RbPlan {
18
+ fn new(ruby: &Ruby, nodes: RArray, root: usize) -> Result<Self, Error> {
19
+ Plan::from_ruby(nodes, root)
20
+ .map(RbPlan)
21
+ .map_err(|message| Error::new(ruby.exception_arg_error(), message))
22
+ }
23
+
24
+ fn encode(ruby: &Ruby, rb_self: &Self, datum: Value) -> Option<RString> {
25
+ let mut out = Vec::with_capacity(256);
26
+ write::Writer::new(ruby, &rb_self.0)
27
+ .datum(rb_self.0.root(), datum, &mut out)
28
+ .ok()?;
29
+ Some(ruby.str_from_slice(&out))
30
+ }
31
+
32
+ fn size(&self) -> usize {
33
+ self.0.len()
34
+ }
35
+ }
36
+
37
+ #[magnus::init]
38
+ fn init(ruby: &Ruby) -> Result<(), Error> {
39
+ let field_struct = ruby.define_module("FieldStruct")?;
40
+ let avro = field_struct.define_module("Avro")?;
41
+ let native = avro.define_module("Native")?;
42
+ let class = native.define_class("Plan", ruby.class_object())?;
43
+ class.define_singleton_method("new", function!(RbPlan::new, 2))?;
44
+ class.define_method("encode", method!(RbPlan::encode, 1))?;
45
+ class.define_method("size", method!(RbPlan::size, 0))?;
46
+ Ok(())
47
+ }
@@ -0,0 +1,157 @@
1
+ //! The encoding plan: a flat list of nodes, children referenced by index so recursive records need no cycles.
2
+ //!
3
+ //! The Ruby side (`FieldStruct::Avro::Native::Compiler`) builds the description from a parsed `Avro::Schema`;
4
+ //! each node is an Array whose first element is a tag.
5
+
6
+ use magnus::{RArray, RString, Value};
7
+
8
+ pub const NULL: i64 = 0;
9
+ pub const BOOLEAN: i64 = 1;
10
+ pub const INT: i64 = 2;
11
+ pub const LONG: i64 = 3;
12
+ pub const FLOAT: i64 = 4;
13
+ pub const DOUBLE: i64 = 5;
14
+ pub const BYTES: i64 = 6;
15
+ pub const STRING: i64 = 7;
16
+ pub const DECIMAL: i64 = 8;
17
+ pub const DATE: i64 = 9;
18
+ pub const TIMESTAMP: i64 = 10;
19
+ pub const ENUM: i64 = 11;
20
+ pub const ARRAY: i64 = 12;
21
+ pub const MAP: i64 = 13;
22
+ pub const RECORD: i64 = 14;
23
+ pub const UNION: i64 = 15;
24
+
25
+ #[derive(Debug)]
26
+ pub enum Node {
27
+ Null,
28
+ Boolean,
29
+ Int,
30
+ Long,
31
+ Float,
32
+ Double,
33
+ Bytes,
34
+ Str,
35
+ /// Unscaled digits must stay below `10**precision`.
36
+ Decimal { precision: u32, scale: u32 },
37
+ Date,
38
+ /// Ticks per second: 1_000 (millis), 1_000_000 (micros) or 1_000_000_000 (nanos).
39
+ Timestamp { per_second: i64 },
40
+ Enum { symbols: Vec<Vec<u8>> },
41
+ Array { items: usize },
42
+ Map { values: usize },
43
+ Record { fields: Vec<(Vec<u8>, usize)> },
44
+ Union { members: Vec<usize> },
45
+ }
46
+
47
+ #[derive(Debug)]
48
+ pub struct Plan {
49
+ nodes: Vec<Node>,
50
+ root: usize,
51
+ }
52
+
53
+ impl Plan {
54
+ pub fn root(&self) -> usize {
55
+ self.root
56
+ }
57
+
58
+ pub fn len(&self) -> usize {
59
+ self.nodes.len()
60
+ }
61
+
62
+ pub fn node(&self, index: usize) -> &Node {
63
+ &self.nodes[index]
64
+ }
65
+
66
+ pub fn from_ruby(list: RArray, root: usize) -> Result<Self, String> {
67
+ let mut nodes = Vec::with_capacity(list.len());
68
+ for i in 0..list.len() {
69
+ let item: RArray = list.entry(i as isize).map_err(|e| format!("node {i}: {e}"))?;
70
+ nodes.push(node(item).map_err(|e| format!("node {i}: {e}"))?);
71
+ }
72
+ let plan = Plan { nodes, root };
73
+ plan.check()?;
74
+ Ok(plan)
75
+ }
76
+
77
+ fn check(&self) -> Result<(), String> {
78
+ let n = self.nodes.len();
79
+ let ok = |i: usize| if i < n { Ok(()) } else { Err(format!("child index {i} out of range")) };
80
+ ok(self.root)?;
81
+ for node in &self.nodes {
82
+ match node {
83
+ Node::Array { items } => ok(*items)?,
84
+ Node::Map { values } => ok(*values)?,
85
+ Node::Record { fields } => fields.iter().try_for_each(|(_, i)| ok(*i))?,
86
+ Node::Union { members } => {
87
+ members.iter().try_for_each(|i| ok(*i))?;
88
+ if members.iter().any(|i| matches!(self.nodes[*i], Node::Union { .. })) {
89
+ return Err("a union inside a union".into());
90
+ }
91
+ }
92
+ Node::Decimal { precision, .. } if *precision == 0 => return Err("decimal precision 0".into()),
93
+ _ => {}
94
+ }
95
+ }
96
+ Ok(())
97
+ }
98
+ }
99
+
100
+ fn at<T: magnus::TryConvert>(item: RArray, i: isize) -> Result<T, String> {
101
+ item.entry(i).map_err(|e| e.to_string())
102
+ }
103
+
104
+ fn bytes_of(value: Value) -> Result<Vec<u8>, String> {
105
+ let s = RString::from_value(value).ok_or("expected a String")?;
106
+ // SAFETY: the slice is copied before any Ruby code runs.
107
+ Ok(unsafe { s.as_slice() }.to_vec())
108
+ }
109
+
110
+ fn node(item: RArray) -> Result<Node, String> {
111
+ let tag: i64 = at(item, 0)?;
112
+ Ok(match tag {
113
+ NULL => Node::Null,
114
+ BOOLEAN => Node::Boolean,
115
+ INT => Node::Int,
116
+ LONG => Node::Long,
117
+ FLOAT => Node::Float,
118
+ DOUBLE => Node::Double,
119
+ BYTES => Node::Bytes,
120
+ STRING => Node::Str,
121
+ DECIMAL => Node::Decimal { precision: at(item, 1)?, scale: at(item, 2)? },
122
+ DATE => Node::Date,
123
+ TIMESTAMP => {
124
+ let per_second: i64 = at(item, 1)?;
125
+ if ![1_000, 1_000_000, 1_000_000_000].contains(&per_second) {
126
+ return Err(format!("timestamp unit {per_second}"));
127
+ }
128
+ Node::Timestamp { per_second }
129
+ }
130
+ ENUM => {
131
+ let list: RArray = at(item, 1)?;
132
+ let symbols = (0..list.len())
133
+ .map(|i| list.entry::<Value>(i as isize).map_err(|e| e.to_string()).and_then(bytes_of))
134
+ .collect::<Result<_, _>>()?;
135
+ Node::Enum { symbols }
136
+ }
137
+ ARRAY => Node::Array { items: at(item, 1)? },
138
+ MAP => Node::Map { values: at(item, 1)? },
139
+ RECORD => {
140
+ let list: RArray = at(item, 1)?;
141
+ let mut fields = Vec::with_capacity(list.len());
142
+ for i in 0..list.len() {
143
+ let pair: RArray = list.entry(i as isize).map_err(|e| e.to_string())?;
144
+ fields.push((bytes_of(at(pair, 0)?)?, at(pair, 1)?));
145
+ }
146
+ Node::Record { fields }
147
+ }
148
+ UNION => {
149
+ let list: RArray = at(item, 1)?;
150
+ let members = (0..list.len())
151
+ .map(|i| list.entry::<usize>(i as isize).map_err(|e| e.to_string()))
152
+ .collect::<Result<_, _>>()?;
153
+ Node::Union { members }
154
+ }
155
+ other => return Err(format!("unknown tag {other}")),
156
+ })
157
+ }
@@ -0,0 +1,435 @@
1
+ //! Walks a Ruby datum against a plan and appends the Avro binary encoding to a buffer.
2
+ //!
3
+ //! Every failure is one of two kinds. `Mismatch`: the avro gem's validator certainly rejects this value for this
4
+ //! schema, so inside a union the next member can be tried. `Unsure`: anything else (a value this encoder does not
5
+ //! handle, or one whose outcome in the pure path it cannot predict); the whole encode is abandoned and the Ruby side
6
+ //! runs the pure path.
7
+
8
+ use magnus::{
9
+ encoding::{Coderange, EncodingCapable, Index},
10
+ prelude::*,
11
+ r_hash::ForEach,
12
+ rb_sys::AsRawValue,
13
+ value::{Lazy, ReprValue},
14
+ Float, Integer, RArray, RClass, RHash, RString, Ruby, Symbol, Value,
15
+ };
16
+
17
+ use field_struct_avro_kernel::{decimal, write_bytes, write_long};
18
+ use crate::plan::{Node, Plan};
19
+
20
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
21
+ pub enum Fail {
22
+ Mismatch,
23
+ Unsure,
24
+ }
25
+
26
+ type Step = Result<(), Fail>;
27
+
28
+ static BIG_DECIMAL: Lazy<RClass> = Lazy::new(|ruby| class_named(ruby, "BigDecimal"));
29
+ static DATE: Lazy<RClass> = Lazy::new(|ruby| class_named(ruby, "Date"));
30
+
31
+ fn class_named(ruby: &Ruby, name: &str) -> RClass {
32
+ ruby.class_object()
33
+ .const_get::<_, RClass>(name)
34
+ .unwrap_or_else(|_| panic!("{name} must be loaded before FieldStruct::Avro::Native"))
35
+ }
36
+
37
+ /// The shape of a Ruby value, as far as the encoder cares. Only exact classes count; subclasses are `Other`.
38
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
39
+ enum Kind {
40
+ Nil,
41
+ Bool(bool),
42
+ Integer,
43
+ Float,
44
+ Str,
45
+ Symbol,
46
+ Hash,
47
+ Array,
48
+ Time,
49
+ Date,
50
+ BigDecimal,
51
+ Other,
52
+ }
53
+
54
+ impl Kind {
55
+ /// A plain value of some other Avro shape: the avro gem's validator rejects it for numbers, strings and the
56
+ /// container types.
57
+ fn is_foreign_scalar(self) -> bool {
58
+ matches!(
59
+ self,
60
+ Kind::Nil | Kind::Bool(_) | Kind::Integer | Kind::Float | Kind::Str | Kind::Symbol | Kind::Hash | Kind::Array
61
+ )
62
+ }
63
+ }
64
+
65
+ pub struct Writer<'a> {
66
+ ruby: &'a Ruby,
67
+ plan: &'a Plan,
68
+ string: RClass,
69
+ hash: RClass,
70
+ array: RClass,
71
+ time: RClass,
72
+ big_decimal: RClass,
73
+ date: RClass,
74
+ utf8: Index,
75
+ ascii: Index,
76
+ binary: Index,
77
+ }
78
+
79
+ /// The Julian day number of 1970-01-01.
80
+ const EPOCH_JD: i64 = 2_440_588;
81
+
82
+ fn same(a: RClass, b: RClass) -> bool {
83
+ a.as_raw() == b.as_raw()
84
+ }
85
+
86
+ impl<'a> Writer<'a> {
87
+ pub fn new(ruby: &'a Ruby, plan: &'a Plan) -> Self {
88
+ Writer {
89
+ ruby,
90
+ plan,
91
+ string: ruby.class_string(),
92
+ hash: ruby.class_hash(),
93
+ array: ruby.class_array(),
94
+ time: ruby.class_time(),
95
+ big_decimal: ruby.get_inner(&BIG_DECIMAL),
96
+ date: ruby.get_inner(&DATE),
97
+ utf8: ruby.utf8_encindex(),
98
+ ascii: ruby.usascii_encindex(),
99
+ binary: ruby.ascii8bit_encindex(),
100
+ }
101
+ }
102
+
103
+ fn kind(&self, v: Value) -> Kind {
104
+ if v.is_nil() {
105
+ return Kind::Nil;
106
+ }
107
+ if v.as_raw() == self.ruby.qtrue().as_raw() {
108
+ return Kind::Bool(true);
109
+ }
110
+ if v.as_raw() == self.ruby.qfalse().as_raw() {
111
+ return Kind::Bool(false);
112
+ }
113
+ if Integer::from_value(v).is_some() {
114
+ return Kind::Integer;
115
+ }
116
+ if Float::from_value(v).is_some() {
117
+ return Kind::Float;
118
+ }
119
+ if Symbol::from_value(v).is_some() {
120
+ return Kind::Symbol;
121
+ }
122
+ let class = v.class();
123
+ if same(class, self.string) {
124
+ Kind::Str
125
+ } else if same(class, self.hash) {
126
+ Kind::Hash
127
+ } else if same(class, self.array) {
128
+ Kind::Array
129
+ } else if same(class, self.time) {
130
+ Kind::Time
131
+ } else if same(class, self.big_decimal) {
132
+ Kind::BigDecimal
133
+ } else if same(class, self.date) {
134
+ Kind::Date
135
+ } else {
136
+ Kind::Other
137
+ }
138
+ }
139
+
140
+ /// Encodes `v` with node `index`, appending to `out`. On failure `out` may hold a partial write.
141
+ pub fn datum(&self, index: usize, v: Value, out: &mut Vec<u8>) -> Step {
142
+ let kind = self.kind(v);
143
+ match self.plan.node(index) {
144
+ Node::Null => match kind {
145
+ Kind::Nil => Ok(()),
146
+ _ => Err(Fail::Mismatch),
147
+ },
148
+ Node::Boolean => match kind {
149
+ Kind::Bool(b) => {
150
+ out.push(b as u8);
151
+ Ok(())
152
+ }
153
+ _ => Err(Fail::Mismatch),
154
+ },
155
+ Node::Int => self.integer(kind, v, i32::MIN as i64, i32::MAX as i64, out),
156
+ Node::Long => self.integer(kind, v, i64::MIN, i64::MAX, out),
157
+ Node::Float => match kind {
158
+ Kind::Float => {
159
+ let f = Float::from_value(v).ok_or(Fail::Unsure)?.to_f64();
160
+ if f.is_nan() {
161
+ return Err(Fail::Unsure);
162
+ }
163
+ out.extend_from_slice(&(f as f32).to_le_bytes());
164
+ Ok(())
165
+ }
166
+ k => Err(not_number(k)),
167
+ },
168
+ Node::Double => match kind {
169
+ Kind::Float => {
170
+ let f = Float::from_value(v).ok_or(Fail::Unsure)?.to_f64();
171
+ out.extend_from_slice(&f.to_le_bytes());
172
+ Ok(())
173
+ }
174
+ k => Err(not_number(k)),
175
+ },
176
+ Node::Bytes => match kind {
177
+ Kind::Str => {
178
+ let s = RString::from_value(v).ok_or(Fail::Unsure)?;
179
+ // SAFETY: copied out before any Ruby code runs.
180
+ write_bytes(unsafe { s.as_slice() }, out);
181
+ Ok(())
182
+ }
183
+ k => Err(not_text(k)),
184
+ },
185
+ Node::Str => match kind {
186
+ Kind::Str => self.text(RString::from_value(v).ok_or(Fail::Unsure)?, out),
187
+ k => Err(not_text(k)),
188
+ },
189
+ Node::Decimal { precision, scale } => match kind {
190
+ Kind::BigDecimal => self.decimal(v, *precision, *scale, out),
191
+ Kind::Nil => Err(Fail::Mismatch),
192
+ _ => Err(Fail::Unsure),
193
+ },
194
+ Node::Date => match kind {
195
+ Kind::Date => {
196
+ let jd: i64 = v.funcall("jd", ()).map_err(|_| Fail::Unsure)?;
197
+ let days = jd.checked_sub(EPOCH_JD).ok_or(Fail::Unsure)?;
198
+ if days < i32::MIN as i64 || days > i32::MAX as i64 {
199
+ return Err(Fail::Unsure);
200
+ }
201
+ write_long(days, out);
202
+ Ok(())
203
+ }
204
+ Kind::Nil => Err(Fail::Mismatch),
205
+ _ => Err(Fail::Unsure),
206
+ },
207
+ Node::Timestamp { per_second } => match kind {
208
+ Kind::Time => self.timestamp(v, *per_second, out),
209
+ Kind::Nil => Err(Fail::Mismatch),
210
+ _ => Err(Fail::Unsure),
211
+ },
212
+ Node::Enum { symbols } => match kind {
213
+ Kind::Str => {
214
+ let s = RString::from_value(v).ok_or(Fail::Unsure)?;
215
+ if !self.ascii_compatible(s) {
216
+ return Err(Fail::Unsure);
217
+ }
218
+ // SAFETY: compared before any Ruby code runs.
219
+ let bytes = unsafe { s.as_slice() };
220
+ match symbols.iter().position(|sym| sym.as_slice() == bytes) {
221
+ Some(i) => {
222
+ write_long(i as i64, out);
223
+ Ok(())
224
+ }
225
+ None => Err(Fail::Mismatch),
226
+ }
227
+ }
228
+ k if k.is_foreign_scalar() => Err(Fail::Mismatch),
229
+ _ => Err(Fail::Unsure),
230
+ },
231
+ Node::Array { items } => match kind {
232
+ Kind::Array => self.array(RArray::from_value(v).ok_or(Fail::Unsure)?, *items, out),
233
+ Kind::Hash => Err(Fail::Mismatch),
234
+ k => Err(not_container(k)),
235
+ },
236
+ Node::Map { values } => match kind {
237
+ Kind::Hash => self.map(RHash::from_value(v).ok_or(Fail::Unsure)?, *values, out),
238
+ Kind::Array => Err(Fail::Mismatch),
239
+ k => Err(not_container(k)),
240
+ },
241
+ Node::Record { fields } => match kind {
242
+ Kind::Hash => self.record(RHash::from_value(v).ok_or(Fail::Unsure)?, fields, out),
243
+ Kind::Array => Err(Fail::Mismatch),
244
+ k => Err(not_container(k)),
245
+ },
246
+ Node::Union { members } => self.union(members, v, out),
247
+ }
248
+ }
249
+
250
+ fn integer(&self, kind: Kind, v: Value, min: i64, max: i64, out: &mut Vec<u8>) -> Step {
251
+ match kind {
252
+ Kind::Integer => {
253
+ let n = Integer::from_value(v).ok_or(Fail::Unsure)?.to_i64().map_err(|_| Fail::Mismatch)?;
254
+ if n < min || n > max {
255
+ return Err(Fail::Mismatch);
256
+ }
257
+ write_long(n, out);
258
+ Ok(())
259
+ }
260
+ Kind::Float => Err(Fail::Mismatch),
261
+ k => Err(not_number(k)),
262
+ }
263
+ }
264
+
265
+ fn ascii_compatible(&self, s: RString) -> bool {
266
+ let enc = s.enc_get();
267
+ enc == self.utf8 || ((enc == self.ascii || enc == self.binary) && is_seven_bit(s))
268
+ }
269
+
270
+ /// A String for an Avro string: the avro gem transcodes to UTF-8, which leaves UTF-8 bytes alone and
271
+ /// 7-bit ASCII/binary bytes unchanged. Any other encoding is left to the pure path.
272
+ fn text(&self, s: RString, out: &mut Vec<u8>) -> Step {
273
+ if !self.ascii_compatible(s) {
274
+ return Err(Fail::Unsure);
275
+ }
276
+ // SAFETY: copied out before any Ruby code runs.
277
+ write_bytes(unsafe { s.as_slice() }, out);
278
+ Ok(())
279
+ }
280
+
281
+ fn decimal(&self, v: Value, precision: u32, scale: u32, out: &mut Vec<u8>) -> Step {
282
+ // The format is explicit: ActiveSupport changes the default to plain notation (spec decision N7).
283
+ let text: RString = v.funcall("to_s", ("E",)).map_err(|_| Fail::Unsure)?;
284
+ // SAFETY: parsed before any Ruby code runs.
285
+ let unscaled = decimal::Unscaled::parse(unsafe { text.as_slice() }, scale, precision).ok_or(Fail::Unsure)?;
286
+ write_bytes(&unscaled.twos_complement(), out);
287
+ Ok(())
288
+ }
289
+
290
+ /// Ticks since the epoch, floored: `to_i * per_second + nsec / (1e9 / per_second)`. `nsec` is already floored and
291
+ /// non-negative, so this equals flooring the exact instant.
292
+ fn timestamp(&self, v: Value, per_second: i64, out: &mut Vec<u8>) -> Step {
293
+ let secs: Integer = v.funcall("to_i", ()).map_err(|_| Fail::Unsure)?;
294
+ let secs = secs.to_i64().map_err(|_| Fail::Unsure)?;
295
+ let nsec: i64 = v.funcall("nsec", ()).map_err(|_| Fail::Unsure)?;
296
+ if !(0..1_000_000_000).contains(&nsec) {
297
+ return Err(Fail::Unsure);
298
+ }
299
+ let ticks = secs
300
+ .checked_mul(per_second)
301
+ .and_then(|t| t.checked_add(nsec / (1_000_000_000 / per_second)))
302
+ .ok_or(Fail::Unsure)?;
303
+ write_long(ticks, out);
304
+ Ok(())
305
+ }
306
+
307
+ fn array(&self, list: RArray, items: usize, out: &mut Vec<u8>) -> Step {
308
+ let len = list.len();
309
+ if len > 0 {
310
+ write_long(len as i64, out);
311
+ for i in 0..len {
312
+ // The length is re-read: nothing here runs Ruby code that could shrink the Array, but stay safe.
313
+ if i >= list.len() {
314
+ return Err(Fail::Unsure);
315
+ }
316
+ let item: Value = list.entry(i as isize).map_err(|_| Fail::Unsure)?;
317
+ self.datum(items, item, out)?;
318
+ }
319
+ }
320
+ out.push(0);
321
+ Ok(())
322
+ }
323
+
324
+ fn map(&self, hash: RHash, values: usize, out: &mut Vec<u8>) -> Step {
325
+ let len = hash.len();
326
+ if len > 0 {
327
+ write_long(len as i64, out);
328
+ let mut result: Step = Ok(());
329
+ let walk = hash.foreach(|key: Value, value: Value| {
330
+ let step = self.map_entry(key, value, values, out);
331
+ if step.is_err() {
332
+ result = step;
333
+ return Ok(ForEach::Stop);
334
+ }
335
+ Ok(ForEach::Continue)
336
+ });
337
+ walk.map_err(|_| Fail::Unsure)?;
338
+ result?;
339
+ }
340
+ out.push(0);
341
+ Ok(())
342
+ }
343
+
344
+ fn map_entry(&self, key: Value, value: Value, values: usize, out: &mut Vec<u8>) -> Step {
345
+ if self.kind(key) != Kind::Str {
346
+ return Err(Fail::Unsure);
347
+ }
348
+ let key = RString::from_value(key).ok_or(Fail::Unsure)?;
349
+ // The avro gem's union check raises on a key that is not valid in its encoding; leave those to it.
350
+ if key.enc_coderange_scan() == Coderange::Broken {
351
+ return Err(Fail::Unsure);
352
+ }
353
+ self.text(key, out)?;
354
+ self.datum(values, value, out)
355
+ }
356
+
357
+ /// The datum must be the Hash `to_datum` builds: String keys, one per field, in field order. Anything else is
358
+ /// left to the pure path.
359
+ fn record(&self, hash: RHash, fields: &[(Vec<u8>, usize)], out: &mut Vec<u8>) -> Step {
360
+ if hash.len() != fields.len() {
361
+ return Err(Fail::Unsure);
362
+ }
363
+ let mut index = 0usize;
364
+ let mut result: Step = Ok(());
365
+ let walk = hash.foreach(|key: Value, value: Value| {
366
+ let step = self.record_entry(fields, index, key, value, out);
367
+ index += 1;
368
+ if step.is_err() {
369
+ result = step;
370
+ return Ok(ForEach::Stop);
371
+ }
372
+ Ok(ForEach::Continue)
373
+ });
374
+ walk.map_err(|_| Fail::Unsure)?;
375
+ result?;
376
+ if index != fields.len() {
377
+ return Err(Fail::Unsure);
378
+ }
379
+ Ok(())
380
+ }
381
+
382
+ fn record_entry(&self, fields: &[(Vec<u8>, usize)], index: usize, key: Value, value: Value, out: &mut Vec<u8>) -> Step {
383
+ let (name, node) = fields.get(index).ok_or(Fail::Unsure)?;
384
+ let key = match self.kind(key) {
385
+ Kind::Str => RString::from_value(key).ok_or(Fail::Unsure)?,
386
+ _ => return Err(Fail::Unsure),
387
+ };
388
+ // SAFETY: compared before any Ruby code runs.
389
+ if unsafe { key.as_slice() } != name.as_slice() {
390
+ return Err(Fail::Unsure);
391
+ }
392
+ self.datum(*node, value, out)
393
+ }
394
+
395
+ /// The first member the value fits, as the avro gem picks it. A member this encoder is unsure about stops the
396
+ /// search: the pure path might pick it.
397
+ fn union(&self, members: &[usize], v: Value, out: &mut Vec<u8>) -> Step {
398
+ let start = out.len();
399
+ for (branch, member) in members.iter().enumerate() {
400
+ write_long(branch as i64, out);
401
+ match self.datum(*member, v, out) {
402
+ Ok(()) => return Ok(()),
403
+ Err(Fail::Mismatch) => out.truncate(start),
404
+ Err(Fail::Unsure) => return Err(Fail::Unsure),
405
+ }
406
+ }
407
+ Err(Fail::Mismatch)
408
+ }
409
+ }
410
+
411
+ fn not_number(kind: Kind) -> Fail {
412
+ match kind {
413
+ Kind::Nil | Kind::Bool(_) | Kind::Str | Kind::Symbol | Kind::Hash | Kind::Array => Fail::Mismatch,
414
+ _ => Fail::Unsure,
415
+ }
416
+ }
417
+
418
+ fn not_text(kind: Kind) -> Fail {
419
+ match kind {
420
+ Kind::Nil | Kind::Bool(_) | Kind::Integer | Kind::Float | Kind::Symbol | Kind::Hash | Kind::Array => Fail::Mismatch,
421
+ _ => Fail::Unsure,
422
+ }
423
+ }
424
+
425
+ fn not_container(kind: Kind) -> Fail {
426
+ match kind {
427
+ Kind::Nil | Kind::Bool(_) | Kind::Integer | Kind::Float | Kind::Str | Kind::Symbol => Fail::Mismatch,
428
+ _ => Fail::Unsure,
429
+ }
430
+ }
431
+
432
+ fn is_seven_bit(s: RString) -> bool {
433
+ // SAFETY: read before any Ruby code runs.
434
+ unsafe { s.as_slice() }.is_ascii()
435
+ }