/rust/registry/src/index.crates.io-1949cf8c6b5b557f/protobuf-3.2.0/src/reflect/name.rs
Line | Count | Source |
1 | 0 | pub(crate) fn concat_paths(a: &str, b: &str) -> String { |
2 | 0 | if a.is_empty() { |
3 | 0 | b.to_owned() |
4 | 0 | } else if b.is_empty() { |
5 | 0 | b.to_owned() |
6 | | } else { |
7 | 0 | format!("{}.{}", a, b) |
8 | | } |
9 | 0 | } |
10 | | |
11 | 0 | pub(crate) fn protobuf_name_starts_with_package<'a>( |
12 | 0 | name: &'a str, |
13 | 0 | package: &str, |
14 | 0 | ) -> Option<&'a str> { |
15 | 0 | assert!( |
16 | 0 | !package.starts_with("."), |
17 | 0 | "package must not start with dot: {}", |
18 | | package |
19 | | ); |
20 | | |
21 | 0 | assert!( |
22 | 0 | name.starts_with("."), |
23 | 0 | "full name must start with dot: {}", |
24 | | name |
25 | | ); |
26 | 0 | let name = &name[1..]; |
27 | | // assert!(!name.starts_with("."), "full name must not start with dot: {}", name); |
28 | | |
29 | 0 | if package.is_empty() { |
30 | 0 | Some(name) |
31 | | } else { |
32 | 0 | if name.starts_with(package) { |
33 | 0 | let rem = &name[package.len()..]; |
34 | 0 | if rem.starts_with(".") { |
35 | 0 | Some(&rem[1..]) |
36 | | } else { |
37 | 0 | None |
38 | | } |
39 | | } else { |
40 | 0 | None |
41 | | } |
42 | | } |
43 | 0 | } |
44 | | |
45 | | #[test] |
46 | | fn 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 | | } |