Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schnauzer: implement v2 template parser #3284

Merged
merged 1 commit into from
Jul 27, 2023

Conversation

cbgbt
Copy link
Contributor

@cbgbt cbgbt commented Jul 21, 2023

Issue number:
#3133

Description of changes:
The v2 template format requires all templates to include a frontmatter section which explicitly specifies each setting and helper that is used to render the template.

We use pest to separate the frontmatter of the document from its body, then serde to parse the frontmatter document.

Testing done:
The included tests pass.

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

@cbgbt cbgbt marked this pull request as ready for review July 21, 2023 20:44
Copy link
Contributor

@stmcginnis stmcginnis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, and test coverage is good!

@cbgbt
Copy link
Contributor Author

cbgbt commented Jul 26, 2023

^ Addresses feedback from @bcressey

@cbgbt
Copy link
Contributor Author

cbgbt commented Jul 26, 2023

^ Addresses feedback from @zmrow

Copy link
Contributor

@zmrow zmrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍰

Copy link
Contributor

@webern webern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@@ -0,0 +1,6 @@
[required-extensions]
beagle = """
+++
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test.

}

#[test]
fn fails_00_invalid_toml() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that you have written these as separate tests instead of cycling through directory templates. Separate tests are much better!

.context(error::GrammarParseSnafu)?
.flatten();

// If the template succesfully parses, we know there's always exactly one TOML document and one template body.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true even if front matter is not present? Is front matter always required in a template, or are there cases where front matter would be optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, the frontmatter is required, but it can be an empty document. In these cases, the template is like this:

+++
template body

We actually have a template in Bottlerocket right now which is a static file, and looks just like this (I can't recall which offhand).

Copy link
Contributor Author

@cbgbt cbgbt Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a testcase that does this.

Comment on lines 160 to 164
}
pub use error::Error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
}
pub use error::Error;
}
pub use error::Error;

The v2 template format requires all templates to include a frontmatter
section which explicitly specifies each setting and helper that is used
to render the template.
@cbgbt
Copy link
Contributor Author

cbgbt commented Jul 27, 2023

Comments from @webern had me realize that there was a mismatch in my own understanding of static-text templates and my implementation. The two changes above fix that, and also address a stylistic nit.

For what it's worth, we must always require the +++ delimiter at the start of a template, otherwise it is impossible for the parser to appropriately disambiguate a template body with +++ in it, especially if that body is a TOML document.

@cbgbt cbgbt merged commit 4fff0cb into bottlerocket-os:develop Jul 27, 2023
42 checks passed
@cbgbt cbgbt deleted the templateparser branch August 15, 2023 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants