/// Read in a CML file and produce the equivalent CM.
Read in a CML file and produce the equivalent CM.
Read in a CML file and produce the equivalent CM .
pub fn compile(file: &PathBuf, pretty: bool, output: Option<PathBuf>) -> Result<(), Error> {
    const BAD_IN_EXTENSION: &str = "Input file does not have the component manifest language \
                                    extension (.cml)";
    match file.extension().and_then(|e| e.to_str()) {
        Some("cml") => Ok(()),
        _ => Err(Error::invalid_args(BAD_IN_EXTENSION)),
    }?;
    const BAD_OUT_EXTENSION: &str =
        "Output file does not have the component manifest extension (.cm)";
    if let Some(ref path) = output {
        match path.extension().and_then(|e| e.to_str()) {
            Some("cm") => Ok(()),
            _ => Err(Error::invalid_args(BAD_OUT_EXTENSION)),
        }?;
    }
    let mut buffer = String::new();
    File::open(&file.as_path())?.read_to_string(&mut buffer)?;
    let value = cm_json::from_json5_str(&buffer)?;
    let document = validate::parse_cml(value)?;
    let out = compile_cml(document)?;
    let mut res = Vec::new();
    if pretty {
        let mut ser = Serializer::with_formatter(&mut res, PrettyFormatter::with_indent(b"    "));
        out.serialize(&mut ser)
            .map_err(|e| Error::parse(format!("Couldn't serialize JSON: {}", e)))?;
    } else {
        let mut ser = Serializer::with_formatter(&mut res, CompactFormatter {});
        out.serialize(&mut ser)
            .map_err(|e| Error::parse(format!("Couldn't serialize JSON: {}", e)))?;
    }
    if let Some(output_path) = output {
        fs::OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(output_path)?
            .write_all(&res)?;
    } else {
        println!("{}", from_utf8(&res)?);
    }
    // Sanity check that output conforms to CM schema.
    let json = serde_json::from_slice(&res)
        .map_err(|e| Error::parse(format!("Couldn't read output as JSON: {}", e)))?;
    cm_json::validate_json(&json, CM_SCHEMA)?;
    Ok(())
}
pub fn compile ( file : & PathBuf , pretty : bool , output : Option < PathBuf > ) -> Result < ( ) , Error > { const BAD_IN_EXTENSION : & str = " \
 " ; match file . extension ( ) . and_then ( | e | e . to_str ( ) ) { Some ( " " ) => Ok ( ( ) ) , _ => Err ( Error :: invalid_args ( BAD_IN_EXTENSION ) ) , } ? ; const BAD_OUT_EXTENSION : & str = " " ; if let Some ( ref path ) = output { match path . extension ( ) . and_then ( | e | e . to_str ( ) ) { Some ( " " ) => Ok ( ( ) ) , _ => Err ( Error :: invalid_args ( BAD_OUT_EXTENSION ) ) , } ? ; } let mut buffer = String :: new ( ) ; File :: open ( & file . as_path ( ) ) ? . read_to_string ( & mut buffer ) ? ; let value = cm_json :: from_json5_str ( & buffer ) ? ; let document = validate :: parse_cml ( value ) ? ; let out = compile_cml ( document ) ? ; let mut res = Vec :: new ( ) ; if pretty { let mut ser = Serializer :: with_formatter ( & mut res , PrettyFormatter :: with_indent ( b" " ) ) ; out . serialize ( & mut ser ) . map_err ( | e | Error :: parse ( format ! ( " " e ) ) ) ? ; } else { let mut ser = Serializer :: with_formatter ( & mut res , CompactFormatter { } ) ; out . serialize ( & mut ser ) . map_err ( | e | Error :: parse ( format ! ( " " e ) ) ) ? ; } if let Some ( output_path ) = output { fs :: OpenOptions :: new ( ) . create ( true ) . truncate ( true ) . write ( true ) . open ( output_path ) ? . write_all ( & res ) ? ; } else { println ! ( " " from_utf8 ( res ) ) ; } // Sanity check that output conforms to CM schema. let json = serde_json :: from_slice ( & res ) . map_err ( | e | Error :: parse ( format ! ( " " e ) ) ) ? ; cm_json :: validate_json ( & json , CM_SCHEMA ) ? ; Ok ( ( ) ) }
Read in a CML file and produce the equivalent CM.
Read in a CML file and produce the equivalent CM .
file
&PathBuf
pretty
bool
output
Option<PathBuf>