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

Styling of Rust Frontmatter #6388

Open
epage opened this issue Nov 7, 2024 · 7 comments
Open

Styling of Rust Frontmatter #6388

epage opened this issue Nov 7, 2024 · 7 comments

Comments

@epage
Copy link
Contributor

epage commented Nov 7, 2024

RFC 3503 adds the concept of a frontmatter to rust for the sake of RFC 3502: cargo-script.

Ideally, before stabilization, we decide on the style and get support for it.

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

Question 1: Should rustfmt insert a blank line before frontmatters?

Compare:

#!/usr/bin/env cargo
---
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}
#!/usr/bin/env cargo

---
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

Question 2: Should rustfmt remove extraneous newlines before or after frontmatter?

Compare:

#!/usr/bin/env cargo
---
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}
#!/usr/bin/env cargo



---
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---




use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

Question 3: Should rustfmt remove extraneous spaces on the infostring

Compare:

#!/usr/bin/env cargo
---cargo
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}
#!/usr/bin/env cargo
---    cargo     
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

Question 4: Should rustfmt coerce to the minimum number of dashes

Compare:

#!/usr/bin/env cargo
---
[dependencies]
clap = { version = "4.2", features = ["derive"] }
---

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}
#!/usr/bin/env cargo
-----------------------
[dependencies]
clap = { version = "4.2", features = ["derive"] }
-----------------------

use clap::Parser;

#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(short, long, help = "Path to config")]
    config: Option<std::path::PathBuf>,
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

There is also the question of whether this issue, #4091, or another issue should be responsible for processing the embedded Cargo.toml.

@epage
Copy link
Contributor Author

epage commented Nov 7, 2024

Also, are there any expectations from rustc's lexer that need to be kept in mind for rustfmt? My initial assumption is to eat the frontmatter like we eat shebangs but I'm assuming rustfmt uses the lexer and that that might not work for you.

@ytmimi
Copy link
Contributor

ytmimi commented Nov 7, 2024

Also, are there any expectations from rustc's lexer that need to be kept in mind for rustfmt? My initial assumption is to eat the frontmatter like we eat shebangs but I'm assuming rustfmt uses the lexer and that that might not work for you.

rustfmt uses the rustc parser. Is it possible to represent the frontmatter in the AST? That would be idea for formatting.

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

No branches or pull requests

2 participants