Skip to content

Commit 79ef017

Browse files
notriddlejyn514
authored andcommitted
Make font-awesome-as-a-crate usable from a const context
This allows it to fit more cleanly into codebases like rustdoc that use constants for their data. https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/icon.20harmonization/near/263796393
1 parent cf3d949 commit 79ef017

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

crates/font-awesome-as-a-crate/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "font-awesome-as-a-crate"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
# https://github.com/FortAwesome/Font-Awesome/blob/master/composer.json
55
authors = ["Michael Howell <[email protected]>", "Travis Chase", "Dave Gandy", "Rob Madole", "Jory Raphael", "Geremia Taglialatela", "Brian Talbot", "Mike Wilkerson", "Fonticons Inc <[email protected]>"]
66
edition = "2018"

crates/font-awesome-as-a-crate/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn write_fontawesome_sprite() {
1414
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("fontawesome.rs");
1515
let mut dest_file = File::create(&dest_path).unwrap();
1616
dest_file
17-
.write_all(b"fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir,file){")
17+
.write_all(b"const fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir.as_bytes(),file.as_bytes()){")
1818
.expect("fontawesome fn write");
1919
for dirname in &["brands", "regular", "solid"] {
2020
let dir = read_dir(Path::new("fontawesome-free-5.14.0-web/svgs").join(dirname)).unwrap();
@@ -34,7 +34,7 @@ fn write_fontawesome_sprite() {
3434
dest_file
3535
.write_all(
3636
format!(
37-
r####"("{dirname}","{filename}")=>r###"{data}"###,"####,
37+
r####"(b"{dirname}",b"{filename}")=>r###"{data}"###,"####,
3838
data = data,
3939
dirname = dirname,
4040
filename = filename.replace(".svg", ""),

crates/font-awesome-as-a-crate/src/lib.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Display for NameError {
4949
impl Error for NameError {}
5050

5151
impl Type {
52-
pub fn as_str(self) -> &'static str {
52+
pub const fn as_str(self) -> &'static str {
5353
match self {
5454
Type::Brands => "brands",
5555
Type::Regular => "regular",
@@ -79,7 +79,7 @@ impl Display for Type {
7979
/**
8080
Get a fontawesome svg file by its name.
8181
*/
82-
pub fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
82+
pub const fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
8383
let svg = fontawesome_svg(type_.as_str(), name);
8484
if svg.is_empty() {
8585
return Err(NameError);
@@ -89,6 +89,13 @@ pub fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
8989

9090
#[cfg(test)]
9191
mod tests {
92+
const fn usable_as_const_() {
93+
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
94+
}
95+
#[test]
96+
fn usable_as_const() {
97+
usable_as_const_();
98+
}
9299
#[test]
93100
fn it_works() {
94101
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());

0 commit comments

Comments
 (0)