use crate::error::PdfiumError;
use crate::pdf::path::segment::PdfPathSegment;
use std::ops::{Range, RangeInclusive};
pub type PdfPathSegmentIndex = u32;
pub trait PdfPathSegments<'a> {
fn len(&self) -> PdfPathSegmentIndex;
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
fn as_range(&self) -> Range<PdfPathSegmentIndex> {
0..self.len()
}
#[inline]
fn as_range_inclusive(&self) -> RangeInclusive<PdfPathSegmentIndex> {
if self.is_empty() {
0..=0
} else {
0..=(self.len() - 1)
}
}
fn get(&self, index: PdfPathSegmentIndex) -> Result<PdfPathSegment<'a>, PdfiumError>;
fn iter(&'a self) -> PdfPathSegmentsIterator<'a>;
}
pub struct PdfPathSegmentsIterator<'a> {
segments: &'a dyn PdfPathSegments<'a>,
next_index: PdfPathSegmentIndex,
}
impl<'a> PdfPathSegmentsIterator<'a> {
#[inline]
pub(crate) fn new(segments: &'a dyn PdfPathSegments<'a>) -> Self {
PdfPathSegmentsIterator {
segments,
next_index: 0,
}
}
}
impl<'a> Iterator for PdfPathSegmentsIterator<'a> {
type Item = PdfPathSegment<'a>;
fn next(&mut self) -> Option<Self::Item> {
let next = self.segments.get(self.next_index);
self.next_index += 1;
next.ok()
}
}