fn cmd_check<P: AsRef<path::Path>, Q: AsRef<path::Path>>(
    schema_path: P,
    input_files: Vec<Q>,
) -> Result<bool, ErrorAtLocation<CheckError, Location>> {

    // ensure schema_path evaluates to a real utf8 path
    let schema_path_s = schema_path
        .as_ref()
        .to_str()
        .ok_or_else(|| CheckError::SchemaPathUtf8Error.at(Location::Unspecified))?
        .to_owned();

    // get filename portion
    let meta_fn = schema_path
        .as_ref()
        .to_owned()
        .file_name()
        .ok_or_else(|| {
            CheckError::SchemaNotAFile.at(Location::File(schema_path_s.clone()))
        })?
        .to_str()
        .safe_unwrap("already verified UTF8")
        .to_owned();

    let meta = csvx::parse_filename(meta_fn.clone()).ok_or_else(|| {
        CheckError::InvalidCsvxFilename(meta_fn).at(Location::File(schema_path_s.clone()))
    })?;

    if !meta.is_schema() {
        return Err(CheckError::NotASchema.at(
            Location::File(schema_path_s.clone()),
        ));
    }

    // load schema
    let schema = csvx::CsvxSchema::from_file(schema_path).map_err(
        |e| e.convert(),
    )?;

    // schema validated correctly, reward user with a checkmark
    println!(
        "{} {}",
        Color::Green.paint(Attr::Bold.paint("✓")),
        Attr::Bold.paint(schema_path_s)
    );

    let mut all_good = true;
    for input_file in input_files {
        // validate filename first.
        // FIXME: should be moved into validation, as filename is validated
        //        and this whole section is a mess!
        let input_fn_s = input_file
            .as_ref()
            .to_owned()
            .file_name()
            .ok_or_else(|| unimplemented!())
            .unwrap()
            .to_string_lossy()
            .to_string();
        let inp_meta = csvx::parse_filename(&input_fn_s).ok_or_else(|| {
            CheckError::InvalidCsvxFilename(input_fn_s.clone()).at(
                Location::File(input_fn_s.clone()),
            )
        })?;

        // FIXME: should not abort just because schema of one file did not
        //        fit
        if inp_meta.schema != meta.table_name {
            return Err(
                CheckError::SchemaMismatch {
                    schema: meta.table_name.clone(),
                    data: inp_meta.schema.clone(),
                }.at(Location::File(
                    input_file.as_ref().to_string_lossy().to_string(),
                )),
            );
        }

        match schema.validate_file(&input_file) {
            Ok(()) => println!("{} {}",
                 Color::Green.paint(Attr::Bold.paint("✓")),
                 input_file.as_ref().to_string_lossy()),
            Err(errs) => {
                all_good = false;
                println!(
                    "{} {}",
                    Color::Red.paint(Attr::Bold.paint("✗")),
                    input_file.as_ref().to_string_lossy()
                );
                for e in errs {
                    e.print_help();
                }
            }
        }
    }

    Ok(all_good)
}
/// Check input files against schema.
///
/// Fatal and schema errors are returned as errors; failing input files just
/// result in a return value of `Ok(false)`.
Check input files against schema.
Fatal and schema errors are returned as errors; failing input files just
result in a return value of `Ok(false)`.
Check input files against schema . Fatal and schema errors are returned as errors ; failing input files just result in a return value of ` Ok ( false ) ` .
fn cmd_check<P: AsRef<path::Path>, Q: AsRef<path::Path>>(
    schema_path: P,
    input_files: Vec<Q>,
) -> Result<bool, ErrorAtLocation<CheckError, Location>> {
    // ensure schema_path evaluates to a real utf8 path
    let schema_path_s = schema_path
        .as_ref()
        .to_str()
        .ok_or_else(|| CheckError::SchemaPathUtf8Error.at(Location::Unspecified))?
        .to_owned();
    // get filename portion
    let meta_fn = schema_path
        .as_ref()
        .to_owned()
        .file_name()
        .ok_or_else(|| {
            CheckError::SchemaNotAFile.at(Location::File(schema_path_s.clone()))
        })?
        .to_str()
        .safe_unwrap("already verified UTF8")
        .to_owned();
    let meta = csvx::parse_filename(meta_fn.clone()).ok_or_else(|| {
        CheckError::InvalidCsvxFilename(meta_fn).at(Location::File(schema_path_s.clone()))
    })?;
    if !meta.is_schema() {
        return Err(CheckError::NotASchema.at(
            Location::File(schema_path_s.clone()),
        ));
    }
    // load schema
    let schema = csvx::CsvxSchema::from_file(schema_path).map_err(
        |e| e.convert(),
    )?;
    // schema validated correctly, reward user with a checkmark
    println!(
        "{} {}",
        Color::Green.paint(Attr::Bold.paint("✓")),
        Attr::Bold.paint(schema_path_s)
    );
    let mut all_good = true;
    for input_file in input_files {
        // validate filename first.
        // FIXME: should be moved into validation, as filename is validated
        //        and this whole section is a mess!
        let input_fn_s = input_file
            .as_ref()
            .to_owned()
            .file_name()
            .ok_or_else(|| unimplemented!())
            .unwrap()
            .to_string_lossy()
            .to_string();
        let inp_meta = csvx::parse_filename(&input_fn_s).ok_or_else(|| {
            CheckError::InvalidCsvxFilename(input_fn_s.clone()).at(
                Location::File(input_fn_s.clone()),
            )
        })?;
        // FIXME: should not abort just because schema of one file did not
        //        fit
        if inp_meta.schema != meta.table_name {
            return Err(
                CheckError::SchemaMismatch {
                    schema: meta.table_name.clone(),
                    data: inp_meta.schema.clone(),
                }.at(Location::File(
                    input_file.as_ref().to_string_lossy().to_string(),
                )),
            );
        }
        match schema.validate_file(&input_file) {
            Ok(()) => println!("{} {}",
                 Color::Green.paint(Attr::Bold.paint("✓")),
                 input_file.as_ref().to_string_lossy()),
            Err(errs) => {
                all_good = false;
                println!(
                    "{} {}",
                    Color::Red.paint(Attr::Bold.paint("✗")),
                    input_file.as_ref().to_string_lossy()
                );
                for e in errs {
                    e.print_help();
                }
            }
        }
    }
    Ok(all_good)
}
fn cmd_check < P : AsRef < path :: Path > , Q : AsRef < path :: Path > > ( schema_path : P , input_files : Vec < Q > , ) -> Result < bool , ErrorAtLocation < CheckError , Location > > { // ensure schema_path evaluates to a real utf8 path let schema_path_s = schema_path . as_ref ( ) . to_str ( ) . ok_or_else ( | | CheckError :: SchemaPathUtf8Error . at ( Location :: Unspecified ) ) ? . to_owned ( ) ; // get filename portion let meta_fn = schema_path . as_ref ( ) . to_owned ( ) . file_name ( ) . ok_or_else ( | | { CheckError :: SchemaNotAFile . at ( Location :: File ( schema_path_s . clone ( ) ) ) } ) ? . to_str ( ) . safe_unwrap ( " " ) . to_owned ( ) ; let meta = csvx :: parse_filename ( meta_fn . clone ( ) ) . ok_or_else ( | | { CheckError :: InvalidCsvxFilename ( meta_fn ) . at ( Location :: File ( schema_path_s . clone ( ) ) ) } ) ? ; if ! meta . is_schema ( ) { return Err ( CheckError :: NotASchema . at ( Location :: File ( schema_path_s . clone ( ) ) , ) ) ; } // load schema let schema = csvx :: CsvxSchema :: from_file ( schema_path ) . map_err ( | e | e . convert ( ) , ) ? ; // schema validated correctly, reward user with a checkmark println ! ( " " Color Green paint ( Attr Bold paint ( " ) , Attr Bold paint ( schema_path_s ) ) ; let mut all_good = true ; for input_file in input_files { // validate filename first. // FIXME: should be moved into validation, as filename is validated //        and this whole section is a mess! let input_fn_s = input_file . as_ref ( ) . to_owned ( ) . file_name ( ) . ok_or_else ( | | unimplemented ! ( ) ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ; let inp_meta = csvx :: parse_filename ( & input_fn_s ) . ok_or_else ( | | { CheckError :: InvalidCsvxFilename ( input_fn_s . clone ( ) ) . at ( Location :: File ( input_fn_s . clone ( ) ) , ) } ) ? ; // FIXME: should not abort just because schema of one file did not //        fit if inp_meta . schema != meta . table_name { return Err ( CheckError :: SchemaMismatch { schema : meta . table_name . clone ( ) , data : inp_meta . schema . clone ( ) , } . at ( Location :: File ( input_file . as_ref ( ) . to_string_lossy ( ) . to_string ( ) , ) ) , ) ; } match schema . validate_file ( & input_file ) { Ok ( ( ) ) => println ! ( " " Color Green paint ( Attr Bold paint ( " ) , input_file as_ref ( ) to_string_lossy ( ) ) , Err ( errs ) => { all_good = false ; println ! ( " " Color Red paint ( Attr Bold paint ( " ) , input_file as_ref ( ) to_string_lossy ( ) ) ; for e in errs { e . print_help ( ) ; } } } } Ok ( all_good ) }
Check input files against schema.
Check input files against schema .
schema_path
P
input_files
Vec<Q>