Skip to main content

CharsetConverter

Struct CharsetConverter 

Source
pub struct CharsetConverter<D, E>{ /* private fields */ }
Expand description

Converts units encoded with one charset into units encoded with another charset.

The converter owns the source and target charset codecs plus the same decode/encode policy hooks used by crate::CharsetDecoder and crate::CharsetEncoder. A decoded character may be kept pending inside the common buffered convert engine when the target output buffer is full. During BufferedTranscoder::finish, the converter drains internally retained output and finishes the composed decode/encode policy hooks. Callers remain responsible for handling any incomplete input tail before finishing the logical stream.

§Type Parameters

  • D: Low-level charset codec used by the source decoder.
  • E: Low-level charset codec used by the target encoder.
use qubit_codec_text::{
    CharsetConverter,
    CharsetDecoder,
    CharsetEncoder,
    TranscodeStatus,
    BufferedTranscoder,
    Utf16U16Codec,
    Utf8Codec,
};

let mut converter = CharsetConverter::from_codecs(Utf8Codec, Utf16U16Codec);
let mut output = [0_u16; 2];

let progress = converter
    .transcode("AB".as_bytes(), 0, &mut output, 0)
    .expect("transcode bytes to utf-16");

assert_eq!(TranscodeStatus::Complete, progress.status());
assert_eq!(2, progress.read());
assert_eq!(2, progress.written());
assert_eq!([65, 66], output);

Implementations§

Source§

impl<D, E> CharsetConverter<D, E>

Source

pub fn from_codecs(source: D, target: E) -> Self

Creates a charset converter from raw source and target codecs.

§Parameters
  • source: Source charset codec.
  • target: Target charset codec.
§Returns

Returns a converter with the default decoder policy and the target encoder policy that can be represented by target. The encoder policy first tries CharsetEncodePolicy::DEFAULT_REPLACEMENT and falls back to CharsetEncodePolicy::DEFAULT_FALLBACK_REPLACEMENT when needed.

§Panics

Panics when neither default replacement can be encoded by target.

Source

pub fn from_codecs_with_policies( source: D, target: E, decode_policy: CharsetDecodePolicy, encode_policy: CharsetEncodePolicy, ) -> Result<Self, CharsetEncodeError>

Creates a charset converter from raw codecs and explicit policies.

§Parameters
  • source: Source charset codec.
  • target: Target charset codec.
  • decode_policy: Malformed source-input policy.
  • encode_policy: Unmappable target-output policy.
§Returns

Returns a converter configured with the supplied policies.

§Errors

Returns an error when encode_policy uses replacement and the target codec cannot encode the replacement character.

Source

pub const fn decode_policy(&self) -> CharsetDecodePolicy

Returns the configured malformed source-input policy.

§Returns

Returns the decoder policy used by this converter.

Source

pub const fn encode_policy(&self) -> CharsetEncodePolicy

Returns the configured unmappable target-output policy.

§Returns

Returns the encoder policy used by this converter.

Source

pub const fn malformed_action(&self) -> MalformedAction

Returns the configured malformed-input action.

§Returns

Returns the action used when source input is malformed.

Source

pub const fn decode_replacement(&self) -> char

Returns the configured source replacement character.

§Returns

Returns the character emitted when malformed source input is replaced.

Source

pub const fn unmappable_action(&self) -> UnmappableAction

Returns the configured unmappable-character action.

§Returns

Returns the action used when the target charset cannot represent a character.

Source

pub const fn replacement(&self) -> char

Returns the configured target replacement character.

§Returns

Returns the character encoded when unmappable target input is replaced.

Trait Implementations§

Source§

impl<D, E> BufferedConverter<<D as Codec>::Unit, <E as Codec>::Unit> for CharsetConverter<D, E>

Source§

impl<D, E> BufferedTranscoder<<D as Codec>::Unit, <E as Codec>::Unit> for CharsetConverter<D, E>

Source§

fn max_output_len(&self, input_len: usize) -> Result<usize, CapacityError>

Returns the target-side upper bound for converted output units.

Source§

fn max_finish_output_len(&self) -> Result<usize, CapacityError>

Returns the maximum target units needed to finalize pending conversion state.

Source§

fn reset(&mut self)

Clears any pending decoded character.

Source§

fn transcode( &mut self, input: &[D::Unit], input_index: usize, output: &mut [E::Unit], output_index: usize, ) -> Result<TranscodeProgress, Self::Error>

Converts source units to target units through the configured decoder and encoder.

§Errors

Returns CharsetConvertError::Decode when input_index is outside the source input buffer or source decoding fails. Returns CharsetConvertError::Encode when target encoding fails.

Source§

fn finish( &mut self, output: &mut [E::Unit], output_index: usize, ) -> Result<usize, FinishError<Self::Error>>

Finalizes internally retained decoded characters and policy hook state.

§Parameters
  • output: Complete output slice visible to the converter.
  • output_index: Absolute output index where writing starts.
§Returns

Returns the number of target units written during finalization.

§Errors

Returns FinishError when output_index is invalid, when output capacity is insufficient, or when encoding pending or final decoded characters violates target charset policy.

Source§

type Error = CharsetConvertError

Error reported for semantic conversion failures.
Source§

impl<D, E> Clone for CharsetConverter<D, E>

Source§

fn clone(&self) -> CharsetConverter<D, E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D, E> Debug for CharsetConverter<D, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D, E> Eq for CharsetConverter<D, E>
where D: CharsetCodec + Eq, E: CharsetEncodeProbe + Eq, E::Unit: Eq,

Source§

impl<D, E> PartialEq for CharsetConverter<D, E>

Source§

fn eq(&self, other: &CharsetConverter<D, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<D, E> StructuralPartialEq for CharsetConverter<D, E>

Auto Trait Implementations§

§

impl<D, E> Freeze for CharsetConverter<D, E>
where D: Freeze, E: Freeze,

§

impl<D, E> RefUnwindSafe for CharsetConverter<D, E>

§

impl<D, E> Send for CharsetConverter<D, E>
where D: Send, E: Send,

§

impl<D, E> Sync for CharsetConverter<D, E>
where D: Sync, E: Sync,

§

impl<D, E> Unpin for CharsetConverter<D, E>
where D: Unpin, E: Unpin,

§

impl<D, E> UnsafeUnpin for CharsetConverter<D, E>
where D: UnsafeUnpin, E: UnsafeUnpin,

§

impl<D, E> UnwindSafe for CharsetConverter<D, E>
where D: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.