pub fn encode_segments_advanced(
        segs: &[QrSegment],
        mut ecl: QrCodeEcc,
        minversion: Version,
        maxversion: Version,
        mask: Option<Mask>,
        boostecl: bool,
    ) -> Result<Self, DataTooLong> {
        assert!(minversion <= maxversion, "Invalid value");

        // Find the minimal version number to use
        let mut version: Version = minversion;
        let datausedbits: usize = loop {
            let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; // Number of data bits available
            let dataused: Option<usize> = QrSegment::get_total_bits(segs, version);
            if dataused.map_or(false, |n| n <= datacapacitybits) {
                break dataused.unwrap(); // This version number is found to be suitable
            } else if version >= maxversion {
                // All versions in the range could not fit the given data
                return Err(match dataused {
                    None => DataTooLong::SegmentTooLong,
                    Some(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),
                });
            } else {
                version = Version::new(version.value() + 1);
            }
        };

        // Increase the error correction level while the data still fits in the current version number
        for &newecl in &[QrCodeEcc::Medium, QrCodeEcc::Quartile, QrCodeEcc::High] {
            // From low to high
            if boostecl && datausedbits <= QrCode::get_num_data_codewords(version, newecl) * 8 {
                ecl = newecl;
            }
        }

        // Concatenate all segments to create the data bit string
        let mut bb = BitBuffer(Vec::new());
        for seg in segs {
            bb.append_bits(seg.mode.mode_bits(), 4);
            bb.append_bits(
                u32::try_from(seg.numchars).unwrap(),
                seg.mode.num_char_count_bits(version),
            );
            bb.0.extend_from_slice(&seg.data);
        }
        debug_assert_eq!(bb.0.len(), datausedbits);

        // Add terminator and pad up to a byte if applicable
        let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;
        debug_assert!(bb.0.len() <= datacapacitybits);
        let numzerobits: usize = std::cmp::min(4, datacapacitybits - bb.0.len());
        bb.append_bits(0, u8::try_from(numzerobits).unwrap());
        let numzerobits: usize = bb.0.len().wrapping_neg() & 7;
        bb.append_bits(0, u8::try_from(numzerobits).unwrap());
        debug_assert_eq!(bb.0.len() % 8, 0);

        // Pad with alternating bytes until data capacity is reached
        for &padbyte in [0xEC, 0x11].iter().cycle() {
            if bb.0.len() >= datacapacitybits {
                break;
            }
            bb.append_bits(padbyte, 8);
        }

        // Pack bits into bytes in big endian
        let mut datacodewords = vec![0u8; bb.0.len() / 8];
        for (i, &bit) in bb.0.iter().enumerate() {
            datacodewords[i >> 3] |= u8::from(bit) << (7 - (i & 7));
        }

        // Create the QR Code object
        Ok(QrCode::encode_codewords(version, ecl, &datacodewords, mask))
    }
/// Returns a QR Code representing the given segments with the given encoding parameters.
///
/// The smallest possible QR Code version within the given range is automatically
/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result
/// may be higher than the ecl argument if it can be done without increasing the
/// version. The mask number is either between 0 to 7 (inclusive) to force that
/// mask, or `None` to automatically choose an appropriate mask (which may be slow).
///
/// This function allows the user to create a custom sequence of segments that switches
/// between modes (such as alphanumeric and byte) to encode text in less space.
/// This is a mid-level API; the high-level API is `encode_text()` and `encode_binary()`.
///
/// Returns a wrapped `QrCode` if successful, or `Err` if the data is too
/// long to fit in any version in the given range at the given ECC level.
Returns a QR Code representing the given segments with the given encoding parameters.
The smallest possible QR Code version within the given range is automatically
chosen for the output. Iff boostecl is `true`, then the ECC level of the result
may be higher than the ecl argument if it can be done without increasing the
version. The mask number is either between 0 to 7 (inclusive) to force that
mask, or `None` to automatically choose an appropriate mask (which may be slow).

This function allows the user to create a custom sequence of segments that switches
between modes (such as alphanumeric and byte) to encode text in less space.
This is a mid-level API; the high-level API is `encode_text()` and `encode_binary()`.

Returns a wrapped `QrCode` if successful, or `Err` if the data is too
long to fit in any version in the given range at the given ECC level.
Returns a QR Code representing the given segments with the given encoding parameters . The smallest possible QR Code version within the given range is automatically chosen for the output . Iff boostecl is ` true ` then the ECC level of the result may be higher than the ecl argument if it can be done without increasing the version . The mask number is either between 0 to 7 ( inclusive ) to force that mask or ` None ` to automatically choose an appropriate mask ( which may be slow ) . This function allows the user to create a custom sequence of segments that switches between modes ( such as alphanumeric and byte ) to encode text in less space . This is a mid - level API ; the high - level API is ` encode_text () ` and ` encode_binary () ` . Returns a wrapped ` QrCode ` if successful or ` Err ` if the data is too long to fit in any version in the given range at the given ECC level .
pub fn encode_segments_advanced(
        segs: &[QrSegment],
        mut ecl: QrCodeEcc,
        minversion: Version,
        maxversion: Version,
        mask: Option<Mask>,
        boostecl: bool,
    ) -> Result<Self, DataTooLong> {
        assert!(minversion <= maxversion, "Invalid value");
        // Find the minimal version number to use
        let mut version: Version = minversion;
        let datausedbits: usize = loop {
            let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; // Number of data bits available
            let dataused: Option<usize> = QrSegment::get_total_bits(segs, version);
            if dataused.map_or(false, |n| n <= datacapacitybits) {
                break dataused.unwrap(); // This version number is found to be suitable
            } else if version >= maxversion {
                // All versions in the range could not fit the given data
                return Err(match dataused {
                    None => DataTooLong::SegmentTooLong,
                    Some(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),
                });
            } else {
                version = Version::new(version.value() + 1);
            }
        };
        // Increase the error correction level while the data still fits in the current version number
        for &newecl in &[QrCodeEcc::Medium, QrCodeEcc::Quartile, QrCodeEcc::High] {
            // From low to high
            if boostecl && datausedbits <= QrCode::get_num_data_codewords(version, newecl) * 8 {
                ecl = newecl;
            }
        }
        // Concatenate all segments to create the data bit string
        let mut bb = BitBuffer(Vec::new());
        for seg in segs {
            bb.append_bits(seg.mode.mode_bits(), 4);
            bb.append_bits(
                u32::try_from(seg.numchars).unwrap(),
                seg.mode.num_char_count_bits(version),
            );
            bb.0.extend_from_slice(&seg.data);
        }
        debug_assert_eq!(bb.0.len(), datausedbits);
        // Add terminator and pad up to a byte if applicable
        let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;
        debug_assert!(bb.0.len() <= datacapacitybits);
        let numzerobits: usize = std::cmp::min(4, datacapacitybits - bb.0.len());
        bb.append_bits(0, u8::try_from(numzerobits).unwrap());
        let numzerobits: usize = bb.0.len().wrapping_neg() & 7;
        bb.append_bits(0, u8::try_from(numzerobits).unwrap());
        debug_assert_eq!(bb.0.len() % 8, 0);
        // Pad with alternating bytes until data capacity is reached
        for &padbyte in [0xEC, 0x11].iter().cycle() {
            if bb.0.len() >= datacapacitybits {
                break;
            }
            bb.append_bits(padbyte, 8);
        }
        // Pack bits into bytes in big endian
        let mut datacodewords = vec![0u8; bb.0.len() / 8];
        for (i, &bit) in bb.0.iter().enumerate() {
            datacodewords[i >> 3] |= u8::from(bit) << (7 - (i & 7));
        }
        // Create the QR Code object
        Ok(QrCode::encode_codewords(version, ecl, &datacodewords, mask))
    }
pub fn encode_segments_advanced ( segs : & [ QrSegment ] , mut ecl : QrCodeEcc , minversion : Version , maxversion : Version , mask : Option < Mask > , boostecl : bool , ) -> Result < Self , DataTooLong > { assert ! ( minversion maxversion " " ) ; // Find the minimal version number to use let mut version : Version = minversion ; let datausedbits : usize = loop { let datacapacitybits : usize = QrCode :: get_num_data_codewords ( version , ecl ) * 8 ; // Number of data bits available let dataused : Option < usize > = QrSegment :: get_total_bits ( segs , version ) ; if dataused . map_or ( false , | n | n <= datacapacitybits ) { break dataused . unwrap ( ) ; // This version number is found to be suitable } else if version >= maxversion { // All versions in the range could not fit the given data return Err ( match dataused { None => DataTooLong :: SegmentTooLong , Some ( n ) => DataTooLong :: DataOverCapacity ( n , datacapacitybits ) , } ) ; } else { version = Version :: new ( version . value ( ) + 1 ) ; } } ; // Increase the error correction level while the data still fits in the current version number for & newecl in & [ QrCodeEcc :: Medium , QrCodeEcc :: Quartile , QrCodeEcc :: High ] { // From low to high if boostecl && datausedbits <= QrCode :: get_num_data_codewords ( version , newecl ) * 8 { ecl = newecl ; } } // Concatenate all segments to create the data bit string let mut bb = BitBuffer ( Vec :: new ( ) ) ; for seg in segs { bb . append_bits ( seg . mode . mode_bits ( ) , 4 ) ; bb . append_bits ( u32 :: try_from ( seg . numchars ) . unwrap ( ) , seg . mode . num_char_count_bits ( version ) , ) ; bb . 0 . extend_from_slice ( & seg . data ) ; } debug_assert_eq ! ( bb 0 len ( ) datausedbits ) ; // Add terminator and pad up to a byte if applicable let datacapacitybits : usize = QrCode :: get_num_data_codewords ( version , ecl ) * 8 ; debug_assert ! ( bb 0 len ( ) datacapacitybits ) ; let numzerobits : usize = std :: cmp :: min ( 4 , datacapacitybits - bb . 0 . len ( ) ) ; bb . append_bits ( 0 , u8 :: try_from ( numzerobits ) . unwrap ( ) ) ; let numzerobits : usize = bb . 0 . len ( ) . wrapping_neg ( ) & 7 ; bb . append_bits ( 0 , u8 :: try_from ( numzerobits ) . unwrap ( ) ) ; debug_assert_eq ! ( bb 0 len ( ) 8 0 ) ; // Pad with alternating bytes until data capacity is reached for & padbyte in [ 0xEC , 0x11 ] . iter ( ) . cycle ( ) { if bb . 0 . len ( ) >= datacapacitybits { break ; } bb . append_bits ( padbyte , 8 ) ; } // Pack bits into bytes in big endian let mut datacodewords = vec ! [ 0u8 bb 0 len ( ) 8 ] ; for ( i , & bit ) in bb . 0 . iter ( ) . enumerate ( ) { datacodewords [ i >> 3 ] |= u8 :: from ( bit ) << ( 7 - ( i & 7 ) ) ; } // Create the QR Code object Ok ( QrCode :: encode_codewords ( version , ecl , & datacodewords , mask ) ) }
Returns a QR Code representing the given segments with the given encoding parameters.
Returns a QR Code representing the given segments with the given encoding parameters .
segs
&[QrSegment]
ecl
QrCodeEcc
minversion
Version
maxversion
Version
mask
Option<Mask>
boostecl
bool