Skip to content

Commit 00564ed

Browse files
committed
add krate-name validation to rustdoc parameter extraction
1 parent 616d9bb commit 00564ed

23 files changed

+448
-417
lines changed

.sqlx/query-1be3826465e3cd76738b3bfc9d46df14795d1f18830783080bf3a58b909ab16a.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-543694448dbb43af41dc18c29017c5b211f45f7722811fdcabea26f796d4514b.json

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-af663241b6e7d77402847d57ef9a1d69412ea5af6d0b6562644fe913bac2e1df.json

Lines changed: 0 additions & 64 deletions
This file was deleted.

.sqlx/query-bdddad099e891bb45ba3703d7144160056d6cb620c55be459ead0f95c3523035.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

.sqlx/query-db8654a53a9914ebfaa15a9bcc0af66fc2f46a8b5caa5473278123403ebbd613.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

.sqlx/query-e1a95bfd43982d86a56cf542fde99a6e5a42e56aedf068a3acf39f923eb32ade.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/db/types/krate_name.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use sqlx::{
66
Decode, Encode, Postgres,
77
encode::IsNull,
88
error::BoxDynError,
9-
postgres::{PgArgumentBuffer, PgTypeInfo, PgValueRef},
9+
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef},
1010
prelude::*,
1111
};
12-
use std::{io::Write, str::FromStr};
12+
use std::{borrow::Cow, io::Write, str::FromStr};
1313

1414
/// validated crate name
1515
///
@@ -31,7 +31,14 @@ use std::{io::Write, str::FromStr};
3131
SerializeDisplay,
3232
bincode::Encode,
3333
)]
34-
pub struct KrateName(String);
34+
pub struct KrateName(Cow<'static, str>);
35+
36+
impl KrateName {
37+
#[cfg(test)]
38+
pub(crate) const fn from_static(s: &'static str) -> Self {
39+
KrateName(Cow::Borrowed(s))
40+
}
41+
}
3542

3643
impl From<&KrateName> for KrateName {
3744
fn from(krate_name: &KrateName) -> Self {
@@ -44,7 +51,7 @@ impl FromStr for KrateName {
4451

4552
fn from_str(s: &str) -> Result<Self, Self::Err> {
4653
validate_crate_name("crate", s)?;
47-
Ok(KrateName(s.to_string()))
54+
Ok(KrateName(Cow::Owned(s.to_string())))
4855
}
4956
}
5057

@@ -67,8 +74,21 @@ impl<'q> Encode<'q, Postgres> for KrateName {
6774

6875
impl<'r> Decode<'r, Postgres> for KrateName {
6976
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
77+
// future improvement: we could also avoid the allocation here
78+
// and return a Cow::Borrowed while reading from the DB.
79+
// But this would mean the lifetime leaks into the whole codebase.
7080
let s: &str = Decode::<Postgres>::decode(value)?;
71-
Ok(Self(s.parse()?))
81+
Ok(Self(Cow::Owned(s.parse()?)))
82+
}
83+
}
84+
85+
impl PgHasArrayType for KrateName {
86+
fn array_type_info() -> PgTypeInfo {
87+
<&str as PgHasArrayType>::array_type_info()
88+
}
89+
90+
fn array_compatible(ty: &PgTypeInfo) -> bool {
91+
<&str as PgHasArrayType>::array_compatible(ty)
7292
}
7393
}
7494

src/storage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use tokio::{
4545
runtime,
4646
sync::Mutex,
4747
};
48-
use tracing::{debug, error, info_span, instrument, trace, warn};
48+
use tracing::{debug, info_span, instrument, trace, warn};
4949
use walkdir::WalkDir;
5050

5151
const ARCHIVE_INDEX_FILE_EXTENSION: &str = "index";

src/utils/cargo_metadata.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{db::types::version::Version, error::Result};
1+
use crate::db::types::krate_name::KrateName;
2+
use crate::web::ReqVersion;
3+
use crate::{db::types::version::Version, error::Result, web::extractors::rustdoc::RustdocParams};
24
use anyhow::{Context, bail};
35
use rustwide::{Toolchain, Workspace, cmd::Command};
46
use semver::VersionReq;
@@ -135,6 +137,19 @@ pub(crate) struct Dependency {
135137
pub(crate) optional: bool,
136138
}
137139

140+
impl Dependency {
141+
pub(crate) fn rustdoc_params(&self) -> RustdocParams {
142+
RustdocParams::new(
143+
// I validated in the database, which makes me assume that renames are
144+
// handled before storing the deps into the column.
145+
self.name
146+
.parse::<KrateName>()
147+
.expect("we validated that the dep name is always a valid KrateName"),
148+
)
149+
.with_req_version(ReqVersion::Semver(self.req.clone()))
150+
}
151+
}
152+
138153
impl bincode::Encode for Dependency {
139154
fn encode<E: bincode::enc::Encoder>(
140155
&self,

src/utils/html.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use lol_html::{element, errors::RewritingError};
1818
use std::sync::Arc;
1919
use tokio::{io::AsyncRead, task::JoinHandle};
2020
use tokio_util::io::ReaderStream;
21-
use tracing::{Span, error, instrument};
21+
use tracing::{Span, instrument};
2222
use tracing_futures::Instrument as _;
2323

2424
const CHANNEL_SIZE: usize = 64;

0 commit comments

Comments
 (0)