Skip to content

Commit dbf782c

Browse files
committed
refactor: re-use common code
1 parent c1b6c66 commit dbf782c

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

src/pipelines/copy_dir.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//! Copy-dir asset pipeline.
22
3-
use std::path::PathBuf;
4-
use std::sync::Arc;
5-
3+
use super::{data_target_path, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
4+
use crate::{
5+
common::{copy_dir_recursive, target_path},
6+
config::RtcBuild,
7+
};
68
use anyhow::{Context, Result};
79
use nipper::Document;
10+
use std::path::PathBuf;
11+
use std::sync::Arc;
812
use tokio::fs;
913
use tokio::task::JoinHandle;
1014

11-
use super::{Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
12-
use crate::common::{copy_dir_recursive, target_path};
13-
use crate::config::RtcBuild;
14-
1515
/// A CopyDir asset pipeline.
1616
pub struct CopyDir {
1717
/// The ID of this pipeline's source HTML element.
@@ -42,10 +42,7 @@ impl CopyDir {
4242
if !path.is_absolute() {
4343
path = html_dir.join(path);
4444
}
45-
let target_path = attrs
46-
.get("data-target-path")
47-
.map(|val| val.parse())
48-
.transpose()?;
45+
let target_path = data_target_path(&attrs)?;
4946

5047
Ok(Self {
5148
id,

src/pipelines/copy_file.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! Copy-file asset pipeline.
22
3-
use crate::common::target_path;
3+
use crate::{
4+
common::target_path,
5+
config::RtcBuild,
6+
pipelines::{
7+
data_target_path, AssetFile, AssetFileType, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
8+
},
9+
};
410
use anyhow::{Context, Result};
511
use nipper::Document;
612
use std::path::PathBuf;
713
use std::sync::Arc;
814
use tokio::task::JoinHandle;
915

10-
use crate::config::RtcBuild;
11-
use crate::pipelines::{AssetFile, AssetFileType, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
12-
1316
/// A CopyFile asset pipeline.
1417
pub struct CopyFile {
1518
/// The ID of this pipeline's source HTML element.
@@ -39,10 +42,7 @@ impl CopyFile {
3942
path.extend(href_attr.split('/'));
4043
let asset = AssetFile::new(&html_dir, path).await?;
4144

42-
let target_path = attrs
43-
.get("data-target-path")
44-
.map(|val| val.parse())
45-
.transpose()?;
45+
let target_path = data_target_path(&attrs)?;
4646

4747
Ok(Self {
4848
id,

src/pipelines/css.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! CSS asset pipeline.
22
3-
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_MINIFY};
4-
use crate::common::target_path;
3+
use super::{
4+
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
5+
ATTR_MINIFY,
6+
};
57
use crate::{
8+
common::target_path,
69
config::RtcBuild,
710
pipelines::AssetFileType,
811
processing::integrity::{IntegrityType, OutputDigest},
@@ -52,10 +55,7 @@ impl Css {
5255

5356
let minify = attrs.get(ATTR_MINIFY).is_none();
5457

55-
let target_path = attrs
56-
.get("data-target-path")
57-
.map(|val| val.parse())
58-
.transpose()?;
58+
let target_path = data_target_path(&attrs)?;
5959

6060
Ok(Self {
6161
id,

src/pipelines/icon.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Icon asset pipeline.
22
3-
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
3+
use super::{data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
44
use crate::common::target_path;
55
use crate::config::RtcBuild;
66
use crate::pipelines::{AssetFileType, ImageType};
@@ -45,10 +45,7 @@ impl Icon {
4545

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

48-
let target_path = attrs
49-
.get("data-target-path")
50-
.map(|val| val.parse())
51-
.transpose()?;
48+
let target_path = data_target_path(&attrs)?;
5249

5350
Ok(Self {
5451
id,

src/pipelines/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const ATTR_SRC: &str = "src";
4646
const ATTR_TYPE: &str = "type";
4747
const ATTR_REL: &str = "rel";
4848
const ATTR_MINIFY: &str = "data-no-minify";
49+
const ATTR_TARGET_PATH: &str = "data-target-path";
4950
const SNIPPETS_DIR: &str = "snippets";
5051
const TRUNK_ID: &str = "data-trunk-id";
5152
const PNG_OPTIMIZATION_LEVEL: u8 = 6;
@@ -398,3 +399,11 @@ impl fmt::Display for AttrWriter<'_> {
398399
Ok(())
399400
}
400401
}
402+
403+
/// Get the target path for an asset
404+
fn data_target_path(attrs: &Attrs) -> Result<Option<PathBuf>> {
405+
Ok(attrs
406+
.get(ATTR_TARGET_PATH)
407+
.map(|val| val.parse())
408+
.transpose()?)
409+
}

0 commit comments

Comments
 (0)