MCPcopy Create free account
hub / github.com/apache/avro-rs / encode_internal

Function encode_internal

avro/src/encode.rs:65–366  ·  view source on GitHub ↗
(
    value: &Value,
    schema: &Schema,
    names: &HashMap<Name, S>,
    enclosing_namespace: NamespaceRef,
    writer: &mut W,
)

Source from the content-addressed store, hash-verified

63}
64
65pub(crate) fn encode_internal<W: Write, S: Borrow<Schema>>(
66 value: &Value,
67 schema: &Schema,
68 names: &HashMap<Name, S>,
69 enclosing_namespace: NamespaceRef,
70 writer: &mut W,
71) -> AvroResult<usize> {
72 if let Schema::Ref { name } = schema {
73 let fully_qualified_name = name.fully_qualified_name(enclosing_namespace);
74 let resolved = names
75 .get(&fully_qualified_name)
76 .ok_or(Details::SchemaResolutionError(
77 fully_qualified_name.into_owned(),
78 ))?;
79 return encode_internal(value, resolved.borrow(), names, enclosing_namespace, writer);
80 }
81
82 match value {
83 Value::Null => {
84 if let Schema::Union(union) = schema {
85 match union.schemas.iter().position(|sch| *sch == Schema::Null) {
86 None => Err(Details::EncodeValueAsSchemaError {
87 value_kind: ValueKind::Null,
88 supported_schema: vec![SchemaKind::Null, SchemaKind::Union],
89 }
90 .into()),
91 Some(p) => encode_long(p as i64, writer),
92 }
93 } else {
94 Ok(0)
95 }
96 }
97 Value::Boolean(b) => writer
98 .write(&[u8::from(*b)])
99 .map_err(|e| Details::WriteBytes(e).into()),
100 // Pattern | Pattern here to signify that these _must_ have the same encoding.
101 Value::Int(i) | Value::Date(i) | Value::TimeMillis(i) => encode_int(*i, writer),
102 Value::Long(i)
103 | Value::TimestampMillis(i)
104 | Value::TimestampMicros(i)
105 | Value::TimestampNanos(i)
106 | Value::LocalTimestampMillis(i)
107 | Value::LocalTimestampMicros(i)
108 | Value::LocalTimestampNanos(i)
109 | Value::TimeMicros(i) => encode_long(*i, writer),
110 Value::Float(x) => writer
111 .write(&x.to_le_bytes())
112 .map_err(|e| Details::WriteBytes(e).into()),
113 Value::Double(x) => writer
114 .write(&x.to_le_bytes())
115 .map_err(|e| Details::WriteBytes(e).into()),
116 Value::Decimal(decimal) => match schema {
117 Schema::Decimal(DecimalSchema { inner, .. }) => match inner {
118 InnerDecimalSchema::Fixed(fixed) => {
119 let bytes = decimal.to_sign_extended_bytes_with_len(fixed.size)?;
120 let num_bytes = bytes.len();
121 if num_bytes != fixed.size {
122 return Err(

Callers 4

encodeFunction · 0.85
write_value_refMethod · 0.85

Calls 15

encode_longFunction · 0.85
encode_intFunction · 0.85
encodeFunction · 0.85
BytesClass · 0.85
encode_bytesFunction · 0.85
serialize_big_decimalFunction · 0.85
fully_qualified_nameMethod · 0.80
getMethod · 0.80
copy_only_sizeMethod · 0.80
as_bytesMethod · 0.80
is_emptyMethod · 0.80

Tested by

no test coverage detected