Skip to content

Commit

Permalink
fix: remove usage of unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 8, 2024
1 parent e502d1e commit 7d8a275
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#[derive(Debug)]
pub enum MdsfError {
Io(std::io::Error),
Fmt(core::fmt::Error),
}

impl core::fmt::Display for MdsfError {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Io(e) => e.fmt(f),
Self::Fmt(e) => e.fmt(f),
}
}
}

impl From<std::io::Error> for MdsfError {
#[inline]
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}

impl From<core::fmt::Error> for MdsfError {
#[inline]
fn from(value: core::fmt::Error) -> Self {
Self::Fmt(value)
}
}
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use clap::{builder::OsStr, Parser};
use cli::{Cli, Commands, FormatCommandArguments};
use error::MdsfError;
use formatters::format_snippet;
use languages::Language;
use pulldown_cmark::CowStr;
use pulldown_cmark_to_cmark::cmark_resume_with_options;

mod cli;
mod error;
mod formatters;
mod languages;

fn format_file(path: &std::path::Path) -> std::io::Result<()> {
fn format_file(path: &std::path::Path) -> Result<(), MdsfError> {
println!("Formatting {path:#?}");

let input = std::fs::read_to_string(path)?;
Expand Down Expand Up @@ -67,23 +69,24 @@ fn format_file(path: &std::path::Path) -> std::io::Result<()> {
..Default::default()
},
)
.unwrap()
.map_err(MdsfError::from)?
.into();
}

if modified {
if let Some(s) = state {
s.finalize(&mut output).unwrap();
s.finalize(&mut output).map_err(MdsfError::from)?;
}

println!("{path:#?} was formatted");
return std::fs::write(path, output);
return std::fs::write(path, output).map_err(MdsfError::from);
}

println!("{path:#?} was not changed");
Ok(())
}

fn format_command(args: FormatCommandArguments) -> std::io::Result<()> {
fn format_command(args: FormatCommandArguments) -> Result<(), MdsfError> {
if args.path.is_file() {
format_file(&args.path)?;
} else {
Expand Down

0 comments on commit 7d8a275

Please sign in to comment.