Browse Source

refactor out meta_str_to_map

xenua 2 years ago
parent
commit
41b18c97b0
  1. 35
      src/main.rs

35
src/main.rs

@ -46,6 +46,21 @@ pub enum Error {
FS, FS,
} }
fn meta_str_to_map(from: &str) -> Result<HashMap<&str, String>, Error> {
let map = from
.lines()
.map(|line| line.trim())
.filter(|line| line.len() > 0)
.map(|line| {
line.split_once(": ")
.ok_or(Error::Metadata)
.and_then(|(k, v)| Ok((k, v.to_string())))
})
.collect::<Result<HashMap<_, _>, Error>>()?;
Ok(map)
}
struct TextPaste { struct TextPaste {
pub text: String, pub text: String,
pub meta: TextMeta, pub meta: TextMeta,
@ -57,16 +72,7 @@ struct TextMeta {
impl TextMeta { impl TextMeta {
pub fn parse(from: &str) -> Result<Self, Error> { pub fn parse(from: &str) -> Result<Self, Error> {
let mut map = HashMap::new(); let map = meta_str_to_map(from)?;
from.lines()
.map(|line| line.trim())
.filter(|line| line.len() > 0)
.map(|line| {
line.split_once(": ")
.ok_or(Error::Metadata)
.map(|(k, v)| map.insert(k.to_string(), v.to_string()))
});
// meh, this prolly doesnt perform well but idc its only init code // meh, this prolly doesnt perform well but idc its only init code
Ok(Self { Ok(Self {
language: map.remove("language"), language: map.remove("language"),
@ -80,14 +86,9 @@ struct FileMeta {
impl FileMeta { impl FileMeta {
pub fn parse(from: &str) -> Result<Self, Error> { pub fn parse(from: &str) -> Result<Self, Error> {
let map = from let map = meta_str_to_map(from)?;
.lines()
.map(|line| line.trim())
.filter(|line| line.len() > 0)
.map(|line| line.split_once(": ").ok_or(Error::Metadata))
.collect::<Result<HashMap<_, _>, Error>>()?;
let file_name = map.remove("file_name").ok_or(Error::Metadata)?.to_string(); let file_name = map.remove("file_name").ok_or(Error::Metadata)?;
Ok(Self { file_name }) Ok(Self { file_name })
} }
} }

Loading…
Cancel
Save