Read settings in a bit
This commit is contained in:
@@ -1,13 +1,44 @@
|
||||
use tool_helper::{Input, Output, Result};
|
||||
pub use tool_helper::Error;
|
||||
|
||||
fn write_cpp(_input: Input, output: &mut Output, line_feed: &str) -> Result<()> {
|
||||
Error::try_with(write!(output, "const char data[] = {{{}", line_feed))?;
|
||||
Error::try_with(write!(output, "}};"))?;
|
||||
pub enum LineFeed {
|
||||
Unix,
|
||||
Windows
|
||||
}
|
||||
|
||||
pub enum FileType {
|
||||
CHeader,
|
||||
CSource,
|
||||
CPPHeader,
|
||||
CPPSource,
|
||||
}
|
||||
|
||||
pub struct Configuration {
|
||||
pub file_name: String,
|
||||
pub data_name: String,
|
||||
pub line_feed: LineFeed,
|
||||
pub file_type: FileType,
|
||||
}
|
||||
|
||||
struct FileVariables<'a> {
|
||||
include: &'a str,
|
||||
var_type: &'a str,
|
||||
}
|
||||
|
||||
const UNIX_LINEFEED: &'static str = "\n";
|
||||
const WINDOWS_LINEFEED: &'static str = "\r\n";
|
||||
|
||||
const C_VARIABLES: FileVariables = FileVariables{include: "", var_type: "char"};
|
||||
const CPP_VARIABLES: FileVariables = FileVariables{include: "#include <stdint.h>", var_type: "uint8_t"};
|
||||
|
||||
fn write_source_file(variables: &FileVariables, input: Input, output: &mut Output, line_feed: &str) -> Result<()> {
|
||||
Error::try_or_new(write!(output, "{}{}{}", variables.include, line_feed, line_feed), None)?;
|
||||
Error::try_or_new(write!(output, "const {} data[] = {{{}", variables.var_type, line_feed), None)?;
|
||||
Error::try_or_new(write!(output, "}};"), None)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn convert(_input: Input, mut output: Output) -> Result<()> {
|
||||
write_cpp(_input, &mut output, "\r\n")
|
||||
pub fn convert(_cfg: Configuration, _input: Input, mut output: Output) -> Result<()> {
|
||||
write_source_file(&CPP_VARIABLES, _input, &mut output, WINDOWS_LINEFEED)
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
use clap::{Parser};
|
||||
use std::path::PathBuf;
|
||||
use cpp_out::{Configuration, Error, FileType};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(about = "Output a file content or stdin to a c++ header/source file", long_about = None)]
|
||||
@@ -11,15 +12,32 @@ struct CommandLine {
|
||||
output_file: PathBuf,
|
||||
}
|
||||
|
||||
fn configurate(cmd: &CommandLine) -> tool_helper::Result<Configuration> {
|
||||
let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?;
|
||||
let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), "File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase();
|
||||
let file_type = Error::ok_or_new({
|
||||
match extension.as_ref() {
|
||||
"h" => Some(FileType::CHeader),
|
||||
"hpp" => Some(FileType::CPPHeader),
|
||||
"c" => Some(FileType::CSource),
|
||||
"cpp" => Some(FileType::CPPSource),
|
||||
_ => None
|
||||
}
|
||||
}, format!("{} unkown file extension", extension), None)?;
|
||||
|
||||
Ok(Configuration{file_name, data_name: "Planschbecken".to_owned(), line_feed: cpp_out::LineFeed::Windows, file_type})
|
||||
}
|
||||
|
||||
fn run_main() -> tool_helper::Result<()> {
|
||||
match CommandLine::try_parse() {
|
||||
Ok(cmd) => {
|
||||
let cfg = configurate(&cmd)?;
|
||||
let input = tool_helper::open_input(cmd.input_file)?;
|
||||
let output = tool_helper::open_output(Some(cmd.output_file))?;
|
||||
|
||||
return cpp_out::convert(input, output);
|
||||
return cpp_out::convert(cfg, input, output);
|
||||
},
|
||||
Err(error) => Err(tool_helper::Error::new(-1, error.to_string()))
|
||||
Err(error) => Err(tool_helper::Error::new(error.to_string(), None))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user