Function protox_parse::parse

source ·
pub fn parse(
    name: &str,
    source: &str,
) -> Result<FileDescriptorProto, ParseError>
Expand description

Parses a single protobuf source file into a FileDescriptorProto.

This function only looks at the syntax of the file, without resolving type names or reading imported files.

§Examples

let source = r#"
    syntax = "proto3";
    import "dep.proto";

    message Foo {
        Bar bar = 1;
    }
"#;
let file_descriptor = parse("foo.proto", source).unwrap();
assert_eq!(file_descriptor, FileDescriptorProto {
    name: Some("foo.proto".to_owned()),
    syntax: Some("proto3".to_owned()),
    dependency: vec!["dep.proto".to_owned()],
    message_type: vec![DescriptorProto {
        name: Some("Foo".to_owned()),
        field: vec![FieldDescriptorProto {
            label: Some(Label::Optional as _),
            name: Some("bar".to_owned()),
            number: Some(1),
            type_name: Some("Bar".to_owned()),
            ..Default::default()
        }],
        ..Default::default()
    }],
    source_code_info: Some(SourceCodeInfo {
        location: vec![
            Location { path: vec![], span: vec![1, 4, 6, 5], ..Default::default() },
            Location { path: vec![3, 0], span: vec![2, 4, 23], ..Default::default() },
            Location { path: vec![4, 0], span: vec![4, 4, 6, 5], ..Default::default() },
            Location { path: vec![4, 0, 1], span: vec![4, 12, 15], ..Default::default() },
            Location { path: vec![4, 0, 2, 0], span: vec![5, 8, 20], ..Default::default() },
            Location { path: vec![4, 0, 2, 0, 1], span: vec![5, 12, 15], ..Default::default() },
            Location { path: vec![4, 0, 2, 0, 3], span: vec![5, 18, 19], ..Default::default() },
            Location { path: vec![4, 0, 2, 0, 6], span: vec![5, 8, 11], ..Default::default() },
            Location { path: vec![12], span: vec![1, 4, 22], ..Default::default() },
        ],
    }),
    ..Default::default()
})