fake-rs

repository·master·Indexed 22 days ago

https://github.com/cksac/fake-rs

A Rust library and command line tool for generating high-quality fake data such as names, addresses, and internet information across multiple languages. It includes the Dummy derive macro for automatic fake data generation for structs and enums, and a CLI for generating random data directly from the terminal.

Tokens
11.1K
Snippets
23
Records
54
Agent score
79%

What's inside fake-rs

  1. Generate fake data with specific locales

    master

    You can generate locale-specific data by passing a locale constant (e.g., EN, ZH_TW, ZH_CN) to a faker.

    Supported locales include:

    • English (EN)
    • French (FR_FR)
    • Arabic (AR_SA)
    • Traditional Chinese (ZH_TW)
    • Simplified Chinese (ZH_CN)
    • Japanese (JA_JP)
    • Portuguese (Brazilian) (PT_BR)
    • Portuguese (Portugal) (PT_PT)
    • German (DE_DE)
    • Italian (IT_IT)
    • Welsh (CY_GB)
    • Dutch (NL_NL)
    • Persian (FA_IR)

    Example:

    use fake::faker::name::raw::*;nuse fake::locales::*;
    
    let name: String = Name(ZH_TW).fake();
    use fake::faker::name::raw::*;
    use fake::locales::*;
    
    let name: String = Name(EN).fake();
    println!("name {:?}", name);
    
    let name: String = Name(ZH_TW).fake();
    println!("name {:?}", name);
  2. Customize documentation content via data files

    master

    The documentation site uses a data-driven architecture. Instead of editing HTML, you can update the site content by modifying specific JavaScript files in the data/ directory:

    • Locales: Edit data/locales.js to add or update supported locales.
    • Features: Edit data/features.js to add or update feature flags.
    • Fakers: Edit data/fakers.js to add or update faker categories and items.
    • Examples: Edit data/examples.js to add or update code examples.
  3. Add a new code example to the documentation

    master

    To add usage examples, edit docs/data/examples.js. The site automatically handles syntax highlighting, the copy button, and rendering notes.

    {
        title: "Example Title",
        language: "rust",  // or "bash", "toml", etc.
        code: `your code here`,
        note: "⚠️ Optional note"  // Optional
    }
  4. Deploy the fake-rs documentation site to GitHub Pages

    master

    You can deploy the documentation site using two methods: manual GitHub Pages configuration or a GitHub Actions workflow.

    Method 1: Manual GitHub Pages Setup

    1. Go to your repository settings on GitHub.
    2. Navigate to the Pages section.
    3. Under Source, select:
      • Source: Deploy from a branch
      • Branch: main (or master)
      • Folder: /docs
    4. Click Save.

    Method 2: Using GitHub Actions

    Create a .github/workflows/pages.yml file with the following configuration to automate deployment on every push to the main branch.

    name: Deploy GitHub Pages
    
    on:
      push:
        branches: [ main ]
      workflow_dispatch:
    
    permissions:
      contents: read
      pages: write
      id-token: write
    
    jobs:
      deploy:
        environment:
          name: github-pages
          url: ${{ steps.deployment.outputs.page_url }}
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v3
          
          - name: Setup Pages
            uses: actions/configure-pages@v3
          
          - name: Upload artifact
            uses: actions/upload-pages-artifact@v2
            with:
              path: './docs'
          
          - name: Deploy to GitHub Pages
            id: deployment
            uses: actions/deploy-pages@v2
  5. Use the Dummy derive macro for automatic fake data generation

    master

    The Dummy derive macro allows you to automatically generate fake data for structs. You can customize how individual fields are populated using the #[dummy(faker = "...")] attribute.

    Supported faker patterns in the attribute include:

    • Range expressions: "1000.." or "1..100"
    • Specific Faker calls: "Name()", "CompanyName()", or "Boolean(70)"
    • Tuple-based generation: "(Faker, 3..5)" (to generate a collection of a certain size)

    To generate an instance, use Faker.fake().

    use fake::faker::boolean::en::*
    use fake::faker::company::en::*
    use fake::faker::name::en::*
    use fake::Dummy;
    use fake::{Fake, Faker};
    
    #[derive(Debug, Dummy)]
    pub struct Order {
        #[dummy(faker = "1000..")]
        order_id: usize,
    
        #[dummy(faker = "Name()",")]
        customer: String,
    
        #[dummy(faker = "(Faker, 3..5)")]
        items: Vec<Item>,
    
        #[dummy(faker = "Boolean(70)")]
        paid: bool,
    }
    
    #[derive(Debug, Dummy)]
    pub struct Item {
        #[dummy(faker = "1..100")]
        product_id: usize,
    
        qty: u8,
    
        #[dummy(faker = "CompanyName()",")]
        company: String,
    }
    
    fn main() {
        let order: Order = Faker.fake();
        println!("{:#?}", order);
    }
  6. Customize documentation styling

    master

    To change the visual appearance of the documentation site, edit the CSS variables in styles.css within the :root selector.

    :root {
        --primary-color: #ff6b35;
        --secondary-color: #004e89;
        --accent-color: #1a659e;
    }
  7. Test documentation changes locally

    master

    Before committing changes, verify them by running a local web server and opening docs/index.html in your browser.

    Recommended check-list:

    • Verify all locales, features, and fakers display correctly.
    • Ensure search functionality works.
    • Check that code examples have syntax highlighting and copy buttons work.
    • Check the browser console (F12) for errors.
    # Python
    python -m http.server 8000
    
    # Node.js
    npx http-server
    
    # PHP
    php -S localhost:8000
  8. Install the fake library

    master

    To use fake as a Rust library, add it to your Cargo.toml. It is recommended to enable the derive feature if you intend to use the #[derive(Dummy)] macro for automatic data generation.

    [dependencies]
    fake = { version = "5", features = ["derive"] }
    ```toml
    [dependencies]
    fake = { version = "5", features = ["derive"] }
    ```埋