Skip to content

Commit

Permalink
fmt: cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Jul 10, 2024
1 parent 0490833 commit b3c8179
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 55 deletions.
12 changes: 10 additions & 2 deletions glass-easel-stylesheet-compiler/src/js_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ pub struct StyleSheetTransformer {
#[wasm_bindgen]
impl StyleSheetTransformer {
#[wasm_bindgen(constructor)]
pub fn new(name: &str, s: &str, class_prefix: Option<String>, rpx_ratio: f32, host_is: Option<String>) -> Self {
pub fn new(
name: &str,
s: &str,
class_prefix: Option<String>,
rpx_ratio: f32,
host_is: Option<String>,
) -> Self {
let mut sst = crate::StyleSheetTransformer::from_css(
name,
s,
Expand All @@ -72,7 +78,9 @@ impl StyleSheetTransformer {
let mut low_priority_content = String::new();
low_priority.write_str(&mut low_priority_content).unwrap();
let mut low_priority_source_map = Vec::new();
low_priority.write_source_map(&mut low_priority_source_map).unwrap();
low_priority
.write_source_map(&mut low_priority_source_map)
.unwrap();

Self {
warnings,
Expand Down
78 changes: 52 additions & 26 deletions glass-easel-stylesheet-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use cssparser::{CowRcStr, ParseError, ParserInput, Token};

pub mod error;
pub mod js_bindings;
mod step;
pub mod output;
mod step;

use output::StyleSheetOutput;
use step::{StepParser, StepToken};
Expand Down Expand Up @@ -89,11 +89,19 @@ impl StyleSheetTransformer {
}

fn current_output(&self) -> &StyleSheetOutput {
if self.using_low_priority { &self.low_priority_output } else { &self.normal_output }
if self.using_low_priority {
&self.low_priority_output
} else {
&self.normal_output
}
}

fn current_output_mut(&mut self) -> &mut StyleSheetOutput {
if self.using_low_priority { &mut self.low_priority_output } else { &mut self.normal_output }
if self.using_low_priority {
&mut self.low_priority_output
} else {
&mut self.normal_output
}
}

fn append_nested_block(
Expand Down Expand Up @@ -139,8 +147,14 @@ impl StyleSheetTransformer {
self.current_output_mut().append_token(token, src)
}

fn append_token_space_preserved(&mut self, token: StepToken, _input: &mut StepParser, src: Option<Token>) {
self.current_output_mut().append_token_space_preserved(token, src)
fn append_token_space_preserved(
&mut self,
token: StepToken,
_input: &mut StepParser,
src: Option<Token>,
) {
self.current_output_mut()
.append_token_space_preserved(token, src)
}

fn cur_output_utf8_len(&self) -> usize {
Expand Down Expand Up @@ -373,7 +387,9 @@ fn parse_at_rule(
let next = input.next()?;
match next.token.clone() {
Token::CurlyBracketBlock => {
let at_rule_str = ss.get_output_segment(output_index..ss.cur_output_utf8_len()).to_string();
let at_rule_str = ss
.get_output_segment(output_index..ss.cur_output_utf8_len())
.to_string();
ss.wrap_at_rule_output(input, at_rule_str, |ss, input| {
let close = ss.append_nested_block(next, input);
if contain_rule_list {
Expand Down Expand Up @@ -432,14 +448,18 @@ fn parse_qualified_rule(input: &mut StepParser, ss: &mut StyleSheetTransformer)
let quoted_host_is = Token::QuotedString(host_is.clone().into());
let r = input.try_parse::<_, _, ParseError<()>>(|input| {
input.expect_colon()?;
let Ok(next) = input.next() else { return Ok(()) };
let Ok(next) = input.next() else {
return Ok(());
};
let mut invalid = match &*next {
Token::Ident(x) if x.as_bytes() == b"host" => None,
Token::Function(x) if x.as_bytes() == b"host" => Some(input.position()),
_ => { return Err(input.new_custom_error(())) }
_ => return Err(input.new_custom_error(())),
};
let next = loop {
let Ok(next) = input.next() else { return Ok(()) };
let Ok(next) = input.next() else {
return Ok(());
};
if *next != Token::CurlyBracketBlock {
if invalid.is_none() {
invalid = Some(input.position());
Expand All @@ -449,17 +469,26 @@ fn parse_qualified_rule(input: &mut StepParser, ss: &mut StyleSheetTransformer)
}
};
if let Some(pos) = invalid {
ss.add_warning(
error::ParseErrorKind::HostSelectorCombination,
pos..pos,
);
ss.add_warning(error::ParseErrorKind::HostSelectorCombination, pos..pos);
} else {
ss.write_in_low_priority(input, |ss, input| {
ss.append_token(StepToken::wrap_at(Token::SquareBracketBlock, &next), input, None);
ss.append_token(StepToken::wrap_at(Token::Ident("is".into()), &next), input, None);
ss.append_token(
StepToken::wrap_at(Token::SquareBracketBlock, &next),
input,
None,
);
ss.append_token(
StepToken::wrap_at(Token::Ident("is".into()), &next),
input,
None,
);
ss.append_token(StepToken::wrap_at(Token::Delim('='), &next), input, None);
ss.append_token(StepToken::wrap_at(quoted_host_is, &next), input, None);
ss.append_token(StepToken::wrap_at(Token::CloseSquareBracket, &next), input, None);
ss.append_token(
StepToken::wrap_at(Token::CloseSquareBracket, &next),
input,
None,
);
let close = ss.append_nested_block(next, input);
convert_rpx_in_block(input, ss, None);
ss.append_nested_block_close(close, input);
Expand All @@ -468,7 +497,7 @@ fn parse_qualified_rule(input: &mut StepParser, ss: &mut StyleSheetTransformer)
Ok(())
});
if r.is_ok() {
return
return;
}
}
loop {
Expand Down Expand Up @@ -909,21 +938,18 @@ mod test {
);
assert_eq!(
trans.warnings().map(|x| x.kind.clone()).collect::<Vec<_>>(),
[error::ParseErrorKind::HostSelectorCombination, error::ParseErrorKind::HostSelectorCombination],
[
error::ParseErrorKind::HostSelectorCombination,
error::ParseErrorKind::HostSelectorCombination
],
);
let (output, lp) = trans.output_and_low_priority_output();
let mut s = Vec::new();
output.write(&mut s).unwrap();
assert_eq!(
std::str::from_utf8(&s).unwrap(),
r#".a{color:green}"#
);
assert_eq!(std::str::from_utf8(&s).unwrap(), r#".a{color:green}"#);
let mut s = Vec::new();
lp.write(&mut s).unwrap();
assert_eq!(
std::str::from_utf8(&s).unwrap(),
r#""#
);
assert_eq!(std::str::from_utf8(&s).unwrap(), r#""#);
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions glass-easel-stylesheet-compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ fn main() {
let (output, low_priority_output) = sst.output_and_low_priority_output();

if let Some(output_file) = args.low_priority_output {
let output_file = fs::File::create(output_file).expect("Failed to open or create output file");
let output_file =
fs::File::create(output_file).expect("Failed to open or create output file");
low_priority_output.write(output_file).unwrap();
} else {
let mut s = Vec::new();
Expand All @@ -100,7 +101,8 @@ fn main() {
}

if let Some(output_file) = args.output {
let output_file = fs::File::create(output_file).expect("Failed to open or create output file");
let output_file =
fs::File::create(output_file).expect("Failed to open or create output file");
output.write(output_file).unwrap();
} else {
let mut s = Vec::new();
Expand Down
8 changes: 2 additions & 6 deletions glass-easel-stylesheet-compiler/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Write;

use cssparser::{TokenSerializationType, ToCss, Token};
use cssparser::{ToCss, Token, TokenSerializationType};
use sourcemap::{SourceMap, SourceMapBuilder};

use crate::step::StepToken;
Expand Down Expand Up @@ -85,11 +85,7 @@ impl StyleSheetOutput {
self.utf16_len += str::encode_utf16(&self.s[output_start_pos..]).count() as u32;
}

pub(crate) fn append_token_space_preserved(
&mut self,
token: StepToken,
src: Option<Token>,
) {
pub(crate) fn append_token_space_preserved(&mut self, token: StepToken, src: Option<Token>) {
if let Token::WhiteSpace(_) = &*token {
self.prev_ser_type = token.serialization_type();
self.s.push(' ');
Expand Down
26 changes: 10 additions & 16 deletions glass-easel-template-compiler/src/parse/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl Template {
if ps.peek_str("</") {
let pos = ps.position();
ps.skip_until_after(">");
ps.add_warning(
ParseErrorKind::InvalidEndTag,
pos..ps.position(),
);
ps.add_warning(ParseErrorKind::InvalidEndTag, pos..ps.position());
}
}

Expand Down Expand Up @@ -2017,13 +2014,15 @@ impl Element {
x
} else {
let location = end_tag_start_location.start..close_location.end;
ps.add_warning(
ParseErrorKind::InvalidEndTag,
location.clone(),
);
Ident { name: CompactString::new(""), location }
ps.add_warning(ParseErrorKind::InvalidEndTag, location.clone());
Ident {
name: CompactString::new(""),
location,
}
};
if end_tag_name.name.len() > 0 && end_tag_name.name != tag_name.name.to_ascii_lowercase() {
if end_tag_name.name.len() > 0
&& end_tag_name.name != tag_name.name.to_ascii_lowercase()
{
return None;
}
ps.skip_whitespace();
Expand Down Expand Up @@ -3280,12 +3279,7 @@ mod test {
ParseErrorKind::UnexpectedCharacter,
12..16
);
case!(
"<div></>",
r#"<div/>"#,
ParseErrorKind::InvalidEndTag,
5..7
);
case!("<div></>", r#"<div/>"#, ParseErrorKind::InvalidEndTag, 5..7);
}

#[test]
Expand Down
24 changes: 21 additions & 3 deletions glass-easel-template-compiler/src/stringify/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ impl Stringify for Element {
"mark",
attr.prefix_location.as_ref().unwrap_or(&attr.name.location),
);
write_attr(stringifier, Some(prefix), &attr.name, &attr.value, Some(attr.is_value_unspecified))?;
write_attr(
stringifier,
Some(prefix),
&attr.name,
&attr.value,
Some(attr.is_value_unspecified),
)?;
}
for ev in event_bindings.iter() {
let prefix = if ev.is_catch {
Expand Down Expand Up @@ -410,7 +416,13 @@ impl Stringify for Element {
"model",
attr.prefix_location.as_ref().unwrap_or(&attr.name.location),
));
write_attr(stringifier, prefix, &attr.name, &attr.value, Some(attr.is_value_unspecified))?;
write_attr(
stringifier,
prefix,
&attr.name,
&attr.value,
Some(attr.is_value_unspecified),
)?;
}
for attr in change_attributes.iter() {
let prefix = (
Expand All @@ -435,7 +447,13 @@ impl Stringify for Element {
"data",
attr.prefix_location.as_ref().unwrap_or(&attr.name.location),
);
write_attr(stringifier, Some(prefix), &attr.name, &attr.value, Some(attr.is_value_unspecified))?;
write_attr(
stringifier,
Some(prefix),
&attr.name,
&attr.value,
Some(attr.is_value_unspecified),
)?;
}
for attr in generics.iter() {
write_static_attr(
Expand Down

0 comments on commit b3c8179

Please sign in to comment.