Skip to content

Commit

Permalink
Add drag-and-drop support
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
jam1garner committed Aug 29, 2021
1 parent 67f6fea commit 5de451b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ use clap::Clap;
#[derive(Clap)]
struct Args {
in_file: PathBuf,
out_file: PathBuf,
out_file: Option<PathBuf>,
}

fn main() {
let args = Args::parse();
let out_file = args.out_file.clone().unwrap_or_else(|| {
let mut out_file = args.in_file.clone();
match out_file.extension().map(|x| x.to_str()).flatten() {
Some("lvd") => out_file.set_extension("yaml"),
Some("yaml") | Some("yml") => out_file.set_extension("lvd"),
_ => true
};
out_file
});

match LvdFile::open(&args.in_file) {
Ok(lvd_file) => {
fs::write(&args.out_file, serde_yaml::to_string(&lvd_file).unwrap()).unwrap();
fs::write(&out_file, serde_yaml::to_string(&lvd_file).unwrap()).unwrap();
}
Err(binrw::Error::BadMagic { pos: 0, .. }) => {
// Magic doesn't match, is a yaml file
let contents = fs::read_to_string(&args.in_file).unwrap();
let lvd_file: LvdFile = serde_yaml::from_str(&contents).unwrap();

lvd_file.save(&args.out_file).unwrap();
lvd_file.save(&out_file).unwrap();
}
Err(err) => {
eprintln!("Error: {:?}", err);
Expand Down

0 comments on commit 5de451b

Please sign in to comment.