Struct Parser

Source
pub struct Parser<ID>
where ID: Eq + Hash + Clone + Debug,
{ /* private fields */ }
Expand description

A parser instance which receives the individual AIDL files via Parser::add_content() or Parser::add_file(). Once all the files have been added, call Parser::parser() to trigger the validation and access the results.

The ID of the files added to the parser are used to uniquely identify the results returned by the parser. It can be any value used as a key (e.g. number of string) or the location of the content (e.g. PathBuf or Uri).

The content added to the parser can be removed or replaced before or after the parsing.

Example:

use aidl_parser::{Parser, ParseFileResult};

let mut parser = Parser::new();

// Add files via ID + content
parser.add_content("id1", "<content of AIDL file #1>");
parser.add_content("id2", "<content of AIDL file #2>");
parser.add_content("id3", "<content of AIDL file #3>");

// Parse and get results
let results = parser.validate();

assert_eq!(results.len(), 3);
assert!(results.contains_key("id1"));
assert!(results.contains_key("id2"));
assert!(results.contains_key("id3"));

// Add/replace/remove files
parser.add_content("id2", "<updated content of AIDL file #2>");
parser.add_content("id4", "<content of AIDL file #4>");
parser.add_content("id5", "<content of AIDL file #5>");
parser.remove_content("id3");

// Parse again and get updated results
let results = parser.validate();

assert_eq!(results.len(), 4);
assert!(results.contains_key("id1"));
assert!(results.contains_key("id2"));
assert!(!results.contains_key("id3"));  // removed
assert!(results.contains_key("id4"));
assert!(results.contains_key("id5"));

Implementations§

Source§

impl<ID> Parser<ID>
where ID: Eq + Hash + Clone + Debug,

Source

pub fn new() -> Self

Create a new, empty parser

Source

pub fn add_content(&mut self, id: ID, content: &str)

Add a file content and its key to the parser.

This will parse the individual content and store the result internally.

Note: if a content with the same id already exists, the old content will be replaced.

Source

pub fn remove_content(&mut self, id: ID)

Remove the file with the given key

Source

pub fn validate(&self) -> HashMap<ID, ParseFileResult<ID>>

Validate the results of all files previously added to the parser and return the collected results (AST + diagnostics)

Source§

impl Parser<PathBuf>

Source

pub fn add_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Add a file to the parser and use its path as key.

If a file with the same path already exists, the old file will be replaced.

Trait Implementations§

Source§

impl<ID> Default for Parser<ID>
where ID: Eq + Hash + Clone + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<ID> Freeze for Parser<ID>

§

impl<ID> RefUnwindSafe for Parser<ID>
where ID: RefUnwindSafe,

§

impl<ID> Send for Parser<ID>
where ID: Send,

§

impl<ID> Sync for Parser<ID>
where ID: Sync,

§

impl<ID> Unpin for Parser<ID>
where ID: Unpin,

§

impl<ID> UnwindSafe for Parser<ID>
where ID: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.