pub fn from_path(conn: &Connection,
                     name: &str,
                     version: &str,
                     req_path: &str)
                     -> Option<FileList> {

        let rows = conn.query("SELECT crates.name,
                                      releases.version,
                                      releases.description,
                                      releases.target_name,
                                      releases.rustdoc_status,
                                      releases.files
                               FROM releases
                               LEFT OUTER JOIN crates ON crates.id = releases.crate_id
                               WHERE crates.name = $1 AND releases.version = $2",
                   &[&name, &version])
            .unwrap();

        if rows.len() == 0 {
            return None;
        }

        let files: Json = rows.get(0).get(5);

        let mut file_list: Vec<File> = Vec::new();

        files.as_array().map(|files| {
            for file in files {
                file.as_array().map(|file| {
                    let mime = file[0].as_string().unwrap();
                    let path = file[1].as_string().unwrap();

                    // skip .cargo-ok generated by cargo
                    if path == ".cargo-ok" {
                        return;
                    }

                    // look only files for req_path
                    if path.starts_with(&req_path) {
                        // remove req_path from path to reach files in this directory
                        let path = path.replace(&req_path, "");
                        let path_splited: Vec<&str> = path.split("/").collect();

                        // if path have '/' it is a directory
                        let ftype = if path_splited.len() > 1 {
                            FileType::Dir
                        } else if mime.starts_with("text") &&
                                              path_splited[0].ends_with(".rs") {
                            FileType::RustSource
                        } else if mime.starts_with("text") {
                            FileType::Text
                        } else {
                            FileType::Binary
                        };

                        let file = File {
                            name: path_splited[0].to_owned(),
                            file_type: ftype,
                        };

                        // avoid adding duplicates, a directory may occur more than once
                        if !file_list.contains(&file) {
                            file_list.push(file);
                        }
                    }
                });
            }
        });

        if file_list.is_empty() {
            return None;
        }

        file_list.sort_by(|a, b| {
            // directories must be listed first
            if a.file_type == FileType::Dir && b.file_type != FileType::Dir {
                Ordering::Less
            } else if a.file_type != FileType::Dir && b.file_type == FileType::Dir {
                Ordering::Greater
            } else {
                a.name.to_lowercase().cmp(&b.name.to_lowercase())
            }
        });

        Some(FileList {
            metadata: MetaData {
                name: rows.get(0).get(0),
                version: rows.get(0).get(1),
                description: rows.get(0).get(2),
                target_name: rows.get(0).get(3),
                rustdoc_status: rows.get(0).get(4),
            },
            files: file_list,
        })
    }
/// Gets FileList from a request path
///
/// All paths stored in database have this format:
///
/// ```text
/// [
///   ["text/plain",".gitignore"],
///   ["text/x-c","src/reseeding.rs"],
///   ["text/x-c","src/lib.rs"],
///   ["text/x-c","README.md"],
///   ...
/// ]
/// ```
///
/// This function is only returning FileList for requested directory. If is empty,
/// it will return list of files (and dirs) for root directory. req_path must be a
/// directory or empty for root directory.
Gets FileList from a request path
All paths stored in database have this format.



This function is only returning FileList for requested directory. If is empty,
it will return list of files (and dirs) for root directory. req_path must be a
directory or empty for root directory.
Gets FileList from a request path All paths stored in database have this format . This function is only returning FileList for requested directory . If is empty it will return list of files ( and dirs ) for root directory . req_path must be a directory or empty for root directory .
pub fn from_path(conn: &Connection,
                     name: &str,
                     version: &str,
                     req_path: &str)
                     -> Option<FileList> {
        let rows = conn.query("SELECT crates.name,
                                      releases.version,
                                      releases.description,
                                      releases.target_name,
                                      releases.rustdoc_status,
                                      releases.files
                               FROM releases
                               LEFT OUTER JOIN crates ON crates.id = releases.crate_id
                               WHERE crates.name = $1 AND releases.version = $2",
                   &[&name, &version])
            .unwrap();
        if rows.len() == 0 {
            return None;
        }
        let files: Json = rows.get(0).get(5);
        let mut file_list: Vec<File> = Vec::new();
        files.as_array().map(|files| {
            for file in files {
                file.as_array().map(|file| {
                    let mime = file[0].as_string().unwrap();
                    let path = file[1].as_string().unwrap();
                    // skip .cargo-ok generated by cargo
                    if path == ".cargo-ok" {
                        return;
                    }
                    // look only files for req_path
                    if path.starts_with(&req_path) {
                        // remove req_path from path to reach files in this directory
                        let path = path.replace(&req_path, "");
                        let path_splited: Vec<&str> = path.split("/").collect();
                        // if path have '/' it is a directory
                        let ftype = if path_splited.len() > 1 {
                            FileType::Dir
                        } else if mime.starts_with("text") &&
                                              path_splited[0].ends_with(".rs") {
                            FileType::RustSource
                        } else if mime.starts_with("text") {
                            FileType::Text
                        } else {
                            FileType::Binary
                        };
                        let file = File {
                            name: path_splited[0].to_owned(),
                            file_type: ftype,
                        };
                        // avoid adding duplicates, a directory may occur more than once
                        if !file_list.contains(&file) {
                            file_list.push(file);
                        }
                    }
                });
            }
        });
        if file_list.is_empty() {
            return None;
        }
        file_list.sort_by(|a, b| {
            // directories must be listed first
            if a.file_type == FileType::Dir && b.file_type != FileType::Dir {
                Ordering::Less
            } else if a.file_type != FileType::Dir && b.file_type == FileType::Dir {
                Ordering::Greater
            } else {
                a.name.to_lowercase().cmp(&b.name.to_lowercase())
            }
        });
        Some(FileList {
            metadata: MetaData {
                name: rows.get(0).get(0),
                version: rows.get(0).get(1),
                description: rows.get(0).get(2),
                target_name: rows.get(0).get(3),
                rustdoc_status: rows.get(0).get(4),
            },
            files: file_list,
        })
    }
pub fn from_path ( conn : & Connection , name : & str , version : & str , req_path : & str ) -> Option < FileList > { let rows = conn . query ( " " , & [ & name , & version ] ) . unwrap ( ) ; if rows . len ( ) == 0 { return None ; } let files : Json = rows . get ( 0 ) . get ( 5 ) ; let mut file_list : Vec < File > = Vec :: new ( ) ; files . as_array ( ) . map ( | files | { for file in files { file . as_array ( ) . map ( | file | { let mime = file [ 0 ] . as_string ( ) . unwrap ( ) ; let path = file [ 1 ] . as_string ( ) . unwrap ( ) ; // skip .cargo-ok generated by cargo if path == " " { return ; } // look only files for req_path if path . starts_with ( & req_path ) { // remove req_path from path to reach files in this directory let path = path . replace ( & req_path , " " ) ; let path_splited : Vec < & str > = path . split ( " " ) . collect ( ) ; // if path have '/' it is a directory let ftype = if path_splited . len ( ) > 1 { FileType :: Dir } else if mime . starts_with ( " " ) && path_splited [ 0 ] . ends_with ( " " ) { FileType :: RustSource } else if mime . starts_with ( " " ) { FileType :: Text } else { FileType :: Binary } ; let file = File { name : path_splited [ 0 ] . to_owned ( ) , file_type : ftype , } ; // avoid adding duplicates, a directory may occur more than once if ! file_list . contains ( & file ) { file_list . push ( file ) ; } } } ) ; } } ) ; if file_list . is_empty ( ) { return None ; } file_list . sort_by ( | a , b | { // directories must be listed first if a . file_type == FileType :: Dir && b . file_type != FileType :: Dir { Ordering :: Less } else if a . file_type != FileType :: Dir && b . file_type == FileType :: Dir { Ordering :: Greater } else { a . name . to_lowercase ( ) . cmp ( & b . name . to_lowercase ( ) ) } } ) ; Some ( FileList { metadata : MetaData { name : rows . get ( 0 ) . get ( 0 ) , version : rows . get ( 0 ) . get ( 1 ) , description : rows . get ( 0 ) . get ( 2 ) , target_name : rows . get ( 0 ) . get ( 3 ) , rustdoc_status : rows . get ( 0 ) . get ( 4 ) , } , files : file_list , } ) }
Gets FileList from a request path
All paths stored in database have this format:
Gets FileList from a request path All paths stored in database have this format :
conn
&Connection
name
&str
version
&str
req_path
&str