Skip to content

Commit c0fab37

Browse files
committed
Format code
1 parent 81e9d80 commit c0fab37

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

src/fat.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::file_data_source::FileDataSource;
12
use anyhow::Context;
2-
use std::{collections::BTreeMap, fs, io, path::Path};
3-
use std::fs::File;
43
use fatfs::{Dir, FileSystem};
5-
use crate::file_data_source::FileDataSource;
4+
use std::fs::File;
5+
use std::{collections::BTreeMap, fs, io, path::Path};
66

77
use crate::KERNEL_FILE_NAME;
88

@@ -31,7 +31,7 @@ pub fn create_fat_filesystem(
3131

3232
// choose a file system label
3333
let mut label = *b"MY_RUST_OS!";
34-
34+
3535
// This __should__ always be a file, but maybe not. Should we allow the caller to set the volume label instead?
3636
if let Some(FileDataSource::File(path)) = files.get(KERNEL_FILE_NAME) {
3737
if let Some(name) = path.file_stem() {
@@ -51,12 +51,15 @@ pub fn create_fat_filesystem(
5151
let filesystem = fatfs::FileSystem::new(&fat_file, fatfs::FsOptions::new())
5252
.context("Failed to open FAT file system of UEFI FAT file")?;
5353
let mut root_dir = filesystem.root_dir();
54-
54+
5555
// copy files to file system
5656
add_files_to_image(&root_dir, files)
5757
}
5858

59-
pub fn add_files_to_image(root_dir: &Dir<&File>, files: BTreeMap<&str, FileDataSource>) -> anyhow::Result<()> {
59+
pub fn add_files_to_image(
60+
root_dir: &Dir<&File>,
61+
files: BTreeMap<&str, FileDataSource>,
62+
) -> anyhow::Result<()> {
6063
for (target_path_raw, source) in files {
6164
let target_path = Path::new(target_path_raw);
6265
// create parent directories
@@ -76,11 +79,15 @@ pub fn add_files_to_image(root_dir: &Dir<&File>, files: BTreeMap<&str, FileDataS
7679
.create_file(target_path_raw)
7780
.with_context(|| format!("failed to create file at `{}`", target_path.display()))?;
7881
new_file.truncate().unwrap();
79-
80-
81-
source.copy_to(&mut new_file)
82-
.with_context(|| format!("failed to copy source data `{:?}` to file at `{}`", source, target_path.display()))?;
82+
83+
source.copy_to(&mut new_file).with_context(|| {
84+
format!(
85+
"failed to copy source data `{:?}` to file at `{}`",
86+
source,
87+
target_path.display()
88+
)
89+
})?;
8390
}
84-
91+
8592
Ok(())
8693
}

src/file_data_source.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
use std::path::{PathBuf};
21
use alloc::vec::Vec;
2+
use anyhow::Context;
33
use core::fmt::{Debug, Formatter};
4-
use std::{fs, io};
54
use std::fs::File;
65
use std::io::Cursor;
7-
use anyhow::Context;
6+
use std::path::PathBuf;
7+
use std::{fs, io};
88

99
#[derive(Clone)]
1010
pub enum FileDataSource {
1111
File(PathBuf),
12-
Data(Vec<u8>)
12+
Data(Vec<u8>),
1313
}
1414

1515
impl Debug for FileDataSource {
1616
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1717
match self {
18-
FileDataSource::File(file) => f.write_fmt(format_args!("data source: File {}", file.display())),
19-
FileDataSource::Data(d) => f.write_fmt(format_args!("data source: {} raw bytes ", d.len()))
18+
FileDataSource::File(file) => {
19+
f.write_fmt(format_args!("data source: File {}", file.display()))
20+
}
21+
FileDataSource::Data(d) => {
22+
f.write_fmt(format_args!("data source: {} raw bytes ", d.len()))
23+
}
2024
}
2125
}
2226
}
2327

2428
impl FileDataSource {
2529
pub fn len(&self) -> anyhow::Result<u64> {
2630
Ok(match self {
27-
FileDataSource::File(path) => {
28-
fs::metadata(path)
29-
.with_context(|| format!("failed to read metadata of file `{}`", path.display()))?
30-
.len()
31-
}
32-
FileDataSource::Data(v) => v.len() as u64
31+
FileDataSource::File(path) => fs::metadata(path)
32+
.with_context(|| format!("failed to read metadata of file `{}`", path.display()))?
33+
.len(),
34+
FileDataSource::Data(v) => v.len() as u64,
3335
})
3436
}
35-
37+
3638
pub fn copy_to(&self, target: &mut dyn io::Write) -> anyhow::Result<()> {
3739
match self {
3840
FileDataSource::File(file_path) => {
3941
io::copy(
40-
&mut fs::File::open(file_path)
41-
.with_context(|| format!("failed to open `{}` for copying", file_path.display()))?,
42+
&mut fs::File::open(file_path).with_context(|| {
43+
format!("failed to open `{}` for copying", file_path.display())
44+
})?,
4245
target,
4346
)?;
44-
},
47+
}
4548
FileDataSource::Data(contents) => {
4649
let mut cursor = Cursor::new(contents);
47-
io::copy(
48-
&mut cursor,
49-
target,
50-
)?;
50+
io::copy(&mut cursor, target)?;
5151
}
5252
};
53-
53+
5454
Ok(())
5555
}
56-
}
56+
}

src/lib.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use anyhow::Context;
3636

3737
use tempfile::NamedTempFile;
3838

39-
pub use bootloader_boot_config::BootConfig;
4039
use crate::file_data_source::FileDataSource;
40+
pub use bootloader_boot_config::BootConfig;
4141

4242
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
4343
const RAMDISK_FILE_NAME: &str = "ramdisk";
@@ -85,9 +85,13 @@ impl DiskImageBuilder {
8585
let bytes = json.as_bytes();
8686
self.add_or_replace_file(FileDataSource::Data(bytes.to_vec()), CONFIG_FILE_NAME)
8787
}
88-
88+
8989
/// Add or replace arbitrary files.
90-
pub fn add_or_replace_file(&mut self, file_data_source: FileDataSource, target: &str) -> &mut Self {
90+
pub fn add_or_replace_file(
91+
&mut self,
92+
file_data_source: FileDataSource,
93+
target: &str,
94+
) -> &mut Self {
9195
self.files.insert(
9296
0,
9397
DiskImageFile {
@@ -107,10 +111,12 @@ impl DiskImageBuilder {
107111
local_map.insert(f.destination.as_str(), f.source.clone());
108112
}
109113

110-
111114
for k in internal_files {
112115
if let Some(_) = local_map.insert(k.0, k.1) {
113-
return Err(anyhow::Error::msg(format!("Attempted to overwrite internal file: {}", k.0)));
116+
return Err(anyhow::Error::msg(format!(
117+
"Attempted to overwrite internal file: {}",
118+
k.0
119+
)));
114120
}
115121
}
116122

@@ -130,8 +136,14 @@ impl DiskImageBuilder {
130136
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
131137
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH"));
132138
let mut internal_files = BTreeMap::new();
133-
internal_files.insert(BIOS_STAGE_3, FileDataSource::File(stage_3_path.to_path_buf()));
134-
internal_files.insert(BIOS_STAGE_4, FileDataSource::File(stage_4_path.to_path_buf()));
139+
internal_files.insert(
140+
BIOS_STAGE_3,
141+
FileDataSource::File(stage_3_path.to_path_buf()),
142+
);
143+
internal_files.insert(
144+
BIOS_STAGE_4,
145+
FileDataSource::File(stage_4_path.to_path_buf()),
146+
);
135147

136148
let fat_partition = self
137149
.create_fat_filesystem_image(internal_files)
@@ -156,7 +168,10 @@ impl DiskImageBuilder {
156168
const UEFI_BOOT_FILENAME: &str = "efi/boot/bootx64.efi";
157169
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
158170
let mut internal_files = BTreeMap::new();
159-
internal_files.insert(UEFI_BOOT_FILENAME, FileDataSource::File(bootloader_path.to_path_buf()));
171+
internal_files.insert(
172+
UEFI_BOOT_FILENAME,
173+
FileDataSource::File(bootloader_path.to_path_buf()),
174+
);
160175
let fat_partition = self
161176
.create_fat_filesystem_image(internal_files)
162177
.context("failed to create FAT partition")?;
@@ -195,7 +210,7 @@ impl DiskImageBuilder {
195210
.create(true)
196211
.truncate(true)
197212
.open(to)?;
198-
213+
199214
f.source.copy_to(&mut new_file)?;
200215
}
201216

0 commit comments

Comments
 (0)