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::bundle::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 keyring.
///
/// This is used
#[derive(Debug)]
pub(crate) enum KeyringValidity {
/// The packet sequence is a valid keyring.
Keyring,
/// The packet sequence is a valid keyring prefix.
KeyringPrefix,
/// The packet sequence is definitely not a keyring.
Error(anyhow::Error),
}
#[allow(unused)]
impl KeyringValidity {
/// 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 {
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
/// keyring.
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 keyring.
#[derive(Debug)]
pub(crate) 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()
}
}
#[allow(unused)]
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<