protobuf/reflect/
name.rs
1pub(crate) fn concat_paths(a: &str, b: &str) -> String {
2 if a.is_empty() {
3 b.to_owned()
4 } else if b.is_empty() {
5 b.to_owned()
6 } else {
7 format!("{}.{}", a, b)
8 }
9}
10
11pub(crate) fn protobuf_name_starts_with_package<'a>(
12 name: &'a str,
13 package: &str,
14) -> Option<&'a str> {
15 assert!(
16 !package.starts_with("."),
17 "package must not start with dot: {}",
18 package
19 );
20
21 assert!(
22 name.starts_with("."),
23 "full name must start with dot: {}",
24 name
25 );
26 let name = &name[1..];
27 if package.is_empty() {
30 Some(name)
31 } else {
32 if name.starts_with(package) {
33 let rem = &name[package.len()..];
34 if rem.starts_with(".") {
35 Some(&rem[1..])
36 } else {
37 None
38 }
39 } else {
40 None
41 }
42 }
43}
44
45#[test]
46fn test_protobuf_name_starts_with_package() {
47 assert_eq!(
48 Some("bar"),
49 protobuf_name_starts_with_package(".foo.bar", "foo")
50 );
51 assert_eq!(None, protobuf_name_starts_with_package(".foo", "foo"));
52 assert_eq!(Some("foo"), protobuf_name_starts_with_package(".foo", ""));
53}