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

feat: read YAML files containing profile using new file io

This commit is contained in:
Paul-Christian Volkmer 2023-12-31 17:09:57 +01:00
parent ee600436f9
commit dc62454a74
2 changed files with 36 additions and 36 deletions

View File

@ -28,7 +28,6 @@ use std::fs::OpenOptions;
use std::io::Write;
use std::ops::Add;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use console::style;
use dialoguer::Confirm;
@ -55,14 +54,6 @@ fn write_outputfile(filename: String, content: &String) -> Result<(), FileError>
Ok(())
}
fn read_profile(filename: String) -> Result<Profile, FileError> {
let profile = fs::read_to_string(filename.clone())
.map_err(|err| FileError::Reading(filename.clone(), err.to_string()))?;
let profile =
Profile::from_str(profile.as_str()).map_err(|err| FileError::Reading(filename, err))?;
Ok(profile)
}
pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
match command {
SubCommand::List {
@ -120,7 +111,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
}
}
}
InputFile::Profile { filename, .. } | InputFile::Other { filename, .. } => {
InputFile::Yaml { filename, .. } | InputFile::Other { filename, .. } => {
return Err(Box::new(FileError::Reading(
filename,
"Nur OSB- und OSC-Dateien werden unterstützt".to_string(),
@ -148,7 +139,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
"Nur OSC-Dateien werden unterstützt. OSB-Dateien erzeugen eine zu lange Ausgabe."
.to_string(),
))),
InputFile::Profile { filename, .. } | InputFile::Other { filename, .. } => {
InputFile::Yaml { filename, .. } | InputFile::Other { filename, .. } => {
return Err(Box::new(FileError::Reading(
filename,
"Nur OSC-Dateien werden unterstützt".to_string(),
@ -169,9 +160,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
if let Some(profile) = profile {
let profile = if profile.contains('.') {
read_profile(profile.clone()).map_err(|_| {
FileError::Reading(profile, "Kann Profildatei nicht lesen!".into())
})?
InputFile::read(profile.to_string(), None)?.try_into()?
} else {
Profile::embedded_profile(profile.as_str())?
};

View File

@ -29,6 +29,7 @@ use std::path::Path;
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
pub enum FileError {
Reading(String, String),
@ -52,18 +53,11 @@ impl Display for FileError {
match &self {
FileError::Reading(filename, err) => format!("Kann Datei '{}' nicht lesen: {}", filename, err),
FileError::Writing(filename, err) => format!("Kann Datei '{}' nicht schreiben: {}", filename, err),
#[cfg(feature = "unzip-osb")]
FileError::Parsing(filename, err) => format!(
"Die Datei '{}' ist entweder keine OSB- oder OSC-Datei, fehlerhaft oder enthält zusätzliche Inhalte\n{}",
"Die Datei '{}' wird nicht unterstützt, ist fehlerhaft oder enthält zusätzliche Inhalte\n{}",
filename,
err
),
#[cfg(not(feature = "unzip-osb"))]
FileError::Parsing(filename, err) => format!(
"Die Datei '{}' ist keine OSC-Datei, fehlerhaft oder enthält zusätzliche Inhalte\n{}",
filename,
err
),
)
}
)
}
@ -75,14 +69,14 @@ pub enum InputFile {
filename: String,
content: String,
},
Profile {
filename: String,
content: String,
},
Osb {
filename: String,
content: Vec<InputFile>,
},
Yaml {
filename: String,
content: String,
},
Other {
filename: String,
content: Vec<u8>,
@ -93,8 +87,8 @@ impl InputFile {
pub fn filename(&self) -> String {
match self {
InputFile::Osc { filename, .. } => filename,
InputFile::Profile { filename, .. } => filename,
InputFile::Osb { filename, .. } => filename,
InputFile::Yaml { filename, .. } => filename,
InputFile::Other { filename, .. } => filename,
}
.to_string()
@ -159,15 +153,13 @@ impl InputFile {
content: result,
})
}
#[cfg(feature = "unzip-osb")]
Some("yml") | Some("yaml") => match fs::read_to_string(filename.clone()) {
Ok(content) => Ok(InputFile::Yaml { filename, content }),
Err(err) => Err(FileError::Reading(filename, err.to_string())),
},
_ => Err(FileError::Parsing(
filename,
"Nur OSB- oder OSC-Dateien werden unterstützt".to_string(),
)),
#[cfg(not(feature = "unzip-osb"))]
_ => Err(FileError::Parsing(
filename,
"Nur OSC-Dateien werden unterstützt".to_string(),
"Kein unterstütztes Dateiformat".to_string(),
)),
};
}
@ -188,10 +180,29 @@ impl TryFrom<InputFile> for OnkostarEditor {
Err(err) => Err(FileError::Parsing(filename, err)),
},
InputFile::Osb { filename, .. }
| InputFile::Profile { filename, .. }
| InputFile::Yaml { filename, .. }
| InputFile::Other { filename, .. } => {
Err(FileError::Parsing(filename, "Keine OSC-Datei".to_string()))
}
};
}
}
impl TryFrom<InputFile> for Profile {
type Error = FileError;
fn try_from(value: InputFile) -> Result<Self, Self::Error> {
return match value {
InputFile::Yaml { filename, content } => match Profile::from_str(&content) {
Ok(profile) => Ok(profile),
Err(err) => Err(FileError::Parsing(filename, err)),
},
InputFile::Osc { filename, .. }
| InputFile::Osb { filename, .. }
| InputFile::Other { filename, .. } => Err(FileError::Parsing(
filename,
"Keine Profildatei".to_string(),
)),
};
}
}