Skip to content

Commit 8535c53

Browse files
vnghiactron
authored andcommitted
add deref for attr
1 parent f07e330 commit 8535c53

File tree

12 files changed

+41
-30
lines changed

12 files changed

+41
-30
lines changed

src/pipelines/copy_dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl CopyDir {
3737
r#"required attr `href` missing for <link data-trunk rel="copy-dir" .../> element"#,
3838
)?;
3939
let mut path = PathBuf::new();
40-
path.extend(href_attr.value.split('/'));
40+
path.extend(href_attr.split('/'));
4141
if !path.is_absolute() {
4242
path = html_dir.join(path);
4343
}

src/pipelines/copy_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl CopyFile {
3838
r#"required attr `href` missing for <link data-trunk rel="copy-file" .../> element"#,
3939
)?;
4040
let mut path = PathBuf::new();
41-
path.extend(href_attr.value.split('/'));
41+
path.extend(href_attr.split('/'));
4242
let asset = AssetFile::new(&html_dir, path).await?;
4343

4444
let target_path = data_target_path(&attrs)?;

src/pipelines/css.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Css {
4747
r#"required attr `href` missing for <link data-trunk rel="css" .../> element"#,
4848
)?;
4949
let mut path = PathBuf::new();
50-
path.extend(href_attr.value.split('/'));
50+
path.extend(href_attr.split('/'));
5151
let asset = AssetFile::new(&html_dir, path).await?;
5252

5353
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

src/pipelines/icon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Icon {
4444
r#"required attr `href` missing for <link data-trunk rel="icon" .../> element"#,
4545
)?;
4646
let mut path = PathBuf::new();
47-
path.extend(href_attr.value.split('/'));
47+
path.extend(href_attr.split('/'));
4848
let asset = AssetFile::new(&html_dir, path).await?;
4949

5050
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

src/pipelines/inline.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ impl Inline {
3737
)?;
3838

3939
let mut path = PathBuf::new();
40-
path.extend(href_attr.value.split('/'));
40+
path.extend(href_attr.split('/'));
4141

4242
let asset = AssetFile::new(&html_dir, path).await?;
43-
let content_type = ContentType::from_attr_or_ext(
44-
attrs.get(ATTR_TYPE).map(|attr| &attr.value),
45-
asset.ext.as_deref(),
46-
)?;
43+
let content_type =
44+
ContentType::from_attr_or_ext(attrs.get(ATTR_TYPE), asset.ext.as_deref())?;
4745

4846
Ok(Self {
4947
id,

src/pipelines/js.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Js {
4747
.get(ATTR_SRC)
4848
.context(r#"required attr `src` missing for <script data-trunk ...> element"#)?;
4949
let mut path = PathBuf::new();
50-
path.extend(src_attr.value.split('/'));
50+
path.extend(src_attr.split('/'));
5151
let asset = AssetFile::new(&html_dir, path).await?;
5252

5353
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

src/pipelines/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use std::{
4242
collections::HashMap,
4343
ffi::OsString,
4444
fmt::{self, Display},
45+
ops::Deref,
4546
path::{Path, PathBuf},
4647
sync::Arc,
4748
};
@@ -104,6 +105,20 @@ impl<S: Display> From<S> for Attr {
104105
}
105106
}
106107

108+
impl Deref for Attr {
109+
type Target = str;
110+
111+
fn deref(&self) -> &Self::Target {
112+
&self.value
113+
}
114+
}
115+
116+
impl AsRef<str> for Attr {
117+
fn as_ref(&self) -> &str {
118+
self
119+
}
120+
}
121+
107122
impl TrunkAsset {
108123
/// Construct a new instance.
109124
pub async fn from_html(
@@ -430,9 +445,9 @@ impl fmt::Display for AttrWriter<'_> {
430445
// then it's probably fine.
431446
write!(f, " {name}")?;
432447
let attr = &self.attrs[name];
433-
if !attr.value.is_empty() {
448+
if !attr.is_empty() {
434449
if attr.need_escape {
435-
let encoded = htmlescape::encode_attribute(&attr.value);
450+
let encoded = htmlescape::encode_attribute(attr);
436451
write!(f, "=\"{}\"", encoded)?;
437452
} else {
438453
write!(f, "=\"{}\"", attr.value)?;
@@ -447,7 +462,7 @@ impl fmt::Display for AttrWriter<'_> {
447462
fn data_target_path(attrs: &Attrs) -> Result<Option<PathBuf>> {
448463
Ok(attrs
449464
.get(ATTR_TARGET_PATH)
450-
.map(|attr| attr.value.trim_end_matches('/'))
465+
.map(|attr| attr.trim_end_matches('/'))
451466
.map(|val| val.parse())
452467
.transpose()?)
453468
}

src/pipelines/rust/mod.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl RustApp {
138138
.get(ATTR_HREF)
139139
.map(|attr| {
140140
let mut path = PathBuf::new();
141-
path.extend(attr.value.split('/'));
141+
path.extend(attr.split('/'));
142142
if !path.is_absolute() {
143143
path = html_dir.join(path);
144144
}
@@ -148,23 +148,21 @@ impl RustApp {
148148
path
149149
})
150150
.unwrap_or_else(|| html_dir.join("Cargo.toml"));
151-
let bin = attrs.get("data-bin").map(|attr| attr.value.to_string());
152-
let target_name = attrs
153-
.get("data-target-name")
154-
.map(|attr| attr.value.to_string());
151+
let bin = attrs.get("data-bin").map(|attr| attr.to_string());
152+
let target_name = attrs.get("data-target-name").map(|attr| attr.to_string());
155153
let keep_debug = attrs.contains_key("data-keep-debug");
156154
let typescript = attrs.contains_key("data-typescript");
157155
let no_demangle = attrs.contains_key("data-no-demangle");
158156
let app_type = attrs
159157
.get("data-type")
160-
.map(|attr| attr.value.parse())
158+
.map(|attr| attr.parse())
161159
.transpose()?
162160
.unwrap_or(RustAppType::Main);
163161
let reference_types = attrs.contains_key("data-reference-types");
164162
let weak_refs = attrs.contains_key("data-weak-refs");
165163
let wasm_opt = attrs
166164
.get("data-wasm-opt")
167-
.map(|attr| attr.value.parse())
165+
.map(|attr| attr.parse())
168166
.transpose()?
169167
.unwrap_or_else(|| {
170168
if cfg.release {
@@ -176,20 +174,20 @@ impl RustApp {
176174
let wasm_opt_params = attrs
177175
.get("data-wasm-opt-params")
178176
.iter()
179-
.flat_map(|attr| attr.value.split_whitespace())
177+
.flat_map(|attr| attr.split_whitespace())
180178
.map(|val| val.to_string())
181179
.collect();
182180
let wasm_bindgen_target = attrs
183181
.get("data-bindgen-target")
184-
.map(|attr| attr.value.parse())
182+
.map(|attr| attr.parse())
185183
.transpose()?
186184
.unwrap_or(match app_type {
187185
RustAppType::Main => WasmBindgenTarget::Web,
188186
RustAppType::Worker => WasmBindgenTarget::NoModules,
189187
});
190188
let cross_origin = attrs
191189
.get("data-cross-origin")
192-
.map(|attr| CrossOrigin::from_str(&attr.value))
190+
.map(|attr| CrossOrigin::from_str(attr))
193191
.transpose()?
194192
.unwrap_or_default();
195193
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;
@@ -229,7 +227,7 @@ impl RustApp {
229227

230228
let data_features = attrs
231229
.get("data-cargo-features")
232-
.map(|attr| attr.value.to_string());
230+
.map(|attr| attr.to_string());
233231
let data_all_features = attrs.contains_key("data-cargo-all-features");
234232
let data_no_default_features = attrs.contains_key("data-cargo-no-default-features");
235233

@@ -268,7 +266,7 @@ impl RustApp {
268266

269267
let initializer = attrs
270268
.get("data-initializer")
271-
.map(|path| PathBuf::from_str(&path.value))
269+
.map(|path| PathBuf::from_str(path))
272270
.transpose()?
273271
.map(|path| {
274272
if !path.is_absolute() {
@@ -411,7 +409,7 @@ impl RustApp {
411409
];
412410
if let Some(profile) = &self.cargo_profile {
413411
args.push("--profile");
414-
args.push(&profile);
412+
args.push(profile);
415413
} else if self.cfg.release {
416414
args.push("--release");
417415
}

src/pipelines/sass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Sass {
4949
r#"required attr `href` missing for <link data-trunk rel="sass|scss" .../> element"#,
5050
)?;
5151
let mut path = PathBuf::new();
52-
path.extend(href_attr.value.split('/'));
52+
path.extend(href_attr.split('/'));
5353
let asset = AssetFile::new(&html_dir, path).await?;
5454
let use_inline = attrs.contains_key(ATTR_INLINE);
5555
let no_minify = attrs.contains_key(ATTR_NO_MINIFY);

src/pipelines/tailwind_css.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl TailwindCss {
5151
)?;
5252
let tailwind_config = attrs.get(ATTR_CONFIG).map(|attr| &attr.value).cloned();
5353
let mut path = PathBuf::new();
54-
path.extend(href_attr.value.split('/'));
54+
path.extend(href_attr.split('/'));
5555
let asset = AssetFile::new(&html_dir, path).await?;
5656
let use_inline = attrs.contains_key(ATTR_INLINE);
5757

0 commit comments

Comments
 (0)