Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce warning topbar menu, allow following crates to see warnings for #2471

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[*.{rs,html,js}]
[*.{rs,html,js,scss}]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ aws-smithy-runtime = {version = "1.0.1", features = ["client", "test-util"]}
aws-smithy-http = "0.60.0"
indoc = "2.0.0"

[profile.dev.package.sqlx-macros]
[profile.dev.package."*"]
opt-level = 2
debug = "line-tables-only"

[build-dependencies]
time = "0.3"
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ services:
build:
context: .
dockerfile: ./dockerfiles/Dockerfile
args:
PROFILE: dev
PROFILE_DIR: debug
platform: "linux/amd64"
depends_on:
- db
Expand All @@ -16,6 +19,7 @@ services:
- "/var/run/docker.sock:/var/run/docker.sock"
- ".rustwide-docker:/opt/docsrs/rustwide"
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
- "./static:/opt/docsrs/static:ro"
environment:
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
Expand Down
19 changes: 14 additions & 5 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ FROM ubuntu:22.04 AS build
# Install packaged dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
build-essential git curl cmake gcc g++ pkg-config libmagic-dev \
libssl-dev zlib1g-dev ca-certificates
libssl-dev zlib1g-dev ca-certificates mold clang

# Install the stable toolchain with rustup
RUN curl https://sh.rustup.rs >/tmp/rustup-init && \
chmod +x /tmp/rustup-init && \
/tmp/rustup-init -y --no-modify-path --default-toolchain stable --profile minimal
ENV PATH=/root/.cargo/bin:$PATH

# Configure linking to use mold instead for speed (need to use clang because gcc
# is too old on this image)
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS=-Clink-arg=-fuse-ld=mold

# Build the dependencies in a separate step to avoid rebuilding all of them
# every time the source code changes. This takes advantage of Docker's layer
# caching, and it works by copying the Cargo.{toml,lock} with dummy source code
Expand All @@ -36,7 +41,9 @@ RUN mkdir -p src/bin && \
echo "fn main() {}" > build.rs

RUN cargo fetch
RUN cargo build --release

ARG PROFILE=release
RUN cargo build --profile=$PROFILE

# Dependencies are now cached, copy the actual source code and do another full
# build. The touch on all the .rs files is needed, otherwise cargo assumes the
Expand All @@ -54,7 +61,7 @@ COPY assets assets/
COPY .sqlx .sqlx/
COPY migrations migrations/

RUN cargo build --release
RUN cargo build --profile=$PROFILE

######################
# Web server stage #
Expand All @@ -69,7 +76,8 @@ RUN apt-get update \
tini \
&& rm -rf /var/lib/apt/lists/*

COPY --from=build /build/target/release/cratesfyi /usr/local/bin
ARG PROFILE_DIR=release
COPY --from=build /build/target/$PROFILE_DIR/cratesfyi /usr/local/bin
COPY static /srv/docsrs/static
COPY templates /srv/docsrs/templates
COPY vendor /srv/docsrs/vendor
Expand All @@ -96,7 +104,8 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \

RUN mkdir -p /opt/docsrs/prefix

COPY --from=build /build/target/release/cratesfyi /usr/local/bin
ARG PROFILE_DIR=release
COPY --from=build /build/target/$PROFILE_DIR/cratesfyi /usr/local/bin
COPY static /opt/docsrs/static
COPY templates /opt/docsrs/templates
COPY dockerfiles/entrypoint.sh /opt/docsrs/
Expand Down
120 changes: 120 additions & 0 deletions static/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
(function() {
function load(key, def = null) {
const value = window.localStorage.getItem(key)
if (value) {
try {
return JSON.parse(value)
} catch (ex) {
console.error(`Failed loading ${key} from local storage`, ex)
return def
}
} else {
return def
}
}

function store(key, value) {
window.localStorage.setItem(key, JSON.stringify(value))
}

function create(tagName, attrs = {}, children = [], listeners = {}) {
const el = document.createElement(tagName)
for (const key of Object.keys(attrs)) {
if (typeof attrs[key] === 'object') {
for (const subkey of Object.keys(attrs[key])) {
el[key].setProperty(subkey, attrs[key][subkey])
}
} else {
el.setAttribute(key, attrs[key])
}
}
el.append(...children)
for (const key of Object.keys(listeners)) {
el.addEventListener(key, listeners[key])
}
return el
}


if (!load('docs-rs-warnings-enabled', false)) {
return
}

const parentEl = document.getElementById('warnings-menu-parent')
parentEl.removeAttribute('hidden')

const current = JSON.parse(document.getElementById("crate-metadata")?.innerText || null)

const menuEl = document.getElementById('warnings-menu')

const followed = load('docs-rs-warnings-followed', [])

function update() {
const children = []

if (followed.length > 0) {
children.push(
create('div', { class: 'pure-g' }, [
create('div', { class: 'pure-u-1' }, [
create('ul', { class: 'pure-menu-list', style: { width: '100%' } }, [
create('li', { class: 'pure-menu-heading' }, [
create('b', {}, ['Followed crates']),
]),
...followed.map(name => (
create('li', { class: 'pure-menu-item followed' }, [
create('a', { class: 'pure-menu-link', href: `/${name}` }, [
name,
]),
create('a', { class: 'pure-menu-link remove', href: '#' }, ['🗙'], {
click: (ev) => {
const index = followed.indexOf(name)
followed.splice(index, 1)
store('docs-rs-warnings-followed', followed)
update()
},
}),
])
)),
]),
]),
]),
)
}

if (current && !followed.includes(current.name)) {
children.push(
create('div', { class: 'pure-g' }, [
create('div', { class: 'pure-u-1' }, [
create('ul', { class: 'pure-menu-list', style: { width: '100%' } }, [
create('li', { class: 'pure-menu-item' }, [
create('a', { class: 'pure-menu-link', href: '#' }, [
"Follow ",
create('b', {}, [current.name]),
], {
click: () => {
const index = followed.findIndex((name) => name > current.name)
if (index >= 0) {
followed.splice(index, 0, current.name)
} else {
followed.push(current.name)
}
store('docs-rs-warnings-followed', followed)
update()
}
}),
]),
]),
]),
]),
)
}

for (const child of children.slice(0, -1)) {
child.classList.add('menu-item-divided')
}

menuEl.replaceChildren(...children)
}

update()
})()
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<script defer type="text/javascript" nonce="{{ csp_nonce }}" src="/-/static/menu.js?{{ docsrs_version() | slugify }}"></script>
<script defer type="text/javascript" nonce="{{ csp_nonce }}" src="/-/static/index.js?{{ docsrs_version() | slugify }}"></script>
<script defer type="text/javascript" nonce="{{ csp_nonce }}" src="/-/static/warnings.js?{{ docsrs_version() | slugify }}"></script>
</head>

<body class="{% block body_classes %}{% endblock body_classes %}">
Expand Down
2 changes: 2 additions & 0 deletions templates/header/topbar_end.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
{# The global alert, if there is one #}
{% include "header/global_alert.html" -%}

{% include "header/warnings.html" -%}

<ul class="pure-menu-list">
{#
The Rust dropdown menu
Expand Down
7 changes: 7 additions & 0 deletions templates/header/warnings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul id="warnings-menu-parent" class="pure-menu-list" hidden>
<li class="pure-menu-item pure-menu-has-children">
<a href="#" class="pure-menu-link" aria-label="Warnings">Warnings <span id="warnings-count"></span></a>
<div id="warnings-menu" class="pure-menu-children">
</div>
</li>
</ul>
1 change: 1 addition & 0 deletions templates/rustdoc/body.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script async src="/-/static/menu.js?{{ docsrs_version() | slugify }}"></script>
<script async src="/-/static/index.js?{{ docsrs_version() | slugify }}"></script>
<script async src="/-/static/warnings.js?{{ docsrs_version() | slugify }}"></script>
{# see comment in ../storage-change-detection.html for details #}
<iframe src="/-/storage-change-detection.html" width="0" height="0" style="display: none"></iframe>
16 changes: 16 additions & 0 deletions templates/style/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ div.nav-container {
}
}
}

#warnings-menu {
.followed {
display: flex;
align-items: center;
:first-child {
flex: auto;
}
}
.remove {
padding: 0 .5em;
&:hover {
color: var(--color-error);
}
}
}
}

#nav-search, #nav-sort {
Expand Down
Loading