diff --git a/Cargo.toml b/Cargo.toml index 0ab8eda..7f032b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mongodb-macro" -version = "0.3.0" +version = "1.0.0" edition = "2021" description = "MongoDB Macro is a crate with macros for quickly creating structures to work with mongodb" keywords = [ "mongodb", "macro", "config", "clap" ] diff --git a/README.md b/README.md index 932a4ea..e48a695 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,27 @@ Make sure you also add to the project: > clap = { version = "*", features = ["derive", "env"] } ## Usage +### Macro: Collection +```rust + +use mongodb::bson::Bson; + +// env DB_URL should contain a link to the mongodb url +// env DB_NAME should contain the database name +// env COLLECTION_NAME should contain the collection name +mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts); +// or with a specified env +// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME")); + +async fn main() -> std::io::Result<()> { + + let factory = CollectionFactory::parse(); + + let collection = factory.create::().await.expect("failed to connect"); + + ... +} +``` ### Macro: Database ```rust @@ -35,7 +56,7 @@ Make sure you also add to the project: use mongodb::bson::Bson; // env DB_URL should contain a link to the mongodb url -// and DB_NAME should contain the database name +// env DB_NAME should contain the database name mongodb_macro::database!(DbFactory; DbFactoryOpts); // or with a specified env // mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME")); @@ -102,6 +123,6 @@ async fn main() -> std::io::Result<()> { } ``` -Current version: 0.3.0 +Current version: 1.0.0 License: MIT diff --git a/src/collection.rs b/src/collection.rs new file mode 100644 index 0000000..3e17d73 --- /dev/null +++ b/src/collection.rs @@ -0,0 +1,100 @@ + + +/// Creates a new configuration structure to initialize the MongoDB collection +/// +/// Create a new configuration structure to initialize the MongoDB collection with a standard environment variable +/// +/// ``` +/// mongodb_macro::collection_config!(Opts); +/// +/// fn main() { +/// std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017"); +/// std::env::set_var("DB_NAME", "test"); +/// std::env::set_var("COLLECTION_NAME", "users"); +/// +/// let opts = Opts::parse(); +/// } +/// ``` +/// +/// Create a new configuration structure to initialize the MongoDB collection with the specified environment variable +/// +/// ``` +/// mongodb_macro::collection_config!(Opts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME")); +/// +/// fn main() { +/// std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017"); +/// std::env::set_var("MONGO_DB_NAME", "test"); +/// std::env::set_var("MONGO_COLLECTION_NAME", "users"); +/// +/// let opts = Opts::parse(); +/// } +/// ``` +#[macro_export] +macro_rules! collection_config { + ($opts:ident) => ($crate::config!{$opts}); + + ($opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) => + ($crate::config!{$opts; $db_url, $db_name, $collection_name}); +} + +/// Creates a new factory to create a MongoDB collection +/// +/// Create mongodb collection factory with standard environment variable for database url, database name and collection name +/// +/// ``` +/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts); +/// +/// fn main() { +/// std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017"); +/// std::env::set_var("DB_NAME", "test"); +/// std::env::set_var("COLLECTION_NAME", "users"); +/// +/// let factory = CollectionFactory::parse(); +/// +/// // let collection = factory.create().await.expect("failed to connect"); +/// } +/// ``` +/// +/// Create mongodb collection factory with specified environment variable for database url, database name and collection name +/// +/// ``` +/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME")); +/// +/// fn main() { +/// std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017"); +/// std::env::set_var("MONGO_DB_NAME", "test"); +/// std::env::set_var("MONGO_COLLECTION_NAME", "users"); +/// +/// let factory = CollectionFactory::parse(); +/// +/// // let collection = factory.create().await.expect("failed to connect"); +/// } +/// ``` +#[macro_export] +macro_rules! collection { + ($collection_factory:ident; $opts:ident) => ($crate::collection!{$collection_factory; $opts; ("DB_URL", "DB_NAME", "COLLECTION_NAME")}); + + ($collection_factory:ident; $opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) => { + + $crate::collection_config!($opts; ($db_url, $db_name, $collection_name)); + + pub struct $collection_factory($opts); + + impl $collection_factory { + fn parse() -> Self { + let opts = $opts::parse(); + Self(opts) + } + + pub fn config(&self) -> &$opts { + &self.0 + } + + pub async fn create(&self) -> Result<::mongodb::Collection, ::mongodb::error::Error> { + let client = ::mongodb::Client::with_uri_str(&self.0.db_url).await?; + let db = client.database(&self.0.db_name); + Ok(db.collection::(&self.0.collection_name)) + } + } + }; +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ebea66f..63016d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,27 @@ Make sure you also add to the project: > clap = { version = "*", features = ["derive", "env"] } # Usage +## Macro: Collection +```no_run + +use mongodb::bson::Bson; + +// env DB_URL should contain a link to the mongodb url +// env DB_NAME should contain the database name +// env COLLECTION_NAME should contain the collection name +mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts); +// or with a specified env +// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME")); + +async fn main() -> std::io::Result<()> { + + let factory = CollectionFactory::parse(); + + let collection = factory.create::().await.expect("failed to connect"); + + ... +} +``` ## Macro: Database ```no_run @@ -23,7 +44,7 @@ Make sure you also add to the project: use mongodb::bson::Bson; // env DB_URL should contain a link to the mongodb url -// and DB_NAME should contain the database name +// env DB_NAME should contain the database name mongodb_macro::database!(DbFactory; DbFactoryOpts); // or with a specified env // mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME")); @@ -102,4 +123,8 @@ pub mod client; /// Macros for creating MongoDB database configurations and factories from environment variables #[doc(hidden)] -pub mod database; \ No newline at end of file +pub mod database; + +/// Macros for creating MongoDB collection configurations and factories from environment variables +#[doc(hidden)] +pub mod collection; \ No newline at end of file