Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make font-awesome-as-a-crate usable from a const context #1573

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/font-awesome-as-a-crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "font-awesome-as-a-crate"
version = "0.1.2"
version = "0.2.0"
# https://github.com/FortAwesome/Font-Awesome/blob/master/composer.json
authors = ["Michael Howell <michael@notriddle.com>", "Travis Chase", "Dave Gandy", "Rob Madole", "Jory Raphael", "Geremia Taglialatela", "Brian Talbot", "Mike Wilkerson", "Fonticons Inc <hello@fontawesome.com>"]
edition = "2018"
Expand Down
4 changes: 2 additions & 2 deletions crates/font-awesome-as-a-crate/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn write_fontawesome_sprite() {
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("fontawesome.rs");
let mut dest_file = File::create(&dest_path).unwrap();
dest_file
.write_all(b"fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir,file){")
.write_all(b"const fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir.as_bytes(),file.as_bytes()){")
.expect("fontawesome fn write");
for dirname in &["brands", "regular", "solid"] {
let dir = read_dir(Path::new("fontawesome-free-5.14.0-web/svgs").join(dirname)).unwrap();
Expand All @@ -34,7 +34,7 @@ fn write_fontawesome_sprite() {
dest_file
.write_all(
format!(
r####"("{dirname}","{filename}")=>r###"{data}"###,"####,
r####"(b"{dirname}",b"{filename}")=>r###"{data}"###,"####,
data = data,
dirname = dirname,
filename = filename.replace(".svg", ""),
Expand Down
11 changes: 9 additions & 2 deletions crates/font-awesome-as-a-crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Display for NameError {
impl Error for NameError {}

impl Type {
pub fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Type::Brands => "brands",
Type::Regular => "regular",
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Display for Type {
/**
Get a fontawesome svg file by its name.
*/
pub fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
pub const fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
let svg = fontawesome_svg(type_.as_str(), name);
if svg.is_empty() {
return Err(NameError);
Expand All @@ -89,6 +89,13 @@ pub fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {

#[cfg(test)]
mod tests {
const fn usable_as_const_() {
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
}
#[test]
fn usable_as_const() {
usable_as_const_();
}
#[test]
fn it_works() {
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
Expand Down