fix: just change `as_key`

Co-authored-by: Lucas Rosa <x@rvcas.dev>
This commit is contained in:
microproofs 2023-05-31 00:27:31 -04:00
parent a6807f0bfb
commit deee50b77e
1 changed files with 8 additions and 13 deletions

View File

@ -36,7 +36,7 @@ impl<T> Definitions<T> {
/// Retrieve a definition, if it exists. /// Retrieve a definition, if it exists.
pub fn lookup(&self, reference: &Reference) -> Option<&T> { pub fn lookup(&self, reference: &Reference) -> Option<&T> {
self.inner self.inner
.get(&reference.to_json_pointer()) .get(&reference.as_key())
.map(|v| v .map(|v| v
.as_ref() .as_ref()
.expect("All registered definitions are 'Some'. 'None' state is only transient during registration") .expect("All registered definitions are 'Some'. 'None' state is only transient during registration")
@ -50,13 +50,12 @@ impl<T> Definitions<T> {
/// Erase a known definition. Does nothing if the reference is unknown. /// Erase a known definition. Does nothing if the reference is unknown.
pub fn remove(&mut self, reference: &Reference) { pub fn remove(&mut self, reference: &Reference) {
self.inner.remove(reference.as_key()); self.inner.remove(&reference.as_key());
} }
/// Insert a new definition /// Insert a new definition
pub fn insert(&mut self, reference: &Reference, schema: T) { pub fn insert(&mut self, reference: &Reference, schema: T) {
self.inner self.inner.insert(reference.as_key(), Some(schema));
.insert(reference.as_key().to_string(), Some(schema));
} }
/// Register a new definition only if it doesn't exist. This uses a strategy of /// Register a new definition only if it doesn't exist. This uses a strategy of
@ -73,10 +72,10 @@ impl<T> Definitions<T> {
let reference = Reference::from_type(type_info, type_parameters); let reference = Reference::from_type(type_info, type_parameters);
let key = reference.as_key(); let key = reference.as_key();
if !self.inner.contains_key(key) { if !self.inner.contains_key(&key) {
self.inner.insert(key.to_string(), None); self.inner.insert(key.clone(), None);
let schema = build_schema(self)?; let schema = build_schema(self)?;
self.inner.insert(key.to_string(), Some(schema)); self.inner.insert(key, Some(schema));
} }
Ok(reference) Ok(reference)
@ -101,8 +100,8 @@ impl Reference {
} }
/// Turn a reference into a key suitable for lookup. /// Turn a reference into a key suitable for lookup.
pub(crate) fn as_key(&self) -> &str { pub(crate) fn as_key(&self) -> String {
self.inner.as_str() self.inner.replace("~1", "/")
} }
/// Turn a reference into a valid JSON pointer. Note that the JSON pointer specification /// Turn a reference into a valid JSON pointer. Note that the JSON pointer specification
@ -111,10 +110,6 @@ impl Reference {
pub(crate) fn as_json_pointer(&self) -> String { pub(crate) fn as_json_pointer(&self) -> String {
format!("#/definitions/{}", self.as_key().replace('/', "~1")) format!("#/definitions/{}", self.as_key().replace('/', "~1"))
} }
pub(crate) fn to_json_pointer(&self) -> String {
self.as_key().replace("~1", "/")
}
} }
impl Display for Reference { impl Display for Reference {