doc: make it clear that ConnectOptions types impl FromStr (#2574)

This commit is contained in:
Austin Bonander 2023-06-30 16:49:24 -07:00 committed by GitHub
parent 3fbc3a3ff2
commit e2ce463af8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 27 deletions

View File

@ -18,6 +18,9 @@ pub use ssl_mode::MySqlSslMode;
/// mysql://[host][/database][?properties] /// mysql://[host][/database][?properties]
/// ``` /// ```
/// ///
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
/// containing a connection URL and then further adjust options if necessary (see example below).
///
/// ## Properties /// ## Properties
/// ///
/// |Parameter|Default|Description| /// |Parameter|Default|Description|
@ -30,13 +33,10 @@ pub use ssl_mode::MySqlSslMode;
/// # Example /// # Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use sqlx_core::error::Error; /// # async fn example() -> sqlx::Result<()> {
/// # use sqlx_core::connection::{Connection, ConnectOptions}; /// use sqlx::{Connection, ConnectOptions};
/// # use sqlx_core::mysql::{MySqlConnectOptions, MySqlConnection, MySqlSslMode}; /// use sqlx::mysql::{MySqlConnectOptions, MySqlConnection, MySqlPool, MySqlSslMode};
/// # ///
/// # fn main() {
/// # #[cfg(feature = "_rt")]
/// # sqlx::__rt::test_block_on(async move {
/// // URL connection string /// // URL connection string
/// let conn = MySqlConnection::connect("mysql://root:password@localhost/db").await?; /// let conn = MySqlConnection::connect("mysql://root:password@localhost/db").await?;
/// ///
@ -47,8 +47,16 @@ pub use ssl_mode::MySqlSslMode;
/// .password("password") /// .password("password")
/// .database("db") /// .database("db")
/// .connect().await?; /// .connect().await?;
/// # Result::<(), Error>::Ok(()) ///
/// # }).unwrap(); /// // Modifying options parsed from a string
/// let mut opts: MySqlConnectOptions = "mysql://root:password@localhost/db".parse()?;
///
/// // Change the log verbosity level for queries.
/// // Information about SQL queries is logged at `DEBUG` level by default.
/// opts.log_statements(log::LevelFilter::Trace);
///
/// let pool = MySqlPool::connect_with(&opts).await?;
/// # Ok(())
/// # } /// # }
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -23,6 +23,9 @@ mod ssl_mode;
/// postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...] /// postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...]
/// ``` /// ```
/// ///
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
/// containing a connection URL and then further adjust options if necessary (see example below).
///
/// ## Parameters /// ## Parameters
/// ///
/// |Parameter|Default|Description| /// |Parameter|Default|Description|
@ -55,13 +58,10 @@ mod ssl_mode;
/// # Example /// # Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use sqlx_core::error::Error; /// use sqlx::{Connection, ConnectOptions};
/// # use sqlx_core::connection::{Connection, ConnectOptions}; /// use sqlx::postgres::{PgConnectOptions, PgConnection, PgPool, PgSslMode};
/// # use sqlx_core::postgres::{PgConnectOptions, PgConnection, PgSslMode}; ///
/// # /// # async fn example() -> sqlx::Result<()> {
/// # fn main() {
/// # #[cfg(feature = "_rt")]
/// # sqlx::__rt::test_block_on(async move {
/// // URL connection string /// // URL connection string
/// let conn = PgConnection::connect("postgres://localhost/mydb").await?; /// let conn = PgConnection::connect("postgres://localhost/mydb").await?;
/// ///
@ -72,9 +72,17 @@ mod ssl_mode;
/// .username("secret-user") /// .username("secret-user")
/// .password("secret-password") /// .password("secret-password")
/// .ssl_mode(PgSslMode::Require) /// .ssl_mode(PgSslMode::Require)
/// .connect().await?; /// .connect()
/// # Result::<(), Error>::Ok(()) /// .await?;
/// # }).unwrap(); ///
/// // Modifying options parsed from a string
/// let mut opts: PgConnectOptions = "postgres://localhost/mydb".parse()?;
///
/// // Change the log verbosity level for queries.
/// // Information about SQL queries is logged at `DEBUG` level by default.
/// opts.log_statements(log::LevelFilter::Trace);
///
/// let pool = PgPool::connect_with(&opts).await?;
/// # } /// # }
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -25,6 +25,9 @@ use sqlx_core::IndexMap;
/// A value of `SqliteConnectOptions` can be parsed from a connection URL, /// A value of `SqliteConnectOptions` can be parsed from a connection URL,
/// as described by [SQLite](https://www.sqlite.org/uri.html). /// as described by [SQLite](https://www.sqlite.org/uri.html).
/// ///
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
/// containing a connection URL and then further adjust options if necessary (see example below).
///
/// | URL | Description | /// | URL | Description |
/// | -- | -- | /// | -- | -- |
/// `sqlite::memory:` | Open an in-memory database. | /// `sqlite::memory:` | Open an in-memory database. |
@ -36,20 +39,17 @@ use sqlx_core::IndexMap;
/// # Example /// # Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use sqlx_core::connection::ConnectOptions; /// # async fn example() -> sqlx::Result<()> {
/// # use sqlx_core::error::Error; /// use sqlx::ConnectOptions;
/// # use sqlx_sqlite::{SqliteConnectOptions, SqliteJournalMode}; /// use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
/// use std::str::FromStr; /// use std::str::FromStr;
/// ///
/// # fn main() {
/// # #[cfg(feature = "_rt-async-std")]
/// # sqlx::__rt::test_block_on(async move {
/// let conn = SqliteConnectOptions::from_str("sqlite://data.db")? /// let conn = SqliteConnectOptions::from_str("sqlite://data.db")?
/// .journal_mode(SqliteJournalMode::Wal) /// .journal_mode(SqliteJournalMode::Wal)
/// .read_only(true) /// .read_only(true)
/// .connect().await?; /// .connect().await?;
/// # Result::<(), Error>::Ok(()) /// #
/// # }).unwrap(); /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[derive(Clone, Debug)] #[derive(Clone, Debug)]