1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 19:56:50 +00:00

feat: add shortcut methods to read OSC and Profile files

This commit is contained in:
Paul-Christian Volkmer 2023-12-31 18:34:42 +01:00
parent dc62454a74
commit 221b2c6a2f
2 changed files with 22 additions and 6 deletions

View File

@ -37,7 +37,7 @@ use sha256::digest;
use crate::checks::{check_file, print_checks, CheckNotice};
use crate::cli::SubCommand;
use crate::file_io::{FileError, InputFile};
use crate::file_io::{FileError, FileReader, InputFile};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
@ -160,7 +160,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
if let Some(profile) = profile {
let profile = if profile.contains('.') {
InputFile::read(profile.to_string(), None)?.try_into()?
FileReader::<Profile>::read(profile)?
} else {
Profile::embedded_profile(profile.as_str())?
};
@ -243,10 +243,8 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
style(&inputfile_b).yellow()
);
let data_a: &mut OnkostarEditor =
&mut InputFile::read(inputfile_a, None)?.try_into()?;
let data_b: &mut OnkostarEditor =
&mut InputFile::read(inputfile_b, None)?.try_into()?;
let data_a = &mut FileReader::<OnkostarEditor>::read(inputfile_a)?;
let data_b = &mut FileReader::<OnkostarEditor>::read(inputfile_b)?;
data_a.print_diff(data_b, strict);
}

View File

@ -25,6 +25,7 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fs;
use std::marker::PhantomData;
use std::path::Path;
use std::str::FromStr;
@ -206,3 +207,20 @@ impl TryFrom<InputFile> for Profile {
};
}
}
/// Shortcut methods for OSC and Profile files
pub struct FileReader<FileType> {
file_type: PhantomData<FileType>,
}
impl FileReader<OnkostarEditor> {
pub fn read(filename: String) -> Result<OnkostarEditor, FileError> {
TryInto::<OnkostarEditor>::try_into(InputFile::read(filename.to_string(), None)?)
}
}
impl FileReader<Profile> {
pub fn read(filename: String) -> Result<Profile, FileError> {
TryInto::<Profile>::try_into(InputFile::read(filename.to_string(), None)?)
}
}