1use crate::font_style::FontStyle;
2use failure::{bail, Error};
3use getset::Getters;
4use std::convert::TryFrom;
5use std::fs::DirEntry;
6use std::path::PathBuf;
7
8#[derive(Debug, Getters, Default)]
10pub struct Font {
11 #[get = "pub"]
13 name: String,
14 #[get = "pub"]
16 path: PathBuf,
17 #[get = "pub"]
19 weight: Option<u16>,
20 #[get = "pub"]
22 style: crate::font_style::FontStyle,
23}
24
25impl TryFrom<DirEntry> for Font {
26 type Error = Error;
27
28 fn try_from(entry: DirEntry) -> Result<Self, Self::Error> {
29 let name = match entry.file_name().into_string() {
30 Ok(string) => string,
31 Err(_) => bail!("Could not convert OsString to String"),
32 };
33
34 Ok(Font {
35 name,
36 path: entry.path(),
37 style: FontStyle::default(),
38 weight: None,
39 })
40 }
41}
42
43impl TryFrom<&DirEntry> for Font {
44 type Error = Error;
45
46 fn try_from(entry: &DirEntry) -> Result<Self, Self::Error> {
47 let name = match entry.file_name().into_string() {
48 Ok(string) => string,
49 Err(_) => bail!("Could not convert OsString to String"),
50 };
51
52 Ok(Font {
53 name,
54 path: entry.path(),
55 style: FontStyle::default(),
56 weight: None,
57 })
58 }
59}