io_transform! {
mod calc;
mod init;
mod options;
pub use calc::FormatParams;
pub use options::{FatTypeSelection, FormatOptions, MediaType, OemName, SectorSize, VolumeLabel};
use crate::error::Result;
use super::fs::FatFs;
use super::io::{Read, Seek, Write};
pub struct FatVolumeFormatter;
impl FatVolumeFormatter {
pub async fn format<DATA>(mut data: DATA, options: FormatOptions) -> Result<FatFs<DATA>>
where
DATA: Read + Write + Seek,
{
use super::super::io::SeekFrom;
let params = calc::calculate_params(&options)?;
init::initialize_volume(&mut data, &options, ¶ms).await?;
data.seek(SeekFrom::Start(0)).await?;
FatFs::open(data).await
}
pub fn calculate_params(options: &FormatOptions) -> Result<FormatParams> {
calc::calculate_params(options)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use std::io::Cursor;
#[test]
fn test_format_fat12() {
let mut buffer = vec![0u8; 2 * 1024 * 1024];
let cursor = Cursor::new(&mut buffer[..]);
let options = FormatOptions::new(2 * 1024 * 1024).with_label("FAT12TEST");
let fs = FatVolumeFormatter::format(cursor, options).unwrap();
assert_eq!(fs.fat_type(), super::super::fat_table::FatType::Fat12);
assert_eq!(fs.volume_info().volume_label(), "FAT12TEST");
}
#[test]
fn test_format_fat16() {
let mut buffer = vec![0u8; 64 * 1024 * 1024];
let cursor = Cursor::new(&mut buffer[..]);
let options = FormatOptions::new(64 * 1024 * 1024).with_label("FAT16TEST");
let fs = FatVolumeFormatter::format(cursor, options).unwrap();
assert_eq!(fs.fat_type(), super::super::fat_table::FatType::Fat16);
assert_eq!(fs.volume_info().volume_label(), "FAT16TEST");
}
#[test]
fn test_format_fat32() {
let mut buffer = vec![0u8; 256 * 1024 * 1024];
let cursor = Cursor::new(&mut buffer[..]);
let options = FormatOptions::new(256 * 1024 * 1024)
.with_label("FAT32TEST")
.with_fat_type(FatTypeSelection::Fat32);
let fs = FatVolumeFormatter::format(cursor, options).unwrap();
assert_eq!(fs.fat_type(), super::super::fat_table::FatType::Fat32);
assert_eq!(fs.volume_info().volume_label(), "FAT32TEST");
}
#[test]
fn test_format_and_create_file() {
let mut buffer = vec![0u8; 4 * 1024 * 1024];
let cursor = Cursor::new(&mut buffer[..]);
let options = FormatOptions::new(4 * 1024 * 1024);
let fs = FatVolumeFormatter::format(cursor, options).unwrap();
let root = fs.root_dir();
let file_entry = fs.create_file(&root, "TEST.TXT").unwrap();
assert!(file_entry.name().starts_with("TEST"));
assert!(file_entry.name().contains("TXT"));
assert!(file_entry.is_file());
}
}