style<S extends Spec<S>> static method

MixProtocolValueCodec<Style<S>> style<S extends Spec<S>>(
  1. MixProtocolBranchContext context
)

A recursively encoded style constrained to the requested spec type.

Implementation

static MixProtocolValueCodec<Style<S>> style<S extends Spec<S>>(
  MixProtocolBranchContext context,
) {
  final schema = Ack.codec<JsonMap, Object, Style<S>>(
    input: context._rootStyleSchema,
    decode: (value) {
      if (value is Style<S>) return value;

      throw SchemaPathError(
        code: MixProtocolErrorCode.typeMismatch,
        relativePath: '',
        reason:
            'Nested style decoded to ${value.runtimeType}; expected a $S '
            'style.',
        value: value,
      );
    },
    encode: (value) => value,
    output: Ack.instance<Style<S>>(),
  );

  return MixProtocolValueCodec._(
    schema.withConstraint(WireSchemaReference<Style<S>>('mix_protocol_style'))
        as AckSchema<Object, Style<S>>,
  );
}