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

Add Add #[non_exhaustive] to public structs/enums #209

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ tonic = { workspace = true, optional = true }
# Others
anyhow = { workspace = true }
serde = { workspace = true }
serde_derive = "1.0.184"
serde_with = { version = "3.8.1", features = ["macros", "chrono_0_4"] }
strum = "0.26.2"
strum_macros = "0.26.2"
serde_derive = "1.0.185"
serde_with = { version = "3.0.0", features = ["macros", "chrono_0_4"] }
strum = "0.26.0"
strum_macros = "0.26.0"
itertools = "0.13.0"
serde_json = "1.0.85"
toml = "0.8.0"
Expand Down Expand Up @@ -135,7 +135,7 @@ tokio = { version = "1.28.0", features = ["full"] }
# For CancellationToken
tokio-util = { version = "0.7.10" }
anyhow = "1.0.69"
serde = { version = "1.0.184", features = ["derive"] }
serde = { version = "1.0.185", features = ["derive"] }

[package.metadata.docs.rs]
# Have docs.rs pass `--all-features` to ensure all features have their documentation built.
Expand Down
2 changes: 1 addition & 1 deletion examples/full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ clap = { version = "4.3.0", features = ["derive"] }

# The default `rss-stats` feature has a dependency that currently can't be satisfied (memchr: ~2.3)
rusty-sidekiq = { version = "0.10.5", default-features = false }
serde = { version = "1.0.184", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }

[build-dependencies]
tonic-build = "0.11"
Expand Down
1 change: 1 addition & 0 deletions examples/full/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::app_state::CustomAppContext;
/// as well. Subcommands not listed under the `roadster` subcommand are specific to `full`.
#[derive(Debug, Parser)]
#[command(version, about)]
#[non_exhaustive]
pub struct AppCli {
#[command(subcommand)]
pub command: Option<AppCommand>,
Expand Down
1 change: 1 addition & 0 deletions src/api/cli/roadster/list_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ use clap::Parser;
use serde_derive::Serialize;

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct ListRoutesArgs {}
4 changes: 4 additions & 0 deletions src/api/cli/roadster/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::app::App;
use crate::error::RoadsterResult;

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct MigrateArgs {
#[clap(subcommand)]
pub command: MigrateCommand,
Expand All @@ -33,6 +34,7 @@ where

#[derive(Debug, Subcommand, Serialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum MigrateCommand {
/// Apply pending migrations
Up(UpArgs),
Expand Down Expand Up @@ -81,13 +83,15 @@ where
}

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct UpArgs {
/// The number of pending migration steps to apply.
#[clap(short = 'n', long)]
pub steps: Option<u32>,
}

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct DownArgs {
/// The number of applied migration steps to rollback.
#[clap(short = 'n', long)]
Expand Down
4 changes: 4 additions & 0 deletions src/api/cli/roadster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
/// is matched, Roadster will default to running/serving your application.
#[derive(Debug, Parser, Serialize)]
#[command(version, about)]
#[non_exhaustive]
pub struct RoadsterCli {
/// Specify the environment to use to run the application. This overrides the corresponding
/// environment variable if it's set.
Expand Down Expand Up @@ -89,6 +90,7 @@ where

#[derive(Debug, Subcommand, Serialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum RoadsterCommand {
/// Roadster subcommands. Subcommands provided by Roadster are listed under this subcommand in
/// order to avoid naming conflicts with the consumer's subcommands.
Expand All @@ -114,6 +116,7 @@ where
}

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct RoadsterArgs {
#[command(subcommand)]
pub command: RoadsterSubCommand,
Expand Down Expand Up @@ -167,6 +170,7 @@ where

#[derive(Debug, Subcommand, Serialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum RoadsterSubCommand {
/// List the API routes available in the app. Note: only the routes defined
/// using the `Aide` crate will be included in the output.
Expand Down
1 change: 1 addition & 0 deletions src/api/cli/roadster/open_api_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde_derive::Serialize;
use std::path::PathBuf;

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct OpenApiArgs {
/// The file to write the schema to. If not provided, will write to stdout.
#[clap(short, long, value_name = "FILE", value_hint = clap::ValueHint::FilePath)]
Expand Down
2 changes: 2 additions & 0 deletions src/api/cli/roadster/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::app::App;
use crate::error::RoadsterResult;

#[derive(Debug, Parser, Serialize)]
#[non_exhaustive]
pub struct PrintConfigArgs {
/// Print the config with the specified format.
#[clap(short, long, default_value = "debug")]
Expand All @@ -21,6 +22,7 @@ pub struct PrintConfigArgs {
)]
#[serde(rename_all = "kebab-case", tag = "type")]
#[strum(serialize_all = "kebab-case")]
#[non_exhaustive]
pub enum Format {
Debug,
Json,
Expand Down
24 changes: 21 additions & 3 deletions src/api/http/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn route<S>(context: &AppContext<S>) -> &str {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "open-api", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct HeathCheckResponse {
/// Total latency of checking the health of the app.
pub latency: u128,
Expand All @@ -101,6 +102,7 @@ pub struct HeathCheckResponse {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "open-api", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ResourceHealth {
status: Status,
/// How long it takes to acquire a connection from the pool.
Expand All @@ -114,9 +116,19 @@ pub struct ResourceHealth {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "open-api", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum Status {
Ok,
Err,
Err(ErrorData),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "open-api", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ErrorData {
#[serde(skip_serializing_if = "Option::is_none")]
pub msg: Option<String>,
}

#[instrument(skip_all)]
Expand All @@ -134,7 +146,7 @@ where
let db_status = if ping_db(state.db()).await.is_ok() {
Status::Ok
} else {
Status::Err
Status::Err(ErrorData { msg: None })
};
let db_timer = db_timer.elapsed();
ResourceHealth {
Expand Down Expand Up @@ -178,7 +190,13 @@ async fn redis_health(redis: &sidekiq::RedisPool) -> ResourceHealth {
let redis_timer = Instant::now();
let (redis_status, acquire_conn_latency, ping_latency) = match ping_redis(redis).await {
Ok((a, b)) => (Status::Ok, Some(a.as_millis()), Some(b.as_millis())),
_ => (Status::Err, None, None),
Err(err) => (
Status::Err(ErrorData {
msg: Some(err.to_string()),
}),
None,
None,
),
};
let redis_timer = redis_timer.elapsed();
ResourceHealth {
Expand Down
1 change: 1 addition & 0 deletions src/api/http/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn route<S>(context: &AppContext<S>) -> &str {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "open-api", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PingResponse {}

#[instrument(skip_all)]
Expand Down
1 change: 1 addition & 0 deletions src/app/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use typed_builder::TypedBuilder;
/// [AppConfig][crate::config::app_config::AppConfig] in order to allow the consumer to provide
/// metadata, such as the app version, that is best determined dynamically.
#[derive(Debug, Default, Clone, TypedBuilder)]
#[non_exhaustive]
pub struct AppMetadata {
/// The name of the app. If not provided, Roadster will use the value from
/// the [config][crate::config::app_config::App].
Expand Down
2 changes: 2 additions & 0 deletions src/config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub type CustomConfig = BTreeMap<String, Value>;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct AppConfig {
pub environment: Environment,
#[validate(nested)]
Expand Down Expand Up @@ -175,6 +176,7 @@ impl AppConfig {

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct App {
pub name: String,
/// Shutdown the whole app if an error occurs in one of the app's top-level tasks (API, workers, etc).
Expand Down
3 changes: 3 additions & 0 deletions src/config/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use validator::Validate;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Auth {
#[validate(nested)]
pub jwt: Jwt,
}

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Jwt {
pub secret: String,
#[serde(default)]
Expand All @@ -20,6 +22,7 @@ pub struct Jwt {

#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
#[non_exhaustive]
pub struct JwtClaims {
// Todo: Default to the server URL?
#[serde(default)]
Expand Down
1 change: 1 addition & 0 deletions src/config/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use validator::Validate;
#[serde_as]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Database {
/// This can be overridden with an environment variable, e.g. `ROADSTER.DATABASE.URI=postgres://example:example@example:1234/example_app`
pub uri: Url,
Expand Down
1 change: 1 addition & 0 deletions src/config/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::error::RoadsterResult;
#[cfg_attr(feature = "cli", derive(ValueEnum))]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
#[non_exhaustive]
pub enum Environment {
Development,
Test,
Expand Down
1 change: 1 addition & 0 deletions src/config/service/common/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use validator::Validate;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Address {
pub host: String,
pub port: u32,
Expand Down
1 change: 1 addition & 0 deletions src/config/service/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn default_config() -> config::File<FileSourceString, FileFormat> {

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct GrpcServiceConfig {
#[serde(flatten)]
#[validate(nested)]
Expand Down
2 changes: 2 additions & 0 deletions src/config/service/http/default_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use validator::ValidationError;
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[validate(schema(function = "validate_default_routes"))]
#[non_exhaustive]
pub struct DefaultRoutes {
#[serde(default = "default_true")]
pub default_enable: bool,
Expand Down Expand Up @@ -53,6 +54,7 @@ fn validate_default_routes(

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct DefaultRouteConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enable: Option<bool>,
Expand Down
3 changes: 3 additions & 0 deletions src/config/service/http/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const PRIORITY_LAST: i32 = 10_000;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Initializer {
#[serde(default = "default_true")]
pub default_enable: bool,
Expand Down Expand Up @@ -50,6 +51,7 @@ pub struct Initializer {

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct CommonConfig {
// Optional so we can tell the difference between a consumer explicitly enabling/disabling
// the initializer, vs the initializer being enabled/disabled by default.
Expand All @@ -76,6 +78,7 @@ impl CommonConfig {

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct InitializerConfig<T> {
#[serde(flatten)]
pub common: CommonConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/config/service/http/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const PRIORITY_LAST: i32 = 10_000;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Middleware {
#[serde(default = "default_true")]
pub default_enable: bool,
Expand Down Expand Up @@ -82,6 +83,7 @@ pub struct Middleware {

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct CommonConfig {
// Optional so we can tell the difference between a consumer explicitly enabling/disabling
// the middleware, vs the middleware being enabled/disabled by default.
Expand All @@ -108,6 +110,7 @@ impl CommonConfig {

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct MiddlewareConfig<T> {
#[serde(flatten)]
pub common: CommonConfig,
Expand Down
1 change: 1 addition & 0 deletions src/config/service/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn default_config() -> config::File<FileSourceString, FileFormat> {

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct HttpServiceConfig {
#[serde(flatten)]
#[validate(nested)]
Expand Down
3 changes: 3 additions & 0 deletions src/config/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use validator::Validate;

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct Service {
#[serde(default = "default_true")]
pub default_enable: bool,
Expand All @@ -37,6 +38,7 @@ pub struct Service {

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
#[non_exhaustive]
pub struct CommonConfig {
// Optional so we can tell the difference between a consumer explicitly enabling/disabling
// the service, vs the service being enabled/disabled by default.
Expand All @@ -54,6 +56,7 @@ impl CommonConfig {

#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct ServiceConfig<T: Validate> {
#[serde(flatten, default)]
pub common: CommonConfig,
Expand Down
Loading
Loading