Repository: jaytaph/gosub-browser
File: src/css3/new_parser.rs (Rust)

Code before:
}

#[derive(Debug, PartialEq, Clone)]
enum ComponentValue {
    /// Any token expect for `<function-token>`, `<{-token>`, `<(-token>`, and `<[-token>` (which are consumed in other higher-level objects)
    ///
    /// Note: `<}-token>`, `<)-token>`, `<]-token>`, `<bad-string-token>`, and `<bad-url-token>` are always parse errors.
    Token(Token),
    Function(Function),
    SimpleBlock(SimpleBlock),
}

impl ComponentValue {
    fn token(&self) -> Token {
        match self {
            ComponentValue::Token(t) => t.clone(),
            ComponentValue::Function(..) | ComponentValue::SimpleBlock(..) => Token::EOF,
        }
    }
}

#[derive(Debug)]
struct ValueIterator {
    pub values: Vec<ComponentValue>,
    pub position: usize,
}

impl ValueIterator {
    pub fn new(values: Vec<ComponentValue>) -> Self {
        Self {
            values,
            position: 0,
        }
    }

    pub fn lookahead(&self, offset: usize) -> ComponentValue {
        if self.position + offset >= self.values.len() {
            return ComponentValue::Token(Token::EOF);
        }

        self.values[self.position + offset].clone()
    }

    pub fn current(&self) -> ComponentValue {
        self.lookahead(0)
    }

    pub fn next(&self) -> ComponentValue {
        self.lookahead(1)
    }


Review comment (suggestion):
I used the name "CharIterator," but that was just a placeholder.  It was intended to be transitional until the time when I would be able to do

```rust
impl Iterator for CharIterator {
  type Item = bytes::Bytes;

  fn next(&mut self) -> Option<Self::Item> {
    // ...
  }
}
```

(This is what would make it a real iterator.)

I now have doubts that `CharIterator` will actually be a real iterator (implementing `Iterator`), and I'm guessing the name will change to something like `BufferQueue`.  So I'd recommend not copying the the "Iterator" name unless it's an actual iterator.  What you have here is more of a `ValueBuffer` or something.  (There's probably a better name than `ValueBuffer` that will come to mind.)

Code after:
}

#[derive(Debug, PartialEq, Clone)]
enum ComponentValue {
    /// Any token expect for `<function-token>`, `<{-token>`, `<(-token>`, and `<[-token>` (which are consumed in other higher-level objects)
    ///
    /// Note: `<}-token>`, `<)-token>`, `<]-token>`, `<bad-string-token>`, and `<bad-url-token>` are always parse errors.
    Token(Token),
    Function(Function),
    SimpleBlock(SimpleBlock),
}

impl ComponentValue {
    fn token(&self) -> Token {
        match self {
            ComponentValue::Token(t) => t.clone(),
            ComponentValue::Function(..) | ComponentValue::SimpleBlock(..) => Token::EOF,
        }
    }
}

#[derive(Debug)]
struct ValueBuffer {
    pub values: Vec<ComponentValue>,
    pub position: usize,
}

impl ValueBuffer {
    pub fn new(values: Vec<ComponentValue>) -> Self {
        Self {
            values,
            position: 0,
        }
    }

    pub fn lookahead(&self, offset: usize) -> ComponentValue {
        if self.position + offset >= self.values.len() {
            return ComponentValue::Token(Token::EOF);
        }

        self.values[self.position + offset].clone()
    }

    pub fn current(&self) -> ComponentValue {
        self.lookahead(0)
    }

    pub fn next(&self) -> ComponentValue {
        self.lookahead(1)
    }