Skip to content

Commit

Permalink
Also make cargofmt happy
Browse files Browse the repository at this point in the history
  • Loading branch information
JEnoch committed Aug 30, 2023
1 parent ccf64d0 commit 30c6e31
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
22 changes: 18 additions & 4 deletions commons/zenoh-config/src/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ where
{
// if include property is present, read the file and remove properites found in file from values
let include_object = if let Some(include_path) = values.get(include_property_name) {
let Some(include_path)= include_path.as_str() else {
bail!("{}.{} : property must have string type", title, include_property_name);
let Some(include_path) = include_path.as_str() else {
bail!(
"{}.{} : property must have string type",
title,
include_property_name
);
};
let include_path_adjusted = Path::new(local_path.as_ref()).join(include_path);
let include_path_canonical = match include_path_adjusted.canonicalize() {
Expand All @@ -92,7 +96,12 @@ where
}

let Some(new_local_path) = include_path_adjusted.parent() else {
bail!("{}.{} : cannot get directory part for '{}' value", title, include_property_name, include_path);
bail!(
"{}.{} : cannot get directory part for '{}' value",
title,
include_property_name,
include_path
);
};
let mut include_object: Value = match deserialize_from_file(&include_path_adjusted) {
Ok(v) => v,
Expand All @@ -105,7 +114,12 @@ where
),
};
let Some(include_values) = include_object.as_object_mut() else {
bail!("{}.{}: included file '{}' must contain an object", title, include_property_name, include_path);
bail!(
"{}.{}: included file '{}' must contain an object",
title,
include_property_name,
include_path
);
};
let include_path = include_path.to_string();
for (k, v) in include_values.iter_mut() {
Expand Down
7 changes: 3 additions & 4 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,10 +833,9 @@ fn sift_privates(value: &mut serde_json::Value) {
}

fn load_external_plugin_config(title: &str, value: &mut Value) -> ZResult<()> {
let Some(values) = value
.as_object_mut() else {
bail!("{} must be object", title);
};
let Some(values) = value.as_object_mut() else {
bail!("{} must be object", title);
};
recursive_include(title, values, HashSet::new(), "__config__", ".")
}

Expand Down
12 changes: 9 additions & 3 deletions commons/zenoh-keyexpr/src/key_expr/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&'s str> for KeFormat<'s, S
i += 1;
}
}
let IterativeConstructor::Complete(storage) = storage else {bail!("Couldn't construct KeFormat because its Storage construction was only partial after adding the last segment.")};
let IterativeConstructor::Complete(storage) = storage else {
bail!("Couldn't construct KeFormat because its Storage construction was only partial after adding the last segment.")
};
let segments = storage.segments();
for i in 0..(segments.len() - 1) {
if segments[(i + 1)..]
Expand Down Expand Up @@ -382,7 +384,9 @@ impl<'s, Storage: IKeFormatStorage<'s>> KeFormatter<'s, Storage> {
pub fn set<S: Display>(&mut self, id: &str, value: S) -> Result<&mut Self, FormatSetError> {
use core::fmt::Write;
let segments = self.format.storage.segments();
let Some(i) = segments.iter().position(|s|s.spec.id() == id) else {return Err(FormatSetError::InvalidId)};
let Some(i) = segments.iter().position(|s| s.spec.id() == id) else {
return Err(FormatSetError::InvalidId);
};
if let Some((start, end)) = self.values.as_ref()[i] {
let end = end.get();
let shift = end - start;
Expand All @@ -398,7 +402,9 @@ impl<'s, Storage: IKeFormatStorage<'s>> KeFormatter<'s, Storage> {
match (|| {
let end = self.buffer.len();
if !(end == start && pattern.as_str() == "**") {
let Ok(ke) = keyexpr::new(&self.buffer[start..end]) else {return Err(())};
let Ok(ke) = keyexpr::new(&self.buffer[start..end]) else {
return Err(());
};
if end > u32::MAX as usize || !pattern.includes(ke) {
return Err(());
}
Expand Down
38 changes: 29 additions & 9 deletions commons/zenoh-keyexpr/src/key_expr/format/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ pub struct Parsed<'s, Storage: IKeFormatStorage<'s>> {

impl<'s, Storage: IKeFormatStorage<'s>> Parsed<'s, Storage> {
pub fn get(&self, id: &str) -> ZResult<Option<&'s keyexpr>> {
let Some(i) = self.format.storage.segments().iter().position(|s| s.spec.id() == id) else {bail!("{} has no {id} field", self.format)};
let Some(i) = self
.format
.storage
.segments()
.iter()
.position(|s| s.spec.id() == id)
else {
bail!("{} has no {id} field", self.format)
};
Ok(self.results.as_ref()[i])
}
pub fn values(&self) -> &[Option<&'s keyexpr>] {
Expand Down Expand Up @@ -92,10 +100,14 @@ impl<'s, Storage: IKeFormatStorage<'s> + 's> KeFormat<'s, Storage> {
let mut results = self.storage.values_storage(|_| None);
let Some(target) = target.strip_suffix(self.suffix) else {
if !segments.is_empty()
&& segments.iter().all(|s| s.spec.pattern() == "**")
&& self.suffix.as_bytes()[0] == b'/'
&& target == &self.suffix[1..] {
return Ok(Parsed { format: self, results });
&& segments.iter().all(|s| s.spec.pattern() == "**")
&& self.suffix.as_bytes()[0] == b'/'
&& target == &self.suffix[1..]
{
return Ok(Parsed {
format: self,
results,
});
}
bail!("{target} is not included in {self}")
};
Expand All @@ -118,8 +130,12 @@ fn do_parse<'s>(
) -> bool {
debug_assert!(!input.starts_with('/'));
// Parsing is finished if there are no more segments to process AND the input is now empty.
let [segment, segments @ ..] = segments else {return input.is_empty()};
let [result, results @ ..] = results else {unreachable!()};
let [segment, segments @ ..] = segments else {
return input.is_empty();
};
let [result, results @ ..] = results else {
unreachable!()
};
// reset result to None in case of backtracking
*result = None;
// Inspect the pattern: we want to know how many chunks we need to have a chance of inclusion, as well as if we need to worry about double wilds
Expand All @@ -145,7 +161,9 @@ fn do_parse<'s>(
);
}
// Strip the prefix (including the end-/ if the prefix is non-empty)
let Some(input) = input.strip_prefix(prefix) else {return false};
let Some(input) = input.strip_prefix(prefix) else {
return false;
};
let mut chunks = 0;
for i in (0..input.len()).filter(|i| input.as_bytes()[*i] == b'/') {
chunks += 1;
Expand Down Expand Up @@ -190,7 +208,9 @@ fn do_parse_doublewild<'s>(
return false;
}
}
let Some(input) = input.strip_prefix(prefix) else {return false};
let Some(input) = input.strip_prefix(prefix) else {
return false;
};
let input = trim_prefix_slash(input);
let mut chunks = 0;
for i in (0..input.len()).filter(|i| input.as_bytes()[*i] == b'/') {
Expand Down
12 changes: 9 additions & 3 deletions commons/zenoh-keyexpr/src/key_expr/format/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ pub(crate) struct Spec<'a> {
impl<'a> TryFrom<&'a str> for Spec<'a> {
type Error = Error;
fn try_from(spec: &'a str) -> Result<Self, Self::Error> {
let Some(id_end) = spec.find(':') else {bail!("Spec {spec} didn't contain `:`")};
let Some(id_end) = spec.find(':') else {
bail!("Spec {spec} didn't contain `:`")
};
let pattern_start = id_end + 1;
let pattern_end = spec[pattern_start..].find('#').unwrap_or(u16::MAX as usize);
if pattern_start < spec.len() {
let Ok(id_end) = id_end.try_into() else {bail!("Spec {spec} contains an id longer than {}", u16::MAX)};
let Ok(id_end) = id_end.try_into() else {
bail!("Spec {spec} contains an id longer than {}", u16::MAX)
};
if pattern_end > u16::MAX as usize {
bail!("Spec {spec} contains a pattern longer than {}", u16::MAX)
}
Expand Down Expand Up @@ -218,7 +222,9 @@ impl<'s> IKeFormatStorage<'s> for Vec<Segment<'s>> {
constructor: IterativeConstructor<Self, Self::PartialConstruct, Self::ConstructionError>,
segment: Segment<'s>,
) -> IterativeConstructor<Self, Self::PartialConstruct, Self::ConstructionError> {
let IterativeConstructor::Complete(mut this) = constructor else {unsafe {core::hint::unreachable_unchecked()}};
let IterativeConstructor::Complete(mut this) = constructor else {
unsafe { core::hint::unreachable_unchecked() }
};
this.push(segment);
IterativeConstructor::Complete(this)
}
Expand Down
5 changes: 4 additions & 1 deletion commons/zenoh-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ impl syn::parse::Parse for FormatDeclaration {
struct FormatDeclarations(syn::punctuated::Punctuated<FormatDeclaration, syn::Token!(,)>);
impl syn::parse::Parse for FormatDeclarations {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(Self(input.parse_terminated(FormatDeclaration::parse, syn::Token![,])?))
Ok(Self(input.parse_terminated(
FormatDeclaration::parse,
syn::Token![,],
)?))
}
}

Expand Down

0 comments on commit 30c6e31

Please sign in to comment.