Add config writing capability #99
|
@ -1,7 +1,7 @@
|
|||
use crate::sd_reader;
|
||||
use core::fmt;
|
||||
use alloc::{string::FromUtf8Error, string::String, vec::Vec};
|
||||
use core_io::{self as io, BufRead, BufReader, Read};
|
||||
use core_io::{self as io, BufRead, BufReader, Read, Write};
|
||||
|
||||
use libboard_zynq::sdio;
|
||||
|
||||
|
@ -103,4 +103,41 @@ impl Config {
|
|||
pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> {
|
||||
Ok(String::from_utf8(self.read(key)?)?)
|
||||
}
|
||||
|
||||
pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{
|
||||
if let Some(fs) = &self.fs {
|
||||
let root_dir = fs.root_dir();
|
||||
let mut file;
|
||||
|
||||
|
||||
let isascii_and_short = data.is_ascii() & (data.len() <= 100);
|
||||
sb10q
commented
A better variable name would be A better variable name would be ``use_config_txt``.
|
||||
let file_path;
|
||||
let config_key_bin = &["/CONFIG/", key, ".BIN"].concat();
|
||||
|
||||
if isascii_and_short {
|
||||
file_path = "/CONFIG.TXT";
|
||||
} else {
|
||||
file_path = config_key_bin;
|
||||
}
|
||||
|
||||
file = match root_dir.create_file(file_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => root_dir.open_file("/CONFIG.TXT")?
|
||||
};
|
||||
|
||||
file.truncate()?;
|
||||
file.write_all(&[key,"="].concat().as_bytes())?;
|
||||
sb10q
commented
Why not a single Why not a single ``file.write_all(&[key, "=", data].concat().as_bytes())?`` (or similar) ?
Also, won't that erase all the other keys from ``config.txt``?
|
||||
file.write_all(data.as_bytes())?;
|
||||
|
||||
if isascii_and_short {
|
||||
match root_dir.remove(config_key_bin) {
|
||||
Ok(_) => {},
|
||||
sb10q
commented
You could simply add a You could simply add a ``Err(ErrorKind::NotFound)`` branch to the main ``match`` statement...
|
||||
Err(_) => {}
|
||||
sb10q
commented
You want to report errors that are not "the file doesn't exist". You want to report errors that are not "the file doesn't exist".
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::NoConfig)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
AFAICT this does not need a forward declaration.
You can just do
let mut file = match...
below. Themut
is perhaps not needed either.If mut is not used, I get "cannot borrow as mutable" when calling truncate and write_all