blob: 13d0bec23a3c87a272161658a8a72271fe5dcc37 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use {
crate::validators::{Validator, ValidatorError},
fuchsia_syslog as syslog,
/// Validator that checks if the given file is composed entirely of UTF-8 characters.
pub struct TextValidator;
impl Validator for TextValidator {
/// Validates that the given file contains only UTF-8 encoded text.
fn validate(&self, file_name: &str, contents: &[u8]) -> Result<(), ValidatorError> {
syslog::fx_log_info!("Validating {} is only UTF-8 encoded text", file_name);
from_utf8(contents).map(|_| ()).map_err(|cause| ValidatorError::FailedToValidate {
cause: Error::from(cause),
file_name: file_name.to_string(),
mod tests {
use super::*;
fn validate_with_text_succeeds() {
let validator = TextValidator;
let text = b"abcdefg";
validator.validate("file_to_validate", text).unwrap();
fn validate_with_empty_contents_succeeds() {
let validator = TextValidator;
let empty = [];
validator.validate("file_to_validate", &empty).unwrap();
fn validate_with_non_utf8_chars_fails() {
let validator = TextValidator;
let text = [0x80, 0xbf];
validator.validate("file_to_validate", &text).unwrap_err();