Skip to content

Commit f0636f0

Browse files
author
Eric Szentiványi
authored
decode ref_name (#940)
1 parent 95975c9 commit f0636f0

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

typify-impl/src/util.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,17 @@ pub(crate) fn constant_string_value(schema: &Schema) -> Option<&str> {
544544
}
545545
}
546546

547+
fn decode_segment(segment: &str) -> String {
548+
segment.replace("~1", "/").replace("~0", "~")
549+
}
550+
547551
pub(crate) fn ref_key(ref_name: &str) -> RefKey {
548552
if ref_name == "#" {
549553
RefKey::Root
550554
} else if let Some(idx) = ref_name.rfind('/') {
551-
RefKey::Def(ref_name[idx + 1..].to_string())
555+
let decoded_segment = decode_segment(&ref_name[idx + 1..]);
556+
557+
RefKey::Def(decoded_segment)
552558
} else {
553559
panic!("expected a '/' in $ref: {}", ref_name)
554560
}
@@ -901,7 +907,7 @@ mod tests {
901907
};
902908

903909
use crate::{
904-
util::{sanitize, schemas_mutually_exclusive, Case},
910+
util::{decode_segment, sanitize, schemas_mutually_exclusive, Case},
905911
Name,
906912
};
907913

@@ -1007,6 +1013,12 @@ mod tests {
10071013
assert!(schemas_mutually_exclusive(&b, &a, &BTreeMap::new()));
10081014
}
10091015

1016+
#[test]
1017+
fn test_decode_segment() {
1018+
assert_eq!(decode_segment("foo~1bar"), "foo/bar");
1019+
assert_eq!(decode_segment("foo~0bar"), "foo~bar");
1020+
}
1021+
10101022
#[test]
10111023
fn test_sanitize() {
10121024
assert_eq!(sanitize("type", Case::Snake), "type_");

0 commit comments

Comments
 (0)