Repository: servo/servo
File: components/fonts/platform/freetype/ohos/font_list.rs (Rust)

Code before:
    fonts: Vec<Font>,
}

struct FontAlias {
    from: String,
    to: String,
    weight: Option<i32>,
}

struct FontList {
    families: Vec<FontFamily>,
    aliases: Vec<FontAlias>,
}

fn enumerate_font_files() -> io::Result<Vec<PathBuf>> {
    let mut font_list = vec![];
    for elem in fs::read_dir(OHOS_FONTS_DIR)?.flatten() {
        if elem.file_type().is_ok_and(|file_type| file_type.is_file()) {
            let name = elem.file_name();
            let raw_name = name.as_bytes();
            if raw_name.ends_with(b".ttf".as_ref()) || raw_name.ends_with(b".ttc".as_ref()) {
                debug!(
                    "Found font {}",
                    elem.file_name()
                        .to_str()
                        .unwrap_or("<name containing non-utf8-data>")
                );
                font_list.push(elem.path())
            }
        }
    }
    Ok(font_list)
}

fn detect_hos_font_style(font_modifiers: &[&str]) -> Option<String> {
    if font_modifiers.contains(&"Italic") {
        Some("italic".to_string())
    } else {
        None
    }
}

// Note: The weights here are taken from the `alias` section of the fontconfig.json
fn detect_hos_font_weight_alias(font_modifiers: &[&str]) -> Option<i32> {
    if font_modifiers.contains(&"Light") {
        Some(100)
    } else if font_modifiers.contains(&"Regular") {
        Some(400)
    } else if font_modifiers.contains(&"Medium") {
        Some(700)
    } else if font_modifiers.contains(&"Bold") {

Review comment (suggestion):
I'm not sure what the original error type is, but perhaps it contains the valid utf-8 substring up-until the original string was utf-8? in that case printing that could be helpful to pinpoint the error to a specific file. 

Code after:
    fonts: Vec<Font>,
}

struct FontAlias {
    from: String,
    to: String,
    weight: Option<i32>,
}

struct FontList {
    families: Vec<FontFamily>,
    aliases: Vec<FontAlias>,
}

fn enumerate_font_files() -> io::Result<Vec<PathBuf>> {
    let mut font_list = vec![];
    for elem in fs::read_dir(OHOS_FONTS_DIR)?.flatten() {
        if elem.file_type().is_ok_and(|file_type| file_type.is_file()) {
            let name = elem.file_name();
            let raw_name = name.as_bytes();
            if raw_name.ends_with(b".ttf".as_ref()) || raw_name.ends_with(b".ttc".as_ref()) {
                debug!(
                    "Found font: {}",
                    String::from_utf8_lossy(elem.file_name().as_bytes())
                );
                font_list.push(elem.path())
            }
        }
    }
    Ok(font_list)
}

fn detect_hos_font_style(font_modifiers: &[&str]) -> Option<String> {
    if font_modifiers.contains(&"Italic") {
        Some("italic".to_string())
    } else {
        None
    }
}

// Note: The weights here are taken from the `alias` section of the fontconfig.json
fn detect_hos_font_weight_alias(font_modifiers: &[&str]) -> Option<i32> {
    if font_modifiers.contains(&"Light") {
        Some(100)
    } else if font_modifiers.contains(&"Regular") {
        Some(400)
    } else if font_modifiers.contains(&"Medium") {
        Some(700)
    } else if font_modifiers.contains(&"Bold") {
        Some(900)
    } else {