| 1 |
//! Example usage of the `link-section` crate. |
| 2 |
#![warn(missing_docs)] |
| 3 |
|
| 4 |
use link_section::{in_section, section}; |
| 5 |
|
| 6 |
struct Driver { |
| 7 |
name: &'static str, |
| 8 |
f: fn(), |
| 9 |
} |
| 10 |
|
| 11 |
impl Driver { |
| 12 |
/// Create a new driver. |
| 13 |
const fn new(name: &'static str, f: fn()) -> Self { |
| 14 |
Self { name, f } |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
/// Drivers. |
| 19 |
#[section(typed)] |
| 20 |
static DATA_SECTION: link_section::TypedSection<Driver>; |
| 21 |
|
| 22 |
mod drivers { |
| 23 |
use crate::Driver; |
| 24 |
use link_section::in_section; |
| 25 |
|
| 26 |
/// A driver for PostgreSQL. |
| 27 |
#[in_section(super::DATA_SECTION)] |
| 28 |
pub const POSTGRES_DRIVER: Driver = |
| 29 |
Driver::new("postgres", || println!("connected to postgres!")); |
| 30 |
|
| 31 |
/// A driver for MySQL. |
| 32 |
#[in_section(super::DATA_SECTION)] |
| 33 |
pub const MYSQL_DRIVER: Driver = Driver::new("mysql", || println!("connected to mysql!")); |
| 34 |
|
| 35 |
/// A driver for SQLite. |
| 36 |
#[in_section(super::DATA_SECTION)] |
| 37 |
pub const SQLITE_DRIVER: Driver = Driver::new("sqlite", || println!("connected to sqlite!")); |
| 38 |
} |
| 39 |
|
| 40 |
/// Databases. |
| 41 |
#[section(typed)] |
| 42 |
static DATABASES: link_section::TypedSection<(&'static str, &'static Driver)>; |
| 43 |
|
| 44 |
/// A database for PostgreSQL. |
| 45 |
#[in_section(DATABASES)] |
| 46 |
pub const POSTGRES_DATABASE: (&'static str, &'static Driver) = |
| 47 |
("postgres://localhost:5432", &drivers::POSTGRES_DRIVER); |
| 48 |
|
| 49 |
/// A database for MySQL. |
| 50 |
#[in_section(DATABASES)] |
| 51 |
pub const MYSQL_DATABASE: (&'static str, &'static Driver) = |
| 52 |
("mysql://localhost:3306", &drivers::MYSQL_DRIVER); |
| 53 |
|
| 54 |
/// A database for SQLite. |
| 55 |
#[in_section(DATABASES)] |
| 56 |
pub const SQLITE_DATABASE: (&'static str, &'static Driver) = |
| 57 |
("sqlite://localhost:1433", &drivers::SQLITE_DRIVER); |
| 58 |
|
| 59 |
fn main() { |
| 60 |
for (url, driver) in DATABASES { |
| 61 |
println!("Connecting to {url} ({})...", driver.name); |
| 62 |
(driver.f)(); |
| 63 |
} |
| 64 |
} |