Browse Source

refactor out meta_str_to_map

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

37
src/main.rs

@ -46,6 +46,21 @@ pub enum Error { @@ -46,6 +46,21 @@ pub enum Error {
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 {
pub text: String,
pub meta: TextMeta,
@ -57,16 +72,7 @@ struct TextMeta { @@ -57,16 +72,7 @@ struct TextMeta {
impl TextMeta {
pub fn parse(from: &str) -> Result<Self, Error> {
let mut map = HashMap::new();
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()))
});
let map = meta_str_to_map(from)?;
// meh, this prolly doesnt perform well but idc its only init code
Ok(Self {
language: map.remove("language"),
@ -80,14 +86,9 @@ struct FileMeta { @@ -80,14 +86,9 @@ struct FileMeta {
impl FileMeta {
pub fn parse(from: &str) -> Result<Self, Error> {
let 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 map = meta_str_to_map(from)?;
let file_name = map.remove("file_name").ok_or(Error::Metadata)?;
Ok(Self { file_name })
}
}

Loading…
Cancel
Save