use std::io;
use std::mem;
use std::vec;
use std::path::Path;
use lalrpop_util::ParseError;
use crate::{
Error,
KeyHandle,
packet::Tag,
Packet,
parse::{
Parse,
PacketParserResult,
PacketParser
},
Result,
cert::components::ComponentBundle,
Cert,
};
mod low_level;
use low_level::{
Lexer,
CertParser as CertLowLevelParser,
CertParserError,
Token,
parse_error_downcast,
};
use super::TRACE;
/// Whether a packet sequence is a valid key ring.
#[derive(Debug)]
pub enum KeyringValidity {
/// The packet sequence is a valid key ring.
Keyring,
/// The packet sequence is a valid key ring prefix.
KeyringPrefix,
/// The packet sequence is definitely not a key ring.
Error(anyhow::Error),
}
impl KeyringValidity {
/// Returns whether the packet sequence is a valid key ring.
///
/// Note: a `KeyringValidator` will only return this after
/// `KeyringValidator::finish` has been called.
pub fn is_keyring(&self) -> bool {
if let KeyringValidity::Keyring = self {
true
} else {
false
}
}
/// Returns whether the packet sequence is a valid Keyring prefix.
///
/// Note: a `KeyringValidator` will only return this before
/// `KeyringValidator::finish` has been called.
pub fn is_keyring_prefix(&self) -> bool {
if let KeyringValidity::KeyringPrefix = self {
true
} else {
false
}
}
/// Returns whether the packet sequence is definitely not a valid
/// key ring.
pub fn is_err(&self) -> bool {
if let KeyringValidity::Error(_) = self {
true
} else {
false
}
}
}
/// Used to help validate that a packet sequence is a valid key ring.
#[derive(Debug)]
pub struct KeyringValidator {
tokens: Vec<Token>,
n_keys: usize,
n_packets: usize,
finished: bool,
// If we know that the packet sequence is invalid.
error: Option<CertParserError>,
}
impl Default for KeyringValidator {
fn default() -> Self {
KeyringValidator::new()
}
}
impl KeyringValidator {
/// Instantiates a new `KeyringValidator`.
pub fn new() -> Self {
KeyringValidator {
tokens: vec![],
n_keys: 0,
n_packets: 0,
finished: false,
error: None,
}
}
/// Returns whether the packet sequence is a valid keyring.
///
/// Note: a `KeyringValidator` will only return this after
/// `KeyringValidator::finish` has been called.
pub fn is_keyring(&self) -> bool {
self.check().is_keyring()
}
/// Returns whether the packet sequence forms a valid keyring
/// prefix.
///
/// Note: a `KeyringValidator` will only return this before
/// `KeyringValidator::finish` has been called.
pub fn is_keyring_prefix(&self) -&