1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
// Copyright © 2022-2024 QR Code Library (QRC). All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
// These lines declare the copyright and licensing for the QR Code Library (QRC).
// The library is protected by copyright law from 2022 to 2024.
// It is dual-licensed under the Apache License 2.0 and the MIT License.
// SPDX (Software Package Data Exchange) identifiers are used for clarity in licensing.
#[macro_export]
/// Macro to add a watermark image to a QR code.
///
/// # Parameters
/// * `$img` - The main QR code image as a mutable reference.
/// * `$watermark` - The watermark image as an immutable reference.
///
/// # Example
/// ```
/// use qrc::QRCode;
/// use image::{ImageBuffer, Rgba};
///
/// // Create a mock QR code and watermark image for the example
/// let mut img = ImageBuffer::from_pixel(100, 100, Rgba([0, 0, 0, 255]));
/// let watermark = ImageBuffer::from_pixel(50, 50, Rgba([255, 255, 255, 255]));
///
/// qrc::add_image_watermark!(&mut img, &watermark);
/// ```
macro_rules! add_image_watermark {
// Defines the macro `add_image_watermark`.
($img:expr, $watermark:expr) => {
// The macro takes two parameters: `$img` and `$watermark`.
// `$img` is an expression representing the main image.
// `$watermark` is an expression representing the watermark image.
QRCode::add_image_watermark($img, $watermark)
// Calls the `add_image_watermark` method on the `QRCode` struct,
// passing the provided image and watermark.
};
}
#[macro_export]
/// Macro to create a new QR code from the given data.
///
/// # Parameters
/// * `$data` - The data to be encoded in the QR code.
///
/// # Example
/// ```
/// use qrc::{QRCode, qr_code};
/// qr_code!("Hello, world!".into());
/// ```
macro_rules! qr_code {
// Defines the macro `qr_code`.
($data:expr) => {
// The macro takes one parameter: `$data`.
// `$data` is an expression representing the data to encode in the QR code.
QRCode::new($data)
// Calls the `new` method on the `QRCode` struct with the provided data.
};
}
#[macro_export]
/// Macro to create a QR code in a specified format with a given width.
///
/// # Parameters
/// * `$data` - The data to be encoded in the QR code.
/// * `$format` - The format of the QR code image (e.g., "png", "jpg", "gif").
/// * `$width` - The width of the QR code image.
///
/// # Example
/// ```
/// use qrc::{QRCode, qr_code_to};
/// qr_code_to!("Hello, world!".into(), "png", 256);
/// ```
macro_rules! qr_code_to {
// Define the macro `qr_code_to`.
// This macro takes three expressions: `$data`, `$format`, and `$width`.
($data:expr, $format:expr, $width:expr) => {
// Match the value of `$format`.
match $format {
// If `$format` is equal to "png", generate a PNG format QR code.
"png" => QRCode::from_bytes($data).to_png($width),
// If `$format` is equal to "jpg", generate a JPG format QR code.
"jpg" => QRCode::from_bytes($data).to_jpg($width),
// If `$format` is equal to "gif", generate a GIF format QR code.
"gif" => QRCode::from_bytes($data).to_gif($width),
// For any other value, cause a panic with the message "Invalid format".
_ => panic!("Invalid format"),
// The underscore `_` is a catch-all pattern; if `$format` doesn't match
// any of the specified formats, this block will execute.
}
};
}
#[macro_export]
/// Sets the size of the QR code.
///
/// This macro allows the user to specify the size of the QR code.
/// The size is typically defined in terms of pixels or modules (the small squares that make up a QR code).
///
/// # Parameters
/// - `$qrcode:expr`: An instance of `QRCode`.
/// - `$size:expr`: The desired size for the QR code.
///
/// # Example
/// ```
/// use qrc::QRCode;
/// use qrc::resize;
///
/// let qrcode = QRCode::new("Hello, world!".as_bytes().to_vec());
/// let resized_qrcode = resize!(qrcode, 256);
/// ```
macro_rules! resize {
($qrcode:expr, $size:expr) => {
$qrcode.resize($size, $size)
};
}
#[macro_export]
/// Sets the encoding format for the data in a QR code.
///
/// QR codes can encode data in several formats, such as numeric, alphanumeric,
/// or binary. This macro allows setting the preferred encoding format for the data.
///
/// # Parameters
/// - `$qr_code:expr`: An instance of `QRCode`.
/// - `$format:expr`: The encoding format for the QR code data.
///
/// # Example
/// ```
/// use qrc::{QRCode, set_encoding_format}; // Import QRCode and the macro
///
/// let qr_code = QRCode::new("some data".as_bytes().to_vec()); // Create a QRCode instance
/// let qr_with_format = set_encoding_format!(qr_code, "utf-8"); // Use the macro to set the encoding format
/// ```
macro_rules! set_encoding_format {
($qr_code:expr, $format:expr) => {
$qr_code.set_encoding_format($format)
};
}
#[macro_export]
/// Overlays an image (e.g., a logo) at the center of the QR code.
///
/// This is particularly useful for branding purposes, allowing the inclusion
/// of a company logo within the QR code.
///
/// # Parameters
/// - `$qr_code:expr`: QRCode instance to which the image will be overlaid.
/// - `$image_path:expr`: Path to the image file to overlay.
///
/// # Example
/// ```
/// # // The following is a mock example, as actual file loading cannot be done in doctests
/// use qrc::{QRCode, overlay_image};
/// use image::{RgbaImage, ImageBuffer, Rgba};
///
/// let qr_code = QRCode::new("some data".as_bytes().to_vec());
/// // Mock an image (e.g., a small red square)
/// let logo = ImageBuffer::from_pixel(10, 10, Rgba([255, 0, 0, 255]));
///
/// let qr_with_logo = overlay_image!(qr_code, &logo); // Use the macro for overlaying the image
/// ```
macro_rules! overlay_image {
($qr_code:expr, $image_path:expr) => {
$qr_code.overlay_image($image_path)
};
}
#[macro_export]
/// Generates multiple QR codes in one operation.
///
/// This macro is useful for batch processing, such as generating QR codes
/// for a list of URLs or serial numbers.
///
/// # Parameters
/// - `$data_list:expr`: A vector of data strings for which QR codes are to be generated.
///
/// # Example
/// ```
/// use qrc::QRCode; // Import QRCode type
/// use qrc::batch_generate_qr; // Import the macro
/// let qr_codes = batch_generate_qr!(vec!["https://example.com".to_string(), "https://example2.com".to_string()]);
/// ```
macro_rules! batch_generate_qr {
($data_list:expr) => {
QRCode::batch_generate_qr_codes($data_list)
};
}
#[macro_export]
/// Compresses data before encoding it into a QR code.
///
/// This is beneficial for encoding large amounts of data into a QR code
/// by reducing the size of the data.
///
/// # Parameters
/// - `$data:expr`: The data to be compressed and encoded.
///
/// # Example
/// ```
/// use qrc::QRCode; // Import QRCode type
/// use qrc::compress_data_macro; // Corrected import to use the macro
/// let compressed_data = compress_data_macro!("Some large string of data"); // Correct usage
/// ```
macro_rules! compress_data_macro {
($data:expr) => {
QRCode::compress_data($data)
};
}
#[macro_export]
/// Combines multiple QR codes into a single QR code.
///
/// This macro is useful for scenarios where multiple QR codes need to be combined,
/// such as creating a composite QR code with several data sources.
///
/// # Parameters
/// - An array of QRCode instances to combine.
///
/// # Example
/// ```
/// use qrc::QRCode; // Import QRCode type
/// use qrc::combine_qr_codes;
///
/// let qr_code1 = QRCode::from_string("Data 1".to_string());
/// let qr_code2 = QRCode::from_string("Data 2".to_string());
/// let qr_code3 = QRCode::from_string("Data 3".to_string());
///
/// let combined_qr_code = combine_qr_codes!(vec![qr_code1, qr_code2, qr_code3]);
/// ```
macro_rules! combine_qr_codes {
($codes:expr) => {
QRCode::combine_qr_codes($codes)
};
}
#[macro_export]
/// Generates a dynamic QR code, which can be updated after creation.
///
/// Useful for scenarios where the data linked to the QR code might change
/// over time, such as promotional offers or event details.
///
/// # Parameters
/// - `$initial_data:expr`: The initial data for the QR code.
///
/// # Example
/// ```
/// use qrc::QRCode; // Import QRCode type
/// use qrc::create_dynamic_qr;
/// create_dynamic_qr!("Initial Data");
/// ```
macro_rules! create_dynamic_qr {
($initial_data:expr) => {
QRCode::create_dynamic($initial_data)
};
}
#[macro_export]
/// Generates QR codes with multi-language support.
///
/// The QR code displays different data based on the user's language preference.
///
/// # Parameters
/// - Pairs of language codes and corresponding data.
///
/// # Example
/// ```
/// use qrc::QRCode; // Import QRCode type
/// use qrc::create_multilanguage_qr;
/// create_multilanguage_qr! {
/// "en" => "Hello",
/// "es" => "Hola",
/// "fr" => "Bonjour"
/// };
/// ```
macro_rules! create_multilanguage_qr {
($($lang:expr => $text:expr),* $(,)?) => {{
use std::collections::HashMap;
let mut data_map: HashMap<String, String> = HashMap::new();
$(
data_map.insert($lang.to_string(), $text.to_string());
)*
QRCode::create_multilanguage(data_map)
}};
}