You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: parser.rs
    path of file: ./repos/icu4x/components/datetime/src/pattern/reference
    
    the code of the file until where you have to start completion: // This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use super::{
    super::error::PatternError,
    super::{GenericPatternItem, PatternItem},
    GenericPattern, Pattern,
};
use crate::fields::FieldSymbol;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};

#[derive(Debug, PartialEq)]
enum Segment {
    Symbol { symbol: FieldSymbol, length: u8 },
    Literal { literal: String, quoted: bool },
}

#[derive(Debug)]
pub struct Parser<'p> {
    source: &'p str,
    state: Segment,
}

impl<'p> Parser<'p> {
    pub fn new(source: &'p str) -> Self {
        Self {
            source,
            state: Segment::Literal {
                literal: String::new(),
                quoted: false,
            },
        }
    }

    fn handle_quoted_literal(
        &mut self,
        ch: char,
        chars: &mut core::iter::Peekable<core::str::Chars>,
        result: &mut Vec<PatternItem>,
    ) -> Result<bool, PatternError> {
        if ch == '\'' {
            match (&mut self.state, chars.peek() == Some(&'\'')) {
                (
                    Segment::Literal {
                        ref mut literal, ..
                    },
                    true,
                ) => {
                    literal.push('\'');
                    chars.next();
                }
                (Segment::Literal { ref mut quoted, .. }, false) => {
                    *quoted = !*quoted;
        
    
    pub fn parse(mut self) -> Result<Vec<PatternItem>, PatternError> {
        let mut chars = self.source.chars().peekable();
        let mut result = vec![];

        while let Some(ch) = chars.next() {
            if !self.handle_quoted_literal(ch, &mut chars, &mut result)? {
                if let Ok(new_symbol) = FieldSymbol::try_from(ch) {
                    match self.state {
                        Segment::Symbol {
                            ref symbol,
                            ref mut length,
                        } if new_symbol == *symbol => {
                            *length += 1;
                        }
                        segment => {
                            Self::collect_segment(segment, &mut result)?;
                            self.state = Segment::Symbol {
                                symbol: new_symbol,
                                length: 1,
                            };
                        }
                    }
                } else {
                    match self.state {
                        Segment::Symbol { symbol, length } => {
                            result.push((symbol, length).try_into()?);
                            self.state = Segment::Literal {
                                literal: String::from(ch),
                                quoted: false,
                            };
                        }
                        Segment::Literal {
                            ref mut literal, ..
                        } => literal.push(ch),
                    }
                }
            }
        }

        Self::collect_segment(self.state, &mut result)?;

        Ok(result)
    }