Skip to content

Commit

Permalink
Fix #123: remove one unused error variant and make types crate public
Browse files Browse the repository at this point in the history
  • Loading branch information
matze committed Feb 25, 2025
1 parent e1edb4b commit 8dc09f2
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::highlight::Theme;

/// An asset associated with a MIME type.
#[derive(Clone)]
pub struct Asset {
pub(crate) struct Asset {
/// Route that this will be served under.
pub route: String,
/// MIME type of this asset determined for the `ContentType` response header.
Expand All @@ -22,7 +22,7 @@ pub struct Asset {

/// Asset kind.
#[derive(Copy, Clone)]
pub enum Kind {
pub(crate) enum Kind {
Css,
Js,
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Asset {
}

/// Collection of light and dark CSS and main UI style CSS derived from them.
pub struct Css {
pub(crate) struct Css {
/// Main UI CSS stylesheet.
pub style: Asset,
/// Light theme colors.
Expand Down
4 changes: 2 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use std::sync::{Arc, Mutex};

/// Cache based on identifier and format.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Key {
pub(crate) struct Key {
pub id: Id,
pub ext: String,
}

/// Stores formatted HTML.
#[derive(Clone)]
pub struct Cache {
pub(crate) struct Cache {
inner: Arc<Mutex<SizedCache<Key, Html>>>,
}

Expand Down
6 changes: 3 additions & 3 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ static CONFIG: LazyLock<argon2::Config> = LazyLock::new(|| argon2::Config {
static SALT: LazyLock<String> = LazyLock::new(env::password_hash_salt);

/// Encrypted data item.
pub struct Encrypted {
pub(crate) struct Encrypted {
/// Encrypted ciphertext.
pub ciphertext: Vec<u8>,
/// Nonce used for encryption.
pub nonce: Vec<u8>,
}

#[derive(Clone)]
pub struct Password(Vec<u8>);
pub(crate) struct Password(Vec<u8>);

/// Plaintext bytes to be encrypted.
pub struct Plaintext(Vec<u8>);
pub(crate) struct Plaintext(Vec<u8>);

impl From<Vec<u8>> for Password {
fn from(value: Vec<u8>) -> Self {
Expand Down
24 changes: 12 additions & 12 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ static MIGRATIONS: LazyLock<Migrations> = LazyLock::new(|| {

/// Our main database and integrated cache.
#[derive(Clone)]
pub struct Database {
pub(crate) struct Database {
conn: Arc<Mutex<Connection>>,
}

/// Database opening modes
#[derive(Debug)]
pub enum Open {
pub(crate) enum Open {
/// Open in-memory database that is wiped after reload
Memory,
/// Open database from given path
Path(PathBuf),
}

/// Module with types for insertion.
pub mod write {
pub(crate) mod write {
use crate::crypto::{Encrypted, Password, Plaintext};
use crate::errors::Error;
use async_compression::tokio::bufread::ZstdEncoder;
Expand All @@ -74,7 +74,7 @@ pub mod write {

/// An uncompressed entry to be inserted into the database.
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Entry {
pub(crate) struct Entry {
/// Content
pub text: String,
/// File extension
Expand All @@ -92,15 +92,15 @@ pub mod write {
}

/// A compressed entry to be inserted.
pub struct CompressedEntry {
pub(crate) struct CompressedEntry {
/// Original data
entry: Entry,
/// Compressed data
data: Vec<u8>,
}

/// An entry that might be encrypted.
pub struct DatabaseEntry {
pub(crate) struct DatabaseEntry {
/// Original data
pub entry: Entry,
/// Compressed and potentially encrypted data
Expand Down Expand Up @@ -147,7 +147,7 @@ pub mod write {
}

/// Module with types for reading from the database.
pub mod read {
pub(crate) mod read {
use crate::crypto::{Encrypted, Password};
use crate::errors::Error;
use async_compression::tokio::bufread::ZstdDecoder;
Expand All @@ -156,7 +156,7 @@ pub mod read {

/// A raw entry as read from the database.
#[derive(Debug)]
pub struct DatabaseEntry {
pub(crate) struct DatabaseEntry {
/// Compressed and potentially encrypted data
pub data: Vec<u8>,
/// Entry is expired
Expand All @@ -173,7 +173,7 @@ pub mod read {

/// Potentially decrypted but still compressed entry
#[derive(Debug)]
pub struct CompressedReadEntry {
pub(crate) struct CompressedReadEntry {
/// Compressed data
data: Vec<u8>,
/// Entry must be deleted
Expand All @@ -186,7 +186,7 @@ pub mod read {

/// Uncompressed entry
#[derive(Debug)]
pub struct UmcompressedEntry {
pub(crate) struct UmcompressedEntry {
/// Content
pub text: String,
/// Entry must be deleted
Expand All @@ -199,7 +199,7 @@ pub mod read {

/// Uncompressed, decrypted data read from the database.
#[derive(Debug)]
pub struct Data {
pub(crate) struct Data {
/// Content
pub text: String,
/// User identifier that inserted the entry
Expand All @@ -210,7 +210,7 @@ pub mod read {

/// Potentially deleted or non-existent expired entry.
#[derive(Debug)]
pub enum Entry {
pub(crate) enum Entry {
/// Entry found and still available.
Regular(Data),
/// Entry burned.
Expand Down
2 changes: 1 addition & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const VAR_THEME: &str = "WASTEBIN_THEME";
const VAR_PASSWORD_SALT: &str = "WASTEBIN_PASSWORD_SALT";

#[derive(thiserror::Error, Debug)]
pub enum Error {
pub(crate) enum Error {
#[error("failed to parse {VAR_CACHE_SIZE}, expected number of elements: {0}")]
CacheSize(ParseIntError),
#[error("failed to parse {VAR_DATABASE_PATH}, contains non-Unicode data")]
Expand Down
11 changes: 4 additions & 7 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;
use std::num::TryFromIntError;

#[derive(thiserror::Error, Debug)]
pub enum Error {
pub(crate) enum Error {
#[error("axum http error: {0}")]
Axum(#[from] axum::http::Error),
#[error("not allowed to delete")]
Expand Down Expand Up @@ -33,8 +33,6 @@ pub enum Error {
CookieParsing(String),
#[error("could not generate QR code: {0}")]
QrCode(#[from] qrcodegen::DataTooLong),
#[error("could not find Host header to generate QR code URL")]
NoHost,
#[error("could not parse URL: {0}")]
UrlParsing(#[from] url::ParseError),
#[error("argon2 error: {0}")]
Expand All @@ -48,19 +46,18 @@ pub enum Error {
}

#[derive(Serialize)]
pub struct JsonError {
pub(crate) struct JsonError {
pub message: String,
}

/// Response carrying a status code and the error message as JSON.
pub type JsonErrorResponse = (StatusCode, Json<JsonError>);
pub(crate) type JsonErrorResponse = (StatusCode, Json<JsonError>);

impl From<Error> for StatusCode {
fn from(err: Error) -> Self {
match err {
Error::NotFound => StatusCode::NOT_FOUND,
Error::NoHost
| Error::IllegalCharacters
Error::IllegalCharacters
| Error::WrongSize
| Error::UrlParsing(_)
| Error::NoPassword
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;

/// Theme extracted from the `pref` cookie.
#[derive(Debug, Deserialize, Clone)]
pub enum Theme {
pub(crate) enum Theme {
#[serde(rename = "dark")]
Dark,
#[serde(rename = "light")]
Expand All @@ -15,14 +15,14 @@ pub enum Theme {

/// Theme preference for use in shared [`axum::extract::Query`]'s.
#[derive(Debug, Deserialize)]
pub struct Preference {
pub(crate) struct Preference {
pub pref: Theme,
}

pub struct Password(pub crypto::Password);
pub(crate) struct Password(pub crypto::Password);

/// Password header to encrypt a paste.
pub const PASSWORD_HEADER_NAME: http::HeaderName =
pub(crate) const PASSWORD_HEADER_NAME: http::HeaderName =
http::HeaderName::from_static("wastebin-password");

impl std::fmt::Display for Theme {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/html/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn get(
/// Burn page shown if "burn-after-reading" was selected during insertion.
#[derive(Template)]
#[template(path = "burn.html", escape = "none")]
pub struct Burn {
pub(crate) struct Burn {
page: Page,
key: Key,
code: qrcodegen::QrCode,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/html/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn get(
/// Index page displaying a form for paste insertion and a selection box for languages.
#[derive(Template)]
#[template(path = "index.html")]
pub struct Index {
pub(crate) struct Index {
page: Page,
theme: Option<Theme>,
highlighter: Highlighter,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use axum::http::StatusCode;
/// Error page showing a message.
#[derive(Template)]
#[template(path = "error.html")]
pub struct Error {
pub(crate) struct Error {
pub page: Page,
pub theme: Option<Theme>,
pub description: String,
Expand All @@ -20,14 +20,14 @@ pub struct Error {
/// Page showing password input.
#[derive(Template)]
#[template(path = "encrypted.html")]
pub struct PasswordInput {
pub(crate) struct PasswordInput {
pub page: Page,
pub theme: Option<Theme>,
pub id: String,
}

/// Error response carrying a status code and the page itself.
pub type ErrorResponse = (StatusCode, Error);
pub(crate) type ErrorResponse = (StatusCode, Error);

/// Create an error response from `error` consisting of [`StatusCode`] derive from `error` as well
/// as a rendered page with a description.
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/html/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use axum_extra::extract::SignedCookieJar;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct PasswordForm {
pub(crate) struct PasswordForm {
password: String,
}

/// Paste view showing the formatted paste.
#[derive(Template)]
#[template(path = "formatted.html")]
pub struct Paste {
pub(crate) struct Paste {
page: Page,
key: Key,
theme: Option<Theme>,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/html/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn get(
/// Paste view showing the formatted paste as well as a bunch of links.
#[derive(Template)]
#[template(path = "qr.html", escape = "none")]
pub struct Qr {
pub(crate) struct Qr {
page: Page,
theme: Option<Theme>,
key: Key,
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/insert/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use std::num::NonZeroU32;

#[derive(Debug, Serialize, Deserialize)]
pub struct Entry {
pub(crate) struct Entry {
pub text: String,
pub extension: Option<String>,
pub expires: Option<NonZeroU32>,
Expand All @@ -18,7 +18,7 @@ pub struct Entry {
}

#[derive(Deserialize, Serialize)]
pub struct RedirectResponse {
pub(crate) struct RedirectResponse {
pub path: String,
}

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/insert/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use std::num::NonZeroU32;

#[derive(Debug, Serialize, Deserialize)]
pub struct Entry {
pub(crate) struct Entry {
pub text: String,
pub extension: Option<String>,
pub expires: Option<String>,
Expand Down
6 changes: 3 additions & 3 deletions src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const HIGHLIGHT_LINE_LENGTH_CUTOFF: usize = 2048;

/// Supported themes.
#[derive(Copy, Clone)]
pub enum Theme {
pub(crate) enum Theme {
Ayu,
Base16Ocean,
Coldark,
Expand All @@ -20,10 +20,10 @@ pub enum Theme {
}

#[derive(Clone)]
pub struct Html(String);
pub(crate) struct Html(String);

#[derive(Clone)]
pub struct Highlighter {
pub(crate) struct Highlighter {
syntax_set: SyntaxSet,
pub syntaxes: Vec<SyntaxReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const CHAR_TABLE: &[char; 64] = &[

/// Represents a 32-bit integer either numerically or mapped to a 6 character string.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Id {
pub(crate) struct Id {
n: u32,
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ mod page;
mod test_helpers;

/// Reference counted [`page::Page`] wrapper.
pub type Page = Arc<page::Page>;
pub(crate) type Page = Arc<page::Page>;

/// Reference counted [`highlight::Highlighter`] wrapper.
pub type Highlighter = Arc<highlight::Highlighter>;
pub(crate) type Highlighter = Arc<highlight::Highlighter>;

#[derive(Clone)]
pub struct AppState {
pub(crate) struct AppState {
db: Database,
cache: Cache,
key: Key,
Expand Down
Loading

0 comments on commit 8dc09f2

Please sign in to comment.