Skip to content

Commit

Permalink
Merge pull request tlsa#173 from tlsa/tlsa/scalar-style
Browse files Browse the repository at this point in the history
Tlsa/scalar style
  • Loading branch information
tlsa committed Oct 21, 2021
2 parents 67aa4ba + 8832276 commit 10c220b
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 3 deletions.
57 changes: 57 additions & 0 deletions include/cyaml/cyaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,63 @@ typedef enum cyaml_flag {
* \ref cyaml_strval strings.
*/
CYAML_FLAG_CASE_INSENSITIVE = (1 << 8),
/**
* When saving, emit scalar value with plain style (no quotes).
*
* \note This is ignored if the value is non-scaler.
*
* \note In cases where conflicting scalar style flags are set, the
* the one with the highest precedence is used. From lowest to
* highest precedence:
* \ref CYAML_FLAG_SCALAR_PLAIN,
* \ref CYAML_FLAG_SCALAR_FOLDED,
* \ref CYAML_FLAG_SCALAR_LITERAL,
* \ref CYAML_FLAG_SCALAR_QUOTE_SINGLE,
* \ref CYAML_FLAG_SCALAR_QUOTE_DOUBLE,
*
* If none of these are set, libyaml's default behaviour is used.
*/
CYAML_FLAG_SCALAR_PLAIN = (1 << 9),
/**
* When saving, emit scalar value with folded style:
*
* ```yaml
* string: >
* This string
* really has no line breaks!
* ```
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_FOLDED = (1 << 10),
/**
* When saving, emit scalar value with literal style:
*
* ```yaml
* string: |
* This is a
* multi-line string!
* ```
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_LITERAL = (1 << 11),
/**
* When saving, emit scalar value with single quotes (`'`).
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_QUOTE_SINGLE = (1 << 12),
/**
* When saving, emit scalar value with double quotes (`"`).
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_QUOTE_DOUBLE = (1 << 13),
} cyaml_flag_e;

/**
Expand Down
36 changes: 33 additions & 3 deletions src/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,35 @@ static inline yaml_mapping_style_t cyaml__get_emit_style_map(
return YAML_ANY_MAPPING_STYLE;
}

/**
* Get the style to use for scalar values from value flags.
*
* \param[in] ctx The CYAML saving context.
* \return The libyaml scalar style to emit the value with.
*/
static inline yaml_scalar_style_t cyaml__get_emit_style_scalar(
const cyaml_schema_value_t *schema)
{
/* Consult flags in order of decreasing priority. */
if (schema->flags & CYAML_FLAG_SCALAR_QUOTE_DOUBLE) {
return YAML_DOUBLE_QUOTED_SCALAR_STYLE;

} else if (schema->flags & CYAML_FLAG_SCALAR_QUOTE_SINGLE) {
return YAML_SINGLE_QUOTED_SCALAR_STYLE;

} else if (schema->flags & CYAML_FLAG_SCALAR_LITERAL) {
return YAML_LITERAL_SCALAR_STYLE;

} else if (schema->flags & CYAML_FLAG_SCALAR_FOLDED) {
return YAML_FOLDED_SCALAR_STYLE;

} else if (schema->flags & CYAML_FLAG_SCALAR_PLAIN) {
return YAML_PLAIN_SCALAR_STYLE;
}

return YAML_ANY_SCALAR_STYLE;
}

/**
* Helper to discern whether to emit document delimiting marks.
*
Expand Down Expand Up @@ -488,7 +517,7 @@ static cyaml_err_t cyaml__emit_scalar(
int ret;
yaml_event_t event;

if (schema == NULL) {
if (schema->type == CYAML_MAPPING) {
cyaml__log(ctx->config, CYAML_LOG_INFO, "Save: [%s]\n", value);
} else {
cyaml__log(ctx->config, CYAML_LOG_INFO,
Expand All @@ -499,7 +528,7 @@ static cyaml_err_t cyaml__emit_scalar(
(yaml_char_t *)tag,
(yaml_char_t *)value,
(int)strlen(value),
1, 0, YAML_PLAIN_SCALAR_STYLE);
1, 1, cyaml__get_emit_style_scalar(schema));

return cyaml__emit_event_helper(ctx, ret, &event);
}
Expand Down Expand Up @@ -1136,7 +1165,8 @@ static cyaml_err_t cyaml__write_mapping(
}
}

err = cyaml__emit_scalar(ctx, NULL, field->key, YAML_STR_TAG);
err = cyaml__emit_scalar(ctx, ctx->state->schema,
field->key, YAML_STR_TAG);
if (err != CYAML_OK) {
return err;
}
Expand Down
Loading

0 comments on commit 10c220b

Please sign in to comment.