Skip to content

Commit 9f2aec2

Browse files
committed
fix: Expand on alternative syntax
1 parent de9862a commit 9f2aec2

File tree

1 file changed

+91
-25
lines changed

1 file changed

+91
-25
lines changed

text/3503-frontmatter.md

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,40 +187,93 @@ Downsides:
187187
- Familiar syntax in an unfamiliar use may make users feel unsettled, unsure how to proceed (what works and what doesn't).
188188
- If viewed from the lens of a comment, it isn't a variant of comment syntax like doc-comments
189189

190-
### Alternative 1: Static-site generator frontmatter
190+
### Alternative 1: Vary the opening/closing character
191191

192-
Note: this alternative was kept close to its source of inspiration, maintaining
193-
no infostring, despite being subject to the same questions that would be
194-
present for fenced code blocks, see [Future possibilities](#future-possibilities).
192+
Instead of backticks, we could do another character, like
193+
- `-`, making it look like YAML presentation streams, following the pattern of static site generators
194+
- `+` like [zola's frontmatter](https://www.getzola.org/documentation/getting-started/overview/#markdown-content)
195+
- `~`, using a lesser known markdown character
196+
- `=`
197+
- Open with `>>>` and close with `<<<`, like with HEREDOC (or invert it)
195198

199+
In practice:
196200
```rust
197201
#!/usr/bin/env cargo
198-
---
202+
---cargo
199203
[package]
200204
edition = "2018"
201205
---
202206

203207
fn main() {
204208
}
205209
```
206-
This is a subset/specialization of YAML presentation streams that mirrors people's experience with static site generators:
207-
- The first line post-shebang-stripping is 3+ dashes, then capture all content until a matching pair of dashes on a dedicated line. This would be captured into a `#![frontmatter = ""]`. `frontmatter` attribute is reserved for crate roots. The 3+ with matching pair is a "just in case" a TOML multi-line string has that syntax in it)
208-
- Future evolution: Allow a markdown-like infostring on the frontmatter opening dashes to declare the format with `cargo` being the default
209-
- Future evolution: Allow `frontmatter` attribute on any module
210+
```rust
211+
#!/usr/bin/env cargo
212+
+++cargo
213+
[package]
214+
edition = "2018"
215+
+++
216+
217+
fn main() {
218+
}
219+
```
220+
```rust
221+
#!/usr/bin/env cargo
222+
~~~cargo
223+
[package]
224+
edition = "2018"
225+
~~~
226+
227+
fn main() {
228+
}
229+
```
230+
```rust
231+
#!/usr/bin/env cargo
232+
===cargo
233+
[package]
234+
edition = "2018"
235+
===
236+
237+
fn main() {
238+
}
239+
```
240+
```rust
241+
#!/usr/bin/env cargo
242+
>>>cargo
243+
[package]
244+
edition = "2018"
245+
<<<
246+
247+
fn main() {
248+
}
249+
```
250+
```rust
251+
#!/usr/bin/env cargo
252+
<<<cargo
253+
[package]
254+
edition = "2018"
255+
>>>
256+
257+
fn main() {
258+
}
259+
```
210260

211261
Benefits
212-
- Visually/syntactically lightweight
213-
- Has parallels to ideas outside of Rust, building on external knowledge that might exist
214-
- Easy for cargo to parse and modify
215-
- Can easily be leveraged by buck2, meson, etc in the future
216-
- Users can edit/copy/paste the manifest without dealing with leading characters
262+
- With `-`, it builds on people's familiarity with static site generators
263+
- People can insert cargo-scripts into markdown (like chat, github issues)
264+
without being familiar enough with markdown to know how to escape backticks
265+
and to actually remember how to do it
217266

218267
Downsides
219-
- Too general that people might abuse it
220-
- We've extended the frontmatter syntax, undoing some of the "familiarity" benefit
221-
- People are used to YAML going in frontmatter (though some systems allow other syntaxes)
268+
- With `-`
269+
- We've extended the frontmatter syntax with an infostring, undoing some of the "familiarity" benefit
270+
- Potential congantive disonance as those familiar with frontmatter are used to YAML being there
271+
- With `>>>` it isn't quite like HEREDOC to have less overhead
222272
- Doesn't feel very rust-like
223273

274+
Note:
275+
- `"` was not considered because that can feel too familiar and users might carry over their expectations for how strings work
276+
224277
### Alternative 2: Extended Shebang
225278

226279
````rust
@@ -241,6 +294,15 @@ The hope would be that we could get buy-in from other languages.
241294
- Future evolution: Allow any `info` string with cargo checking for `content.starts_with(["cargo", "cargo,"])`
242295
- Future evolution: Allow `frontmatter` attribute on any module
243296

297+
If we dropped future possibilities for additional content, we could remove the opening/closing syntax and tweak it to avoid attribute ambiguity,
298+
greatly reducing the minimum syntax needed in some cases.
299+
````rust
300+
#!/usr/bin/env cargo
301+
## package.edition = "2018"
302+
303+
fn main() {}
304+
````
305+
244306
Benefits
245307
- Visually connected to the shebang
246308
- Has parallels to ideas outside of Rust, building on external knowledge that might exist
@@ -277,13 +339,14 @@ Benefits
277339
- Depending on doc-comment style used, users may be able to edit/copy/paste the manifest without dealing with leading characters
278340

279341
Downsides:
342+
- **Blocker** Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
343+
- If we extend additional restrictions to make it more tool friendly, then we break from user expectations for how this syntax works
280344
- When discussing with a Rust crash course teacher, it was felt their students would have a hard time learning to write these manifests from scratch
281345
- Having the explain the overloading of concepts to new users
282346
- Unpredictable location (both the doc comment and the cargo code block within it)
283347
- Visual clutter (where clutter is overwhelming already in Rust)
284348
- Might be a bit complicated to do edits (translating between location within
285349
`toml_edit` spans to the location within `syn` spans especially with different comment styles)
286-
- Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
287350
- Requires pulling in a full markdown parser to extract the manifest
288351
- Incorrectly formatted markdown would lead to a missing manifest and confusing error messages at best or silent incorrect behavior at worse
289352

@@ -311,12 +374,13 @@ Benefits
311374
- Users can edit/copy/paste the manifest without dealing with leading characters
312375

313376
Downsides
377+
- **Blocker** Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
378+
- If we extend additional restrictions to make it more tool friendly, then we break from user expectations for how this syntax works
314379
- When discussing with a Rust crash course teacher, it was felt their students would have a hard time learning to write these manifests from scratch
315380
- Unpredictable location (both the doc comment and the cargo code block within it)
316381
- From talking to a teacher, users are more forgiving of not understanding the details for structure data in an unstructured format (doc comments / comments) but something that looks meaningful, they will want to understand it all requiring dealing with all of the concepts
317382
- The attribute approach requires explaining multiple "advanced" topics: One teacher doesn't get to teaching any attributes until the second level in his crash course series and two teachers have found it difficult to teach people raw strings
318383
- Attributes look "scary" (and they are in some respects for the hidden stuff they do)
319-
- Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
320384

321385
### Alternative 5: Regular Comment
322386

@@ -356,17 +420,18 @@ Benefits
356420
- Depending on the exact syntax decided on, users may be able to edit/copy/paste the manifest without dealing with leading characters
357421

358422
Downsides
423+
- **Blocker** Assuming it can't be parsed with `syn` and either we need to write a
424+
sufficiently compatible comment parser or pull in a much larger rust parser
425+
to extract and update comments.
426+
- If we extend additional restrictions to make it more tool friendly, then we break from user expectations for how this syntax works
427+
- Like with doc comments, this should map to an attribute and then we'd just start the MVP with that attribute
359428
- Unfamiliar syntax
360429
- When discussing with a Rust crash course teacher, it was felt their students would have a hard time learning to write these manifests from scratch
361430
- Having the explain the overloading of concepts to new users
362431
- Unpredictable location (both the doc comment and the cargo code block within it)
363432
- Visual clutter (where clutter is overwhelming already in Rust)
364433
- New style of structured comment for the ecosystem to support with potential
365434
compatibility issues, likely requiring a new edition
366-
- Assuming it can't be parsed with `syn` and either we need to write a
367-
sufficiently compatible comment parser or pull in a much larger rust parser
368-
to extract and update comments.
369-
- Like with doc comments, this should map to an attribute and then we'd just start the MVP with that attribute
370435

371436
### Alternative 6: Macro
372437

@@ -386,13 +451,14 @@ Benefits
386451
- Users can edit/copy/paste the manifest without dealing with leading characters
387452

388453
Downsides
454+
- **Blocker** Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
455+
- If we extend additional restrictions to make it more tool friendly, then we break from user expectations for how this syntax works
389456
- When discussing with a Rust crash course teacher, it was felt their students would have a hard time learning to write these manifests from scratch
390457
- Unpredictable location (both the doc comment and the cargo code block within it)
391458
- The `cargo` macro would need to come from somewhere (`std`?) which means it is taking on `cargo`-specific knowledge
392459
- An unexplored direction we could go with this is a `meta!` macro (e.g. we'd need to have a format marker in it)
393460
- A lot of tools/IDEs have problems in dealing with macros
394461
- Free-form rust code makes it harder for cargo to make edits to the manifest
395-
- Either we expose `syn`s lesser parse errors or we skip errors, deferring to rustc's, but then have the wrong behavior on commands that don't invoke rustc, like `cargo metadata`
396462

397463
### Alternative 7: Presentation Streams
398464

@@ -417,7 +483,7 @@ Benefits
417483
- Users can edit/copy/paste the manifest without dealing with leading characters
418484

419485
Downsides
420-
- Difficult to parse without assistance from something like `syn` as we'd need to distinguish what the start of a stream is vs content of a string literal
486+
- **Blocker** Difficult to parse without assistance from something like `syn` as we'd need to distinguish what the start of a stream is vs content of a string literal
421487
- Being a new file format (a "text tar" format), there would be a lot of details to work out, including
422488
- How to delineate and label documents
423489
- How to allow escaping to avoid conflicts with content in a documents

0 commit comments

Comments
 (0)