fn truncate_dir<P: AsRef<Path>>(dir: P) -> Result<()> {
    let dir = dir.as_ref();

    if dir.exists() {
        if dir.is_dir() {
            fs::remove_dir_all(dir)
                .chain_err(|| format!("failed to remove existing directory '{:?}'", dir))?;
        } else if dir.is_file() {
            fs::remove_file(dir)
                .chain_err(|| format!("failed to remove existing file '{:?}'", dir))?;
        } else {
            return Err(format!(
                "failed to remove existing path '{:?}': not a file or directory",
                dir
            )
            .into());
        }
    }

    fs::create_dir_all(dir).chain_err(|| format!("failed to create directory '{:?}'", dir))
}
/// `truncate_dir` empties a directory and resets it's permission to the current
/// umask. If the directory doesn't exists, it creates it. If the path exists
/// but it's a file, it deletes the file and creates a directory there instead.
`truncate_dir` empties a directory and resets it's permission to the current
umask. If the directory doesn't exists, it creates it. If the path exists
but it's a file, it deletes the file and creates a directory there instead.
` truncate_dir ` empties a directory and resets it ' s permission to the current umask . If the directory doesn ' t exists it creates it . If the path exists but it ' s a file it deletes the file and creates a directory there instead .
fn truncate_dir<P: AsRef<Path>>(dir: P) -> Result<()> {
    let dir = dir.as_ref();
    if dir.exists() {
        if dir.is_dir() {
            fs::remove_dir_all(dir)
                .chain_err(|| format!("failed to remove existing directory '{:?}'", dir))?;
        } else if dir.is_file() {
            fs::remove_file(dir)
                .chain_err(|| format!("failed to remove existing file '{:?}'", dir))?;
        } else {
            return Err(format!(
                "failed to remove existing path '{:?}': not a file or directory",
                dir
            )
            .into());
        }
    }
    fs::create_dir_all(dir).chain_err(|| format!("failed to create directory '{:?}'", dir))
}
fn truncate_dir < P : AsRef < Path > > ( dir : P ) -> Result < ( ) > { let dir = dir . as_ref ( ) ; if dir . exists ( ) { if dir . is_dir ( ) { fs :: remove_dir_all ( dir ) . chain_err ( | | format ! ( " " dir ) ) ? ; } else if dir . is_file ( ) { fs :: remove_file ( dir ) . chain_err ( | | format ! ( " " dir ) ) ? ; } else { return Err ( format ! ( " " dir ) . into ( ) ) ; } } fs :: create_dir_all ( dir ) . chain_err ( | | format ! ( " " dir ) ) }
`truncate_dir` empties a directory and resets it's permission to the current
umask.
` truncate_dir ` empties a directory and resets it ' s permission to the current umask .
dir
P
dir
P