🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2021-09-07 15:19:17.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2021-09-07 15:19:17.0 +01:00:00
commit
c492deffa5a75e3771b7f62a59ce033c16ddca0b [patch]
tree
b0188c42b5886afce8a86befb56202490898335a
parent
a0278262ae9c7b8cd124740f5dd555bae64a029d
download
c492deffa5a75e3771b7f62a59ce033c16ddca0b.tar.gz

clippy



Diff

 chartered-db/src/crates.rs                       | 59 ++++++++++++++++++++++++++++++++++++++++++-----------------
 chartered-db/src/lib.rs                          |  3 +++
 chartered-db/src/users.rs                        |  8 ++++----
 chartered-fs/src/lib.rs                          |  4 ++++
 chartered-git/src/main.rs                        | 25 ++++++++++---------------
 chartered-web/src/main.rs                        |  5 +++++
 chartered-web/src/middleware/auth.rs             |  6 +++---
 chartered-web/src/endpoints/cargo_api/publish.rs | 14 +++++++++++---
 8 files changed, 67 insertions(+), 57 deletions(-)

diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs
index 2187d6b..76715af 100644
--- a/chartered-db/src/crates.rs
+++ a/chartered-db/src/crates.rs
@@ -19,7 +19,7 @@
        conn: ConnectionPool,
    ) -> Result<HashMap<Crate, Vec<CrateVersion>>> {
        tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
            let conn = conn.get()?;

            let crate_versions = crates::table
                .inner_join(crate_versions::table)
@@ -31,26 +31,26 @@
    }

    pub async fn find_by_name(conn: ConnectionPool, crate_name: String) -> Result<Option<Self>> {
        use crate::schema::crates::dsl::*;
        use crate::schema::crates::dsl::{crates, name};

        Ok(tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
        tokio::task::spawn_blocking(move || {
            let conn = conn.get()?;

            crates
            Ok(crates
                .filter(name.eq(crate_name))
                .first::<Crate>(&conn)
                .optional()
                .optional()?)
        })
        .await??)
        .await?
    }

    pub async fn versions(self: Arc<Self>, conn: ConnectionPool) -> Result<Vec<CrateVersion>> {
        Ok(tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
        tokio::task::spawn_blocking(move || {
            let conn = conn.get()?;

            CrateVersion::belonging_to(&*self).load::<CrateVersion>(&conn)
            Ok(CrateVersion::belonging_to(&*self).load::<CrateVersion>(&conn)?)
        })
        .await??)
        .await?
    }

    pub async fn version(
@@ -58,17 +58,17 @@
        conn: ConnectionPool,
        crate_version: String,
    ) -> Result<Option<CrateVersion>> {
        use crate::schema::crate_versions::*;
        use crate::schema::crate_versions::version;

        Ok(tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
        tokio::task::spawn_blocking(move || {
            let conn = conn.get()?;

            CrateVersion::belonging_to(&*self)
            Ok(CrateVersion::belonging_to(&*self)
                .filter(version.eq(crate_version))
                .get_result::<CrateVersion>(&conn)
                .optional()
                .optional()?)
        })
        .await??)
        .await?
    }
}

@@ -89,21 +89,20 @@
    version_string: String,
    file_identifier: chartered_fs::FileReference,
    file_checksum: String,
) {
    use crate::schema::{crate_versions::dsl::*, crates::dsl::*};
) -> Result<()> {
    use crate::schema::{
        crate_versions::dsl::{checksum, crate_id, crate_versions, filesystem_object, version},
        crates::dsl::{crates, name},
    };

    tokio::task::spawn_blocking(move || {
        let conn = conn.get().unwrap();
        let conn = conn.get()?;

        insert_or_ignore_into(crates)
            .values(name.eq(&crate_name))
            .execute(&conn)
            .unwrap();
            .execute(&conn)?;

        let selected_crate = crates
            .filter(name.eq(crate_name))
            .first::<Crate>(&conn)
            .unwrap();
        let selected_crate = crates.filter(name.eq(crate_name)).first::<Crate>(&conn)?;

        insert_into(crate_versions)
            .values((
@@ -112,9 +111,9 @@
                filesystem_object.eq(file_identifier.to_string()),
                checksum.eq(file_checksum),
            ))
            .execute(&conn)
            .unwrap();
            .execute(&conn)?;

        Ok(())
    })
    .await
    .unwrap()
    .await?
}
diff --git a/chartered-db/src/lib.rs b/chartered-db/src/lib.rs
index cadecd3..564a2a4 100644
--- a/chartered-db/src/lib.rs
+++ a/chartered-db/src/lib.rs
@@ -1,3 +1,6 @@
#![deny(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]

pub mod crates;
pub mod schema;
pub mod users;
diff --git a/chartered-db/src/users.rs b/chartered-db/src/users.rs
index 9228b90..34dec2e 100644
--- a/chartered-db/src/users.rs
+++ a/chartered-db/src/users.rs
@@ -15,10 +15,10 @@
        conn: ConnectionPool,
        given_api_key: String,
    ) -> Result<Option<User>> {
        use crate::schema::user_api_keys::dsl::*;
        use crate::schema::user_api_keys::dsl::api_key;

        tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
            let conn = conn.get()?;

            Ok(crate::schema::user_api_keys::table
                .filter(api_key.eq(given_api_key))
@@ -34,12 +34,12 @@
        conn: ConnectionPool,
        given_ssh_key: Vec<u8>,
    ) -> Result<Option<User>> {
        use crate::schema::user_ssh_keys::dsl::*;
        use crate::schema::user_ssh_keys::dsl::ssh_key;

        eprintln!("looking up by ssh key: {:x?}", given_ssh_key);

        tokio::task::spawn_blocking(move || {
            let conn = conn.get().unwrap();
            let conn = conn.get()?;

            Ok(crate::schema::user_ssh_keys::table
                .filter(ssh_key.eq(given_ssh_key))
diff --git a/chartered-fs/src/lib.rs b/chartered-fs/src/lib.rs
index cbbd41f..ec43145 100644
--- a/chartered-fs/src/lib.rs
+++ a/chartered-fs/src/lib.rs
@@ -1,3 +1,6 @@
#![deny(clippy::pedantic)]
#![deny(clippy::pedantic)]

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::{
@@ -66,6 +69,7 @@
    async fn read(&self, file_ref: FileReference) -> Result<Vec<u8>, std::io::Error>;
    async fn write(&self, data: &[u8]) -> Result<FileReference, std::io::Error>;

    #[must_use]
    fn create_ref() -> FileReference {
        FileReference {
            file_system: Self::KIND,
diff --git a/chartered-git/src/main.rs b/chartered-git/src/main.rs
index 9857680..2dec1b5 100644
--- a/chartered-git/src/main.rs
+++ a/chartered-git/src/main.rs
@@ -87,22 +87,13 @@
    }
}

type AsyncHandlerFn = Pin<
    Box<
        dyn Future<Output = Result<(Handler, Session), <Handler as server::Handler>::Error>> + Send,
    >,
>;
type AsyncHandlerFut<T> =
    dyn Future<Output = Result<T, <Handler as server::Handler>::Error>> + Send;

impl server::Handler for Handler {
    type Error = anyhow::Error;
    type FutureAuth = Pin<
        Box<
            dyn Future<
                    Output = Result<(Handler, server::Auth), <Handler as server::Handler>::Error>,
                > + Send,
        >,
    >;
    type FutureUnit = AsyncHandlerFn;
    type FutureAuth = Pin<Box<AsyncHandlerFut<(Handler, server::Auth)>>>;
    type FutureUnit = Pin<Box<AsyncHandlerFut<(Handler, Session)>>>;
    type FutureBool = futures::future::Ready<Result<(Self, Session, bool), anyhow::Error>>;

    fn finished_auth(self, auth: Auth) -> Self::FutureAuth {
@@ -324,12 +315,14 @@
    links: Option<()>,
}

pub type TwoCharTree<T> = BTreeMap<[u8; 2], T>;

async fn fetch_tree(
    db: chartered_db::ConnectionPool,
) -> BTreeMap<[u8; 2], BTreeMap<[u8; 2], BTreeMap<String, String>>> {
) -> TwoCharTree<TwoCharTree<BTreeMap<String, String>>> {
    use chartered_db::crates::Crate;

    let mut tree: BTreeMap<[u8; 2], BTreeMap<[u8; 2], BTreeMap<String, String>>> = BTreeMap::new();
    let mut tree: TwoCharTree<TwoCharTree<BTreeMap<String, String>>> = BTreeMap::new();

    // todo: handle files with 1/2/3 characters
    for (crate_def, versions) in Crate::all_with_versions(db).await.unwrap() {
@@ -365,7 +358,7 @@
fn build_tree<'a>(
    root_tree: &mut Vec<TreeItem<'a>>,
    pack_file_entries: &mut Vec<PackFileEntry<'a>>,
    tree: &'a BTreeMap<[u8; 2], BTreeMap<[u8; 2], BTreeMap<String, String>>>,
    tree: &'a TwoCharTree<TwoCharTree<BTreeMap<String, String>>>,
) -> Result<(), anyhow::Error> {
    root_tree.reserve(tree.len());
    pack_file_entries.reserve(tree.iter().map(|(_, v)| 1 + v.len()).sum::<usize>() + tree.len());
diff --git a/chartered-web/src/main.rs b/chartered-web/src/main.rs
index 46d41ff..1ea1fee 100644
--- a/chartered-web/src/main.rs
+++ a/chartered-web/src/main.rs
@@ -1,3 +1,6 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

mod endpoints;
mod middleware;

@@ -7,6 +10,7 @@
};
use tower::{filter::AsyncFilterLayer, ServiceBuilder};

#[allow(clippy::unused_async)]
async fn hello_world() -> &'static str {
    "hello, world!"
}
@@ -26,6 +30,7 @@
}

#[tokio::main]
#[allow(clippy::semicolon_if_nothing_returned)] // lint breaks with tokio::main
async fn main() {
    env_logger::init();

diff --git a/chartered-web/src/middleware/auth.rs b/chartered-web/src/middleware/auth.rs
index 5a76763..af2112d 100644
--- a/chartered-web/src/middleware/auth.rs
+++ a/chartered-web/src/middleware/auth.rs
@@ -41,7 +41,7 @@
                .await
                .unwrap();

            let key = params.get("key").map(|v| v.as_str()).unwrap_or_default();
            let key = params.get("key").map(String::as_str).unwrap_or_default();

            let db = req
                .extensions()
@@ -65,9 +65,9 @@

            req.extensions_mut().unwrap().insert(user);

            let res: Response<ResBody> = inner.call(req.try_into_request().unwrap()).await?;
            let response: Response<ResBody> = inner.call(req.try_into_request().unwrap()).await?;

            Ok(res)
            Ok(response)
        })
    }
}
diff --git a/chartered-web/src/endpoints/cargo_api/publish.rs b/chartered-web/src/endpoints/cargo_api/publish.rs
index 18d677c..8c3f840 100644
--- a/chartered-web/src/endpoints/cargo_api/publish.rs
+++ a/chartered-web/src/endpoints/cargo_api/publish.rs
@@ -29,16 +29,22 @@

    let file_ref = chartered_fs::Local.write(crate_bytes).await.unwrap();

    chartered_db::crates::publish_crate(
    let mut response = PublishCrateResponse::default();

    if let Err(e) = chartered_db::crates::publish_crate(
        db,
        metadata.name.to_string(),
        metadata.vers.to_string(),
        file_ref,
        hex::encode(Sha256::digest(crate_bytes)),
    )
    .await;
    .await
    {
        // todo: should this be a normal http error?
        response.warnings.other.push(e.to_string());
    }

    axum::response::Json(PublishCrateResponse::default())
    axum::response::Json(response)
}

fn parse(body: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8])> {
@@ -47,7 +53,7 @@

    let u32_from_le_bytes =
        |b: &[u8]| Ok::<_, TryFromSliceError>(u32::from_le_bytes(b.try_into()?));
    let mut read_u32 = map_res(take(4usize), u32_from_le_bytes);
    let mut read_u32 = map_res(take(4_usize), u32_from_le_bytes);

    let (rest, metadata_length) = read_u32(body)?;
    let (rest, metadata_bytes) = take(metadata_length)(rest)?;