Skip to main content

rustidy_ast/
shebang.rs

1//! Shebang
2
3// Imports
4use {
5	rustidy_format::{Format, Formattable},
6	rustidy_parse::Parse,
7	rustidy_print::Print,
8	rustidy_util::AstStr,
9};
10
11/// Shebang
12#[derive(PartialEq, Eq, Clone, Debug)]
13#[derive(serde::Serialize, serde::Deserialize)]
14#[derive(Parse, Formattable, Format, Print)]
15#[parse(name = "a shebang")]
16#[parse(error(name = Shebang, fmt = "Expected a `#!`"))]
17#[format(no_prefix_ws)]
18pub struct Shebang(#[parse(try_update_with = Self::parse)] #[format(str)] pub AstStr);
19
20impl Shebang {
21	fn parse(s: &mut &str) -> Result<(), ShebangError> {
22		if !s.starts_with("#!") || s.starts_with("#![") {
23			return Err(ShebangError::Shebang);
24		}
25
26		*s = match s.find('\n') {
27			Some(idx) => &s[idx + 1..],
28			None => &s[s.len()..],
29		};
30
31		Ok(())
32	}
33}