| 1 | //! Module `qr` is the entrypoint to start making `QRCodes` |
| 2 | |
| 3 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::modulerust-analyzer cargo fast_qr 0.13.1 module/::Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#; |
| 4 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#}; |
| 5 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#, IndexMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#}; |
| 6 | |
| 7 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::datamaskingrust-analyzer cargo fast_qr 0.13.1 datamasking/::Maskrust-analyzer cargo fast_qr 0.13.1 datamasking/Mask#; |
| 8 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::encoderust-analyzer cargo fast_qr 0.13.1 encode/::Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#; |
| 9 | #[cfg(not(feature = "wasm-bindgen"))] |
| 10 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::helpersrust-analyzer cargo fast_qr 0.13.1 helpers/; |
| 11 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::{encoderust-analyzer cargo fast_qr 0.13.1 encode/, Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#, ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#}; |
| 12 | |
| 13 | const QR_MAX_WIDTHrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_WIDTH.: usize = 177; |
| 14 | const QR_MAX_MODULESrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_MODULES.: usize = QR_MAX_WIDTHrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_WIDTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().QR_MAX_WIDTHrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_WIDTH.; |
| 15 | |
| 16 | /// A `QRCode` can be created using [`QRBuilder`]. Simple API for simple usage. |
| 17 | /// If you need to use `QRCode` directly, please file an [issue on |
| 18 | /// github](https://github.com/erwanvivien/fast_qr) explaining your use case. |
| 19 | /// |
| 20 | /// Contains all needed information about the `QRCode`. |
| 21 | /// This is the main struct of the crate. |
| 22 | /// |
| 23 | /// It contains the matrix of the `QRCode`, stored as a one-dimensional array. |
| 24 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 25 | pub struct QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 26 | /// This array length is of size `177 x 177`. It is using a fixed size |
| 27 | /// array simply because of performance. |
| 28 | /// |
| 29 | /// # Other data type possible: |
| 30 | /// - Templated Matrix was faster but crate size was huge. |
| 31 | /// - Vector using `with_capacity`, really bad. |
| 32 | pub datarust-analyzer cargo fast_qr 0.13.1 qr/QRCode#data.: [Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#; QR_MAX_MODULESrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_MODULES.], |
| 33 | /// Width & Height of QRCode. If manually set, should be `version * 4 + 17`, `version` going |
| 34 | /// from 1 to 40 both included. |
| 35 | pub sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.: usize, |
| 36 | |
| 37 | /// Version of the `QRCode`, impacts the size. |
| 38 | /// |
| 39 | /// `None` will optimize Version according to ECL and Mode |
| 40 | pub versionrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#version.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#>, |
| 41 | /// Defines how powerful `QRCode` redundancy should be or how much percent of a QRCode can be |
| 42 | /// recovered. |
| 43 | /// |
| 44 | /// - `ECL::L`: 7% |
| 45 | /// - `ECL::M`: 15% |
| 46 | /// - `ECL::Q`: 25% |
| 47 | /// - `ELC::H`: 30% |
| 48 | /// |
| 49 | /// `None` will set ECL to Quartile (`ELC::Q`) |
| 50 | pub eclrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#ecl.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#>, |
| 51 | |
| 52 | /// Changes the final pattern used. |
| 53 | /// |
| 54 | /// None will find the best suited mask. |
| 55 | pub maskrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mask.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Maskrust-analyzer cargo fast_qr 0.13.1 datamasking/Mask#>, |
| 56 | /// Mode defines which data is being parsed, between Numeric, AlphaNumeric & Byte. |
| 57 | /// |
| 58 | /// `None` will optimize Mode according to user input. |
| 59 | /// |
| 60 | /// ## Note |
| 61 | /// Kanji mode is not supported (yet). |
| 62 | pub moderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mode.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#>, |
| 63 | } |
| 64 | |
| 65 | impl Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 66 | fn fmtrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][Debug]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 67 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("QRCode") |
| 68 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("size", &self.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.) |
| 69 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("version", &self.versionrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#version.) |
| 70 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("ecl", &self.eclrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#ecl.) |
| 71 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("mask", &self.maskrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mask.) |
| 72 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("mode", &self.moderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mode.) |
| 73 | .finish_non_exhaustiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish_non_exhaustive().() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 78 | /// A default `QRCode` will have all it's fields as `None` and a default Matrix filled with `Module::LIGHT`. |
| 79 | #[must_use] |
| 80 | pub const fn defaultrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode]default().(size: usize) -> Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode] { |
| 81 | QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 82 | datarust-analyzer cargo fast_qr 0.13.1 qr/QRCode#data.: [Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::datarust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]data().(Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::LIGHTrust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]LIGHT.); QR_MAX_MODULESrust-analyzer cargo fast_qr 0.13.1 qr/QR_MAX_MODULES.], |
| 83 | sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size., |
| 84 | versionrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#version.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 85 | eclrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#ecl.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 86 | maskrust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mask.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 87 | moderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#mode.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#<usize> for QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 93 | type Outputrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][`Index<usize>`]Output# = [Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#]; |
| 94 | |
| 95 | fn indexrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][`Index<usize>`]index().(&self, index: usize) -> &Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][`Index<usize>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#Output# { |
| 96 | &self.datarust-analyzer cargo fast_qr 0.13.1 qr/QRCode#data.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl IndexMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#<usize> for QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 101 | fn index_mutrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][`IndexMut<usize>`]index_mut().(&mut self, index: usize) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode][`IndexMut<usize>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#Output# { |
| 102 | &mut self.datarust-analyzer cargo fast_qr 0.13.1 qr/QRCode#data.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Contains different error when [`QRCode`] could not be created |
| 107 | pub enum QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError# { |
| 108 | /// If data if too large to be encoded (refer to Table 7-11 of the spec or [an online table](https://fast-qr.com/blog/tables/ecl)) |
| 109 | EncodedDatarust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#EncodedData#, |
| 110 | /// Specified version too small to contain data |
| 111 | SpecifiedVersionrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#SpecifiedVersion#, |
| 112 | } |
| 113 | |
| 114 | // We don't want to use `std::error::Error` on wasm32 |
| 115 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError# {} |
| 116 | |
| 117 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError# { |
| 118 | fn fmtrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCodeError][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 119 | match self { |
| 120 | QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::EncodedDatarust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#EncodedData# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Data too big to be encoded"), |
| 121 | QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::SpecifiedVersionrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#SpecifiedVersion# => { |
| 122 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Specified version too low to contain data") |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError# { |
| 129 | fn fmtrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCodeError][Debug]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 130 | match self { |
| 131 | QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::EncodedDatarust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#EncodedData# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Data too big to be encoded"), |
| 132 | QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::SpecifiedVersionrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#SpecifiedVersion# => { |
| 133 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Specified version too low to contain data") |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode# { |
| 140 | /// Creates a new `QRCode` from a ECL / version |
| 141 | /// |
| 142 | /// # Errors |
| 143 | /// - `QRCodeError::EncodedData` if `input` is too large to be encoded |
| 144 | /// - `QRCodeError::SpecifiedVersion` if specified `version` is too small to contain data |
| 145 | pub(craterust-analyzer cargo fast_qr 0.13.1 crate/) fn newrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode]new().( |
| 146 | input: &[u8], |
| 147 | ecl: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#>, |
| 148 | v: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#>, |
| 149 | mode: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#>, |
| 150 | mut mask: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Maskrust-analyzer cargo fast_qr 0.13.1 datamasking/Mask#>, |
| 151 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode], QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#> { |
| 152 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::placementrust-analyzer cargo fast_qr 0.13.1 placement/::create_matrixrust-analyzer cargo fast_qr 0.13.1 placement/create_matrix().; |
| 153 | |
| 154 | let mode = mode.unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| encoderust-analyzer cargo fast_qr 0.13.1 encode/::best_encodingrust-analyzer cargo fast_qr 0.13.1 encode/best_encoding().(input)); |
| 155 | let level = ecl.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#::Qrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#Q#); |
| 156 | |
| 157 | let version = match Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#::getrust-analyzer cargo fast_qr 0.13.1 version/impl#[Version]get().(mode, level, input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) { |
| 158 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version) => version, |
| 159 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::EncodedDatarust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#EncodedData#), |
| 160 | }; |
| 161 | let version = match v { |
| 162 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user_version) if user_version as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().version as usize => user_version, |
| 163 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => version, |
| 164 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#::SpecifiedVersionrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#SpecifiedVersion#), |
| 165 | }; |
| 166 | |
| 167 | let out = create_matrixrust-analyzer cargo fast_qr 0.13.1 placement/create_matrix().(input, level, mode, version, &mut mask); |
| 168 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(out) |
| 169 | } |
| 170 | |
| 171 | /// Prints the `QRCode` to the terminal |
| 172 | #[must_use] |
| 173 | #[cfg(not(feature = "wasm-bindgen"))] |
| 174 | pub fn to_strrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode]to_str().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 175 | helpersrust-analyzer cargo fast_qr 0.13.1 helpers/::print_matrix_with_marginrust-analyzer cargo fast_qr 0.13.1 helpers/print_matrix_with_margin().(self) |
| 176 | } |
| 177 | |
| 178 | /// Prints the `QRCode` to the terminal |
| 179 | #[cfg(not(feature = "wasm-bindgen"))] |
| 180 | pub fn printrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode]print().(&self) { |
| 181 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", helpersrust-analyzer cargo fast_qr 0.13.1 helpers/::print_matrix_with_marginrust-analyzer cargo fast_qr 0.13.1 helpers/print_matrix_with_margin().(self)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// Builder struct, makes it easier to create a [`QRCode`]. |
| 186 | /// |
| 187 | /// # Example |
| 188 | /// ```rust |
| 189 | /// use fast_qr::QRBuilder; |
| 190 | /// use fast_qr::{Mask, ECL, Version}; |
| 191 | /// |
| 192 | /// // Creates a `QRCode` with a forced `version`, `ecl` and/or `mask` |
| 193 | /// let input = String::from("Hello World!"); |
| 194 | /// let qr = QRBuilder::new(input) |
| 195 | /// // .version(Version::V05) |
| 196 | /// // .ecl(ECL::H) |
| 197 | /// // .mask(Mask::Checkerboard) |
| 198 | /// .build(); |
| 199 | /// ``` |
| 200 | pub struct QRBuilderrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder# { |
| 201 | inputrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#input.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 202 | eclrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#ecl.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#>, |
| 203 | moderust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mode.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#>, |
| 204 | versionrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#version.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#>, |
| 205 | maskrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mask.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Maskrust-analyzer cargo fast_qr 0.13.1 datamasking/Mask#>, |
| 206 | } |
| 207 | |
| 208 | impl QRBuilderrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder# { |
| 209 | /// Creates an instance of `QRBuilder` with default parameters |
| 210 | #[must_use] |
| 211 | pub fn newrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]new().<I: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>>(input: I) -> QRBuilderrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder# { |
| 212 | QRBuilderrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder# { |
| 213 | inputrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#input.: input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(), |
| 214 | maskrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mask.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 215 | moderust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mode.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 216 | versionrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#version.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 217 | eclrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#ecl.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Forces the Mode |
| 222 | pub fn moderust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]mode().(&mut self, mode: Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder] { |
| 223 | self.moderust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mode. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mode); |
| 224 | self |
| 225 | } |
| 226 | |
| 227 | /// Forces the Encoding Level |
| 228 | pub fn eclrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]ecl().(&mut self, ecl: ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder] { |
| 229 | self.eclrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#ecl. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ecl); |
| 230 | self |
| 231 | } |
| 232 | |
| 233 | /// Forces the version |
| 234 | pub fn versionrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]version().(&mut self, version: Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder] { |
| 235 | self.versionrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#version. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version); |
| 236 | self |
| 237 | } |
| 238 | |
| 239 | /// Forces the mask, should very rarely be used |
| 240 | pub fn maskrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]mask().(&mut self, mask: Maskrust-analyzer cargo fast_qr 0.13.1 datamasking/Mask#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder] { |
| 241 | self.maskrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mask. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mask); |
| 242 | self |
| 243 | } |
| 244 | |
| 245 | /// Computes a [`QRCode`] with given parameters |
| 246 | /// |
| 247 | /// # Errors |
| 248 | /// - `QRCodeError::EncodedData` if `input` is too large to be encoded. See [an online table](https://fast-qr.com/blog/tables/ecl) for more info. |
| 249 | /// - `QRCodeError::SpecifiedVersion` if specified `version` is too small to contain data |
| 250 | pub fn buildrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRBuilder]build().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#, QRCodeErrorrust-analyzer cargo fast_qr 0.13.1 qr/QRCodeError#> { |
| 251 | QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#::newrust-analyzer cargo fast_qr 0.13.1 qr/impl#[QRCode]new().(&self.inputrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#input., self.eclrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#ecl., self.versionrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#version., self.moderust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mode., self.maskrust-analyzer cargo fast_qr 0.13.1 qr/QRBuilder#mask.) |
| 252 | } |
| 253 | } |