Struct protobuf_parse::Parser

source ·
pub struct Parser { /* private fields */ }
Expand description

Configure and invoke .proto parser.

Implementations§

source§

impl Parser

source

pub fn new() -> Parser

Create new default configured parser.

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 22)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}
source

pub fn pure(&mut self) -> &mut Self

Use pure rust parser.

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 28)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}
source

pub fn protoc(&mut self) -> &mut Self

Use protoc for parsing.

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 25)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}
source

pub fn include(&mut self, include: impl AsRef<Path>) -> &mut Self

Add an include directory.

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 33)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}
source

pub fn includes( &mut self, includes: impl IntoIterator<Item = impl AsRef<Path>> ) -> &mut Self

Add include directories.

source

pub fn input(&mut self, input: impl AsRef<Path>) -> &mut Self

Append a .proto file path to compile

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 32)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}
source

pub fn inputs( &mut self, inputs: impl IntoIterator<Item = impl AsRef<Path>> ) -> &mut Self

Append multiple .proto file paths to compile

source

pub fn protoc_path(&mut self, protoc: &Path) -> &mut Self

Specify protoc path used for parsing.

This is ignored if pure rust parser is used.

source

pub fn protoc_extra_args( &mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>> ) -> &mut Self

Extra arguments to pass to protoc command (like experimental options).

This is ignored if pure rust parser is used.

source

pub fn capture_stderr(&mut self) -> &mut Self

Capture stderr and return it in error.

This option applies only to protoc parser. By default protoc stderr is inherited from this process stderr.

source

pub fn parse_and_typecheck(&self) -> Result<ParsedAndTypechecked>

Parse .proto files and typecheck them using pure Rust parser of protoc command.

source

pub fn file_descriptor_set(&self) -> Result<FileDescriptorSet>

Parse and convert result to FileDescriptorSet.

Examples found in repository?
examples/file-descriptor-out-compare.rs (line 34)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let args = env::args().skip(1).collect::<Vec<_>>();
    let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
    let (path, out_protoc, out_pure) = match args.as_slice() {
        // Just invoke protoc.
        [path, out_protoc, out_pure] => (path, out_protoc, out_pure),
        _ => panic!("wrong args"),
    };

    for which in [Which::Pure, Which::Protoc] {
        let mut parser = Parser::new();
        match which {
            Which::Protoc => {
                parser.protoc();
            }
            Which::Pure => {
                parser.pure();
            }
        }

        parser.input(path);
        parser.include(".");
        let fds = parser.file_descriptor_set().unwrap();
        let fds = text_format::print_to_string_pretty(&fds);
        let out = match which {
            Which::Protoc => out_protoc,
            Which::Pure => out_pure,
        };
        fs::write(out, fds).unwrap();
    }
}

Trait Implementations§

source§

impl Debug for Parser

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Parser

source§

fn default() -> Parser

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

Auto Trait Implementations§

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>,

§

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>,

§

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.