scan_fonts/
font.rs

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/// A font file on the filesystem.
9#[derive(Debug, Getters, Default)]
10pub struct Font {
11  /// Access the name of the font.
12  #[get = "pub"]
13  name: String,
14  /// Access the path of the font file on the filesystem.
15  #[get = "pub"]
16  path: PathBuf,
17  /// Access the font weight property.
18  #[get = "pub"]
19  weight: Option<u16>,
20  /// Access the font style property.
21  #[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}