Skip to content

Commit 265a1fc

Browse files
committed
fix: Wrong inlined file prefix in CLI
Ref: #89
1 parent 6d856d5 commit 265a1fc

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Wrong inlined file prefixes handling in CLI. [#89](https://github.com/Stranger6667/css-inline/issues/89)
8+
59
### Performance
610

711
- Use `Formatter.write_str` instead of `write!` macro in the `Display` trait implementation for `InlineError`. [#85](https://github.com/Stranger6667/css-inline/issues/85)

src/main.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use css_inline::{CSSInliner, InlineOptions};
22
use rayon::prelude::*;
3+
use std::ffi::OsString;
4+
use std::path::Path;
35
use std::{
46
borrow::Cow,
57
error::Error,
@@ -98,20 +100,24 @@ fn main() -> Result<(), Box<dyn Error>> {
98100
} else {
99101
args.files
100102
.par_iter()
101-
.map(|filename| {
102-
File::open(filename)
103+
.map(|file_path| {
104+
File::open(file_path)
103105
.and_then(read_file)
104106
.and_then(|contents| {
105-
let mut new_filename =
106-
String::with_capacity(filename.len().saturating_add(8));
107-
new_filename.push_str("inlined.");
108-
new_filename.push_str(filename);
109-
File::create(new_filename).map(|file| (file, contents))
107+
let path = Path::new(file_path);
108+
let mut new_filename = OsString::from("inlined.");
109+
new_filename.push(
110+
path.to_path_buf()
111+
.file_name()
112+
.expect("It is already read, therefore it is a file"),
113+
);
114+
let new_path = path.with_file_name(new_filename);
115+
File::create(new_path).map(|file| (file, contents))
110116
})
111117
.map(|(mut file, contents)| {
112-
(filename, inliner.inline_to(contents.as_str(), &mut file))
118+
(file_path, inliner.inline_to(contents.as_str(), &mut file))
113119
})
114-
.map_err(|error| (filename, error))
120+
.map_err(|error| (file_path, error))
115121
})
116122
.for_each(|result| match result {
117123
Ok((filename, result)) => match result {

0 commit comments

Comments
 (0)