diff --git a/Cargo.lock b/Cargo.lock index 14f16158e2..245a297fbc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1994,6 +1994,14 @@ dependencies = [ "chrono", ] +[[package]] +name = "dimensions" +version = "0.0.1" +dependencies = [ + "errors", + "my-workspace-hack", +] + [[package]] name = "displaydoc" version = "0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 5606bde5d2..5f4feb8c32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ members = [ "crates/lib/deprecated/config", # TODO(bt, 2023-02-13): Deprecate or repurpose "crates/lib/deprecated/container_common", # TODO(bt, 2023-02-13): Pull apart and rehome pieces "crates/lib/deprecated/http_server_common", # TODO(bt, 2023-03-20): Move useful pieces to actix_helpers + "crates/lib/dimensions", "crates/lib/easyenv", # NB: Vendored version of https://github.com/echelon/easyenv.rs that we'll improve. "crates/lib/elasticsearch_schema", "crates/lib/email_sender", diff --git a/crates/_types/enums/src/no_table/mod.rs b/crates/_types/enums/src/no_table/mod.rs index 7bb3fd4399..977ace7de9 100644 --- a/crates/_types/enums/src/no_table/mod.rs +++ b/crates/_types/enums/src/no_table/mod.rs @@ -1,2 +1,3 @@ pub mod premium_product; -pub mod style_transfer; \ No newline at end of file +pub mod style_transfer; +pub mod video_dimensions; \ No newline at end of file diff --git a/crates/_types/enums/src/no_table/video_dimensions/mod.rs b/crates/_types/enums/src/no_table/video_dimensions/mod.rs new file mode 100644 index 0000000000..6f8ed8967c --- /dev/null +++ b/crates/_types/enums/src/no_table/video_dimensions/mod.rs @@ -0,0 +1 @@ +mod video_dimensions; \ No newline at end of file diff --git a/crates/_types/enums/src/no_table/video_dimensions/video_dimensions.rs b/crates/_types/enums/src/no_table/video_dimensions/video_dimensions.rs new file mode 100644 index 0000000000..6b96ee47fc --- /dev/null +++ b/crates/_types/enums/src/no_table/video_dimensions/video_dimensions.rs @@ -0,0 +1,117 @@ +use std::collections::BTreeSet; + +#[cfg(test)] +use strum::EnumCount; +#[cfg(test)] +use strum::EnumIter; +use utoipa::ToSchema; + +/// This enum is not backed by a particular database table. +/// This is used to determine the video generation size. +#[cfg_attr(test, derive(EnumIter, EnumCount))] +#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, ToSchema)] +#[serde(rename_all = "snake_case")] +pub enum VideoDimensions { + Landscape, + Portrait, + Square, +} + +// TODO(bt, 2022-12-21): This desperately needs MySQL integration tests! +impl_enum_display_and_debug_using_to_str!(VideoDimensions); +//impl_mysql_enum_coders!(VideoDimensions); +//impl_mysql_from_row!(VideoDimensions); + +/// NB: Legacy API for older code. +impl VideoDimensions { + pub fn to_str(&self) -> &'static str { + match self { + Self::Landscape => "landscape", + Self::Portrait => "portrait", + Self::Square => "square", + } + } + + pub fn from_str(value: &str) -> Result { + match value { + "landscape" => Ok(Self::Landscape), + "portrait" => Ok(Self::Portrait), + "square" => Ok(Self::Square), + _ => Err(format!("Unknown VideoDimensions: {}", value)), + } + } + + pub fn all_variants() -> BTreeSet { + // NB: BTreeSet is sorted + // NB: BTreeSet::from() isn't const, but not worth using LazyStatic, etc. + BTreeSet::from([ + Self::Landscape, + Self::Portrait, + Self::Square, + ]) + } +} + +#[cfg(test)] +mod tests { + use crate::no_table::video_dimensions::video_dimensions::VideoDimensions; + use crate::test_helpers::assert_serialization; + + mod serde { + use super::*; + + #[test] + fn test_serialization() { + assert_serialization(VideoDimensions::Landscape, "landscape"); + assert_serialization(VideoDimensions::Portrait, "portrait"); + assert_serialization(VideoDimensions::Square, "square"); + } + + mod impl_methods { + use super::*; + + #[test] + fn to_str() { + assert_eq!(VideoDimensions::Landscape.to_str(), "landscape"); + assert_eq!(VideoDimensions::Portrait.to_str(), "portrait"); + assert_eq!(VideoDimensions::Square.to_str(), "square"); + } + + #[test] + fn from_str() { + assert_eq!(VideoDimensions::from_str("landscape").unwrap(), VideoDimensions::Landscape); + assert_eq!(VideoDimensions::from_str("portrait").unwrap(), VideoDimensions::Portrait); + assert_eq!(VideoDimensions::from_str("square").unwrap(), VideoDimensions::Square); + } + } + + mod manual_variant_checks { + use super::*; + + #[test] + fn all_variants() { + let variants = VideoDimensions::all_variants(); + assert_eq!(variants.len(), 3); + } + } + + mod mechanical_checks { + use super::*; + + #[test] + fn variant_length() { + use strum::IntoEnumIterator; + assert_eq!(VideoDimensions::all_variants().len(), VideoDimensions::iter().len()); + } + + #[test] + fn round_trip() { + for variant in VideoDimensions::all_variants() { + assert_eq!(variant, VideoDimensions::from_str(variant.to_str()).unwrap()); + assert_eq!(variant, VideoDimensions::from_str(&format!("{}", variant)).unwrap()); + assert_eq!(variant, VideoDimensions::from_str(&format!("{:?}", variant)).unwrap()); + } + } + } + } +} diff --git a/crates/lib/dimensions/Cargo.toml b/crates/lib/dimensions/Cargo.toml new file mode 100644 index 0000000000..2f3903d10c --- /dev/null +++ b/crates/lib/dimensions/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "dimensions" +edition = "2018" +version = "0.0.1" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] +publish = false + +[lib] +# NB: Public "dimensions" crate is not relevant / abandoned, so we're fine to use this internally. +name = "dimensions" +path = "src/lib.rs" + +[package.metadata.cargo-udeps.ignore] +normal = ["my-workspace-hack"] + +[dependencies] +# Workspace hack (faster compile times) +my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } + +# Internal +errors = { path = "../errors" } + +# External +# (None Yet) + +[dev-dependencies] +# None yet diff --git a/crates/lib/dimensions/src/lib.rs b/crates/lib/dimensions/src/lib.rs new file mode 100644 index 0000000000..1b83d9f365 --- /dev/null +++ b/crates/lib/dimensions/src/lib.rs @@ -0,0 +1,20 @@ +//! dimensions +//! +//! The purpose of this library is to specify common dimensions for video and +//! to do aspect ratio calculations in a reusable manner. +//! + +// Never allow these +#![forbid(private_bounds)] +#![forbid(private_interfaces)] +#![forbid(unused_must_use)] // NB: It's unsafe to not close/check some things + +// Okay to toggle +#![forbid(unreachable_patterns)] +#![forbid(unused_imports)] +#![forbid(unused_mut)] +#![forbid(unused_variables)] + +// Always allow +#![allow(dead_code)] +#![allow(non_snake_case)] diff --git a/crates/lib/password/Cargo.toml b/crates/lib/password/Cargo.toml index d24c3cb5c4..d152ef798f 100644 --- a/crates/lib/password/Cargo.toml +++ b/crates/lib/password/Cargo.toml @@ -17,12 +17,12 @@ path = "src/lib.rs" normal = ["my-workspace-hack"] [dependencies] -# Internal -errors = { path = "../errors" } - # Workspace hack (faster compile times) my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } +# Internal +errors = { path = "../errors" } + # External bcrypt = "0.15" # password hashes #hmac = "0.10.0" # jwt signing