Skip to content

Commit

Permalink
Simplify fatfs example (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyoung authored Apr 8, 2022
1 parent 0d95159 commit 1d3236f
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions examples/fatfs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ thread_local! {

#[query]
fn ls() -> Vec<String> {
_ls().unwrap()
}

fn _ls() -> std::io::Result<Vec<String>> {
FS.with(|fs| {
let fs = fs.borrow();
let root_dir = fs.root_dir();
Expand All @@ -75,14 +71,11 @@ fn _ls() -> std::io::Result<Vec<String>> {
.collect();
entries
})
.unwrap()
}

#[query]
fn read_file(filename: String) -> String {
_read_file(filename).unwrap()
}

fn _read_file(filename: String) -> std::io::Result<String> {
FS.with(|fs| {
let fs = fs.borrow();
let root_dir = fs.root_dir();
Expand All @@ -91,23 +84,21 @@ fn _read_file(filename: String) -> std::io::Result<String> {
file.read_to_end(&mut buf)?;
let contents = String::from_utf8(buf)
.map_err(|error| std::io::Error::new(std::io::ErrorKind::Other, error))?;
Ok(contents)
std::io::Result::Ok(contents)
})
.unwrap()
}

#[update]
fn write_file(filename: String, contents: String) {
_write_file(filename, contents).unwrap();
}

fn _write_file(filename: String, contents: String) -> std::io::Result<()> {
FS.with(|fs| {
let fs = fs.borrow();
let root_dir = fs.root_dir();
let mut file = root_dir.create_file(&filename)?;
file.truncate()?;
file.write_all(&contents.into_bytes())?;
file.flush()?;
Ok(())
std::io::Result::Ok(())
})
.unwrap()
}

0 comments on commit 1d3236f

Please sign in to comment.