minor improvements

This commit is contained in:
Anton-4 2024-04-08 19:40:06 +02:00
parent 612fa4c75a
commit e85cbdce75
No known key found for this signature in database
GPG key ID: 0971D718C0A9B937
2 changed files with 18 additions and 13 deletions

View file

@ -25,7 +25,7 @@ use super::wrap_in_decode_custom_decode_with;
/// {first: a, second: b} /// {first: a, second: b}
/// ``` /// ```
/// ///
/// we'd like to generate an impl like /// we'd like to generate an implementation like
/// ///
/// ```roc /// ```roc
/// decoder : Decoder {first: a, second: b} fmt where a implements Decoding, b implements Decoding, fmt implements DecoderFormatting /// decoder : Decoder {first: a, second: b} fmt where a implements Decoding, b implements Decoding, fmt implements DecoderFormatting
@ -369,6 +369,7 @@ pub(super) fn step_field(
(expr, function_type) (expr, function_type)
} }
struct DecodingFieldArgs { struct DecodingFieldArgs {
field_name: Lowercase, field_name: Lowercase,
field_var: Variable, field_var: Variable,
@ -377,6 +378,9 @@ struct DecodingFieldArgs {
result_field_var: Variable, result_field_var: Variable,
decode_err_var: Variable, decode_err_var: Variable,
} }
/// Expression for custom decoder.
///
/// ```roc /// ```roc
/// Decode.custom \bytes, fmt -> /// Decode.custom \bytes, fmt ->
/// # Uses a single-branch `when` because `let` is more expensive to monomorphize /// # Uses a single-branch `when` because `let` is more expensive to monomorphize
@ -391,7 +395,6 @@ struct DecodingFieldArgs {
/// } /// }
/// ) /// )
/// ``` /// ```
/// Expression for custom decoder
fn custom_decoder(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variable, Expr) { fn custom_decoder(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variable, Expr) {
let (this_custom_callback_var, custom_callback) = custom_decoder_lambda(env, args); let (this_custom_callback_var, custom_callback) = custom_decoder_lambda(env, args);
@ -462,6 +465,7 @@ fn custom_decoder_lambda(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variabl
let fmt_arg_symbol = env.new_symbol("fmt"); let fmt_arg_symbol = env.new_symbol("fmt");
let bytes_arg_var = env.subs.fresh_unnamed_flex_var(); let bytes_arg_var = env.subs.fresh_unnamed_flex_var();
let fmt_arg_var = env.subs.fresh_unnamed_flex_var(); let fmt_arg_var = env.subs.fresh_unnamed_flex_var();
// The result of decoding this field's value - either the updated state, or a decoding error. // The result of decoding this field's value - either the updated state, or a decoding error.
let when_expr_var = { let when_expr_var = {
let flat_type = FlatType::TagUnion( let flat_type = FlatType::TagUnion(
@ -472,7 +476,7 @@ fn custom_decoder_lambda(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variabl
synth_var(env.subs, Content::Structure(flat_type)) synth_var(env.subs, Content::Structure(flat_type))
}; };
// What our decoder passed to `Decode.custom` returns - the result of decoding the // What our decoder passed to `Decode.custom` returns; the result of decoding the
// field's value, and the remaining bytes. // field's value, and the remaining bytes.
custom_callback_ret_var = { custom_callback_ret_var = {
let rest_field = RecordField::Required(Variable::LIST_U8); let rest_field = RecordField::Required(Variable::LIST_U8);
@ -1020,8 +1024,8 @@ pub(super) fn finalizer(
/// this allows the decoder to decode types that have a state for "missing", such as /// this allows the decoder to decode types that have a state for "missing", such as
/// an "Option" type. /// an "Option" type.
/// ///
/// field_var: The variable of the field in the state record: `Result var NoField` /// field_var: The variable of the field in the state record: `Result var NoField`.
/// result_field_var: The variable of the actual field /// result_field_var: The variable of the actual field.
fn attempt_empty_decode_if_missing( fn attempt_empty_decode_if_missing(
env: &mut Env<'_>, env: &mut Env<'_>,
DecodingFieldArgs { DecodingFieldArgs {
@ -1087,7 +1091,7 @@ fn attempt_empty_decode_if_missing(
// Example: `Ok x -> Ok x` // Example: `Ok x -> Ok x`
let ok_branch = ok_to_ok_branch(result_field_var, rec_dot_result, field_var, symbol, env); let ok_branch = ok_to_ok_branch(result_field_var, rec_dot_result, field_var, symbol, env);
// Example: `Err NoField -> when decodeWith [] decoder #Derived.fmt is ` // Example: `Err NoField -> when decodeWith [] decoder #Derived.fmt is`
let no_field_label = "NoField"; let no_field_label = "NoField";
let union_tags = UnionTags::tag_without_arguments(env.subs, no_field_label.into()); let union_tags = UnionTags::tag_without_arguments(env.subs, no_field_label.into());
let no_field_var = synth_var( let no_field_var = synth_var(
@ -1213,9 +1217,9 @@ fn initial_state(
struct DecodeWithVars { struct DecodeWithVars {
/// Type of the record returned by `Decode.decodeWith` /// Type of the record returned by `Decode.decodeWith`
/// `rec : { rest: List U8, result: (typeof rec.result) }`` /// `rec : { rest: List U8, result: (typeof rec.result) }`
rec_var: Variable, rec_var: Variable,
/// Type of the result field of the record returned by `Decode.decodeWith` /// type of the result field of the record returned by `Decode.decodeWith`
rec_dot_result: Variable, rec_dot_result: Variable,
/// type of `Decode.decoder` /// type of `Decode.decoder`
decoder_var: Variable, decoder_var: Variable,
@ -1225,7 +1229,7 @@ struct DecodeWithVars {
this_decode_with_var: Variable, this_decode_with_var: Variable,
} }
/// Makes the vars for decoding this particular field and decode format /// Makes the vars for decoding this particular field and decode format.
fn make_decode_with_vars( fn make_decode_with_vars(
env: &mut Env<'_>, env: &mut Env<'_>,
field_var: Variable, field_var: Variable,
@ -1287,7 +1291,7 @@ fn make_decode_with_vars(
/// ///
/// Generates a call to decodeWith, returns that expression, /// Generates a call to decodeWith, returns that expression,
/// the variable of the return value `{ rest: List U8, result: (typeof rec.result) }`, /// the variable of the return value `{ rest: List U8, result: (typeof rec.result) }`,
/// and the variable of the result field of the return value `[Ok field_var, Err DecodeError]` /// and the variable of the result field of the return value `[Ok field_var, Err DecodeError]`.
pub(super) fn decode_with( pub(super) fn decode_with(
env: &mut Env<'_>, env: &mut Env<'_>,
field_var: Variable, field_var: Variable,
@ -1296,7 +1300,7 @@ pub(super) fn decode_with(
fmt_arg_symbol: Symbol, fmt_arg_symbol: Symbol,
decode_err_var: Variable, decode_err_var: Variable,
) -> (Expr, Variable, Variable) { ) -> (Expr, Variable, Variable) {
// Creates all the vars we need to call decode_with for the specific field and fmt we are going to call it with // Creates all the vars we need to call decode_with for the specific field and fmt we are going to call it with.
let DecodeWithVars { let DecodeWithVars {
rec_var, rec_var,
rec_dot_result, rec_dot_result,

View file

@ -210,7 +210,7 @@ pub(crate) enum ExtensionKind {
} }
/// Ok a -> Ok a /// Ok a -> Ok a
/// A when is branch that matches Ok a and returns Ok a /// A `when ... is` branch that matches `Ok a` and returns `Ok a`
pub(crate) fn ok_to_ok_branch( pub(crate) fn ok_to_ok_branch(
pattern_var: Variable, pattern_var: Variable,
result_var: Variable, result_var: Variable,
@ -238,8 +238,9 @@ pub(crate) fn ok_to_ok_branch(
redundant: RedundantMark::known_non_redundant(), redundant: RedundantMark::known_non_redundant(),
} }
} }
/// `[]` /// `[]`
/// Creates an empty list of the type provided /// Creates an empty list of the type provided.
pub(crate) fn empty_list(var: Variable) -> Expr { pub(crate) fn empty_list(var: Variable) -> Expr {
Expr::List { Expr::List {
elem_var: var, elem_var: var,