Skip to content

Commit ab0a962

Browse files
itsmeoliviaCQ Bot
authored andcommitted
[cleanup] move offer fns to live in offer file
Change-Id: I849d84bf9e7cf6b7a19712a17b320373b187fc82 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1441468 Commit-Queue: Olivia Nordquist <nolivia@google.com> Reviewed-by: Claire Gonyeo <cgonyeo@google.com>
1 parent c379070 commit ab0a962

File tree

4 files changed

+272
-266
lines changed

4 files changed

+272
-266
lines changed

tools/lib/cml/src/lib.rs

Lines changed: 5 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ use maplit::{hashmap, hashset};
2424
use serde::{Deserialize, Serialize, de, ser};
2525
use serde_json::{Map, Value};
2626
use std::fmt;
27-
use std::fmt::Write;
2827
use std::hash::Hash;
2928
use std::num::NonZeroU32;
3029
use std::str::FromStr;
3130
use std::sync::Arc;
32-
use validate::offer_to_all_from_offer;
3331

3432
pub use crate::types::capability::{Capability, CapabilityFromRef, ContextCapability};
3533
pub use crate::types::capability_id::CapabilityId;
@@ -41,7 +39,9 @@ pub use crate::types::document::{
4139
};
4240
pub use crate::types::environment::{Environment, ResolverRegistration};
4341
pub use crate::types::expose::{ContextExpose, Expose};
44-
pub use crate::types::offer::{Offer, OfferFromRef, OfferToRef};
42+
pub use crate::types::offer::{
43+
Offer, OfferFromRef, OfferToAllCapability, OfferToRef, offer_to_all_from_offer,
44+
};
4545
pub use crate::types::program::Program;
4646
pub use crate::types::r#use::{Use, UseFromRef};
4747

@@ -54,7 +54,7 @@ use error::Location;
5454

5555
pub use crate::one_or_many::OneOrMany;
5656
pub use crate::translate::{CompileOptions, compile};
57-
pub use crate::validate::{CapabilityRequirements, MustUseRequirement, OfferToAllCapability};
57+
pub use crate::validate::{CapabilityRequirements, MustUseRequirement};
5858

5959
/// Parses a string `buffer` into a [Document]. `file` is used for error reporting.
6060
pub fn parse_one_document(buffer: &String, file: &std::path::Path) -> Result<Document, Error> {
@@ -144,7 +144,7 @@ pub struct OneOrManyPaths;
144144
)]
145145
pub struct OneOrManyEventScope;
146146

147-
/// A reference in an `offer to`.
147+
/// A reference in an `offer to` or `exose to`.
148148
#[derive(Debug, Deserialize, PartialEq, Eq, Hash, Clone, Serialize)]
149149
#[serde(rename_all = "snake_case")]
150150
pub enum SourceAvailability {
@@ -954,116 +954,6 @@ pub fn format_cml(buffer: &str, file: Option<&std::path::Path>) -> Result<Vec<u8
954954
.map_err(|e| Error::json5(e, file))
955955
}
956956

957-
pub fn offer_to_all_and_component_diff_sources_message<'a>(
958-
capability: impl Iterator<Item = OfferToAllCapability<'a>>,
959-
component: &str,
960-
) -> String {
961-
let mut output = String::new();
962-
let mut capability = capability.peekable();
963-
write!(&mut output, "{} ", capability.peek().unwrap().offer_type()).unwrap();
964-
for (i, capability) in capability.enumerate() {
965-
if i > 0 {
966-
write!(&mut output, ", ").unwrap();
967-
}
968-
write!(&mut output, "{}", capability.name()).unwrap();
969-
}
970-
write!(
971-
&mut output,
972-
r#" is offered to both "all" and child component "{}" with different sources"#,
973-
component
974-
)
975-
.unwrap();
976-
output
977-
}
978-
979-
pub fn offer_to_all_and_component_diff_capabilities_message<'a>(
980-
capability: impl Iterator<Item = OfferToAllCapability<'a>>,
981-
component: &str,
982-
) -> String {
983-
let mut output = String::new();
984-
let mut capability_peek = capability.peekable();
985-
986-
// Clone is needed so the iterator can be moved forward.
987-
// This doesn't actually allocate memory or copy a string, as only the reference
988-
// held by the OfferToAllCapability<'a> is copied.
989-
let first_offer_to_all = capability_peek.peek().unwrap().clone();
990-
write!(&mut output, "{} ", first_offer_to_all.offer_type()).unwrap();
991-
for (i, capability) in capability_peek.enumerate() {
992-
if i > 0 {
993-
write!(&mut output, ", ").unwrap();
994-
}
995-
write!(&mut output, "{}", capability.name()).unwrap();
996-
}
997-
write!(&mut output, r#" is aliased to "{}" with the same name as an offer to "all", but from different source {}"#, component, first_offer_to_all.offer_type_plural()).unwrap();
998-
output
999-
}
1000-
1001-
/// Returns `Ok(true)` if desugaring the `offer_to_all` using `name` duplicates
1002-
/// `specific_offer`. Returns `Ok(false)` if not a duplicate.
1003-
///
1004-
/// Returns Err if there is a validation error.
1005-
pub fn offer_to_all_would_duplicate(
1006-
offer_to_all: &Offer,
1007-
specific_offer: &Offer,
1008-
target: &cm_types::BorrowedName,
1009-
) -> Result<bool, Error> {
1010-
// Only protocols and dictionaries may be offered to all
1011-
assert!(offer_to_all.protocol.is_some() || offer_to_all.dictionary.is_some());
1012-
1013-
// If none of the pairs of the cross products of the two offer's protocols
1014-
// match, then the offer is certainly not a duplicate
1015-
if CapabilityId::from_offer_expose(specific_offer).iter().flatten().all(
1016-
|specific_offer_cap_id| {
1017-
CapabilityId::from_offer_expose(offer_to_all)
1018-
.iter()
1019-
.flatten()
1020-
.all(|offer_to_all_cap_id| offer_to_all_cap_id != specific_offer_cap_id)
1021-
},
1022-
) {
1023-
return Ok(false);
1024-
}
1025-
1026-
let to_field_matches = specific_offer.to.iter().any(
1027-
|specific_offer_to| matches!(specific_offer_to, OfferToRef::Named(c) if **c == *target),
1028-
);
1029-
1030-
if !to_field_matches {
1031-
return Ok(false);
1032-
}
1033-
1034-
if offer_to_all.from != specific_offer.from {
1035-
return Err(Error::validate(offer_to_all_and_component_diff_sources_message(
1036-
offer_to_all_from_offer(offer_to_all),
1037-
target.as_str(),
1038-
)));
1039-
}
1040-
1041-
// Since the capability ID's match, the underlying protocol must also match
1042-
if offer_to_all_from_offer(offer_to_all).all(|to_all_protocol| {
1043-
offer_to_all_from_offer(specific_offer)
1044-
.all(|to_specific_protocol| to_all_protocol != to_specific_protocol)
1045-
}) {
1046-
return Err(Error::validate(offer_to_all_and_component_diff_capabilities_message(
1047-
offer_to_all_from_offer(offer_to_all),
1048-
target.as_str(),
1049-
)));
1050-
}
1051-
1052-
Ok(true)
1053-
}
1054-
1055-
#[cfg(test)]
1056-
pub fn create_offer(
1057-
protocol_name: &str,
1058-
from: OneOrMany<OfferFromRef>,
1059-
to: OneOrMany<OfferToRef>,
1060-
) -> Offer {
1061-
Offer {
1062-
protocol: Some(OneOrMany::One(Name::from_str(protocol_name).unwrap())),
1063-
..Offer::empty(from, to)
1064-
}
1065-
}
1066-
1067957
#[cfg(test)]
1068958
mod tests {
1069959
use super::*;
@@ -2042,95 +1932,6 @@ mod tests {
20421932
assert_eq!(some.program, expected.program);
20431933
}
20441934

2045-
#[test]
2046-
fn test_offer_would_duplicate() {
2047-
let offer = create_offer(
2048-
"fuchsia.logger.LegacyLog",
2049-
OneOrMany::One(OfferFromRef::Parent {}),
2050-
OneOrMany::One(OfferToRef::Named(Name::from_str("something").unwrap())),
2051-
);
2052-
2053-
let offer_to_all = create_offer(
2054-
"fuchsia.logger.LogSink",
2055-
OneOrMany::One(OfferFromRef::Parent {}),
2056-
OneOrMany::One(OfferToRef::All),
2057-
);
2058-
2059-
// different protocols
2060-
assert!(
2061-
!offer_to_all_would_duplicate(
2062-
&offer_to_all,
2063-
&offer,
2064-
&Name::from_str("something").unwrap()
2065-
)
2066-
.unwrap()
2067-
);
2068-
2069-
let offer = create_offer(
2070-
"fuchsia.logger.LogSink",
2071-
OneOrMany::One(OfferFromRef::Parent {}),
2072-
OneOrMany::One(OfferToRef::Named(Name::from_str("not-something").unwrap())),
2073-
);
2074-
2075-
// different targets
2076-
assert!(
2077-
!offer_to_all_would_duplicate(
2078-
&offer_to_all,
2079-
&offer,
2080-
&Name::from_str("something").unwrap()
2081-
)
2082-
.unwrap()
2083-
);
2084-
2085-
let mut offer = create_offer(
2086-
"fuchsia.logger.LogSink",
2087-
OneOrMany::One(OfferFromRef::Parent {}),
2088-
OneOrMany::One(OfferToRef::Named(Name::from_str("something").unwrap())),
2089-
);
2090-
2091-
offer.r#as = Some(Name::from_str("FakeLog").unwrap());
2092-
2093-
// target has alias
2094-
assert!(
2095-
!offer_to_all_would_duplicate(
2096-
&offer_to_all,
2097-
&offer,
2098-
&Name::from_str("something").unwrap()
2099-
)
2100-
.unwrap()
2101-
);
2102-
2103-
let offer = create_offer(
2104-
"fuchsia.logger.LogSink",
2105-
OneOrMany::One(OfferFromRef::Parent {}),
2106-
OneOrMany::One(OfferToRef::Named(Name::from_str("something").unwrap())),
2107-
);
2108-
2109-
assert!(
2110-
offer_to_all_would_duplicate(
2111-
&offer_to_all,
2112-
&offer,
2113-
&Name::from_str("something").unwrap()
2114-
)
2115-
.unwrap()
2116-
);
2117-
2118-
let offer = create_offer(
2119-
"fuchsia.logger.LogSink",
2120-
OneOrMany::One(OfferFromRef::Named(Name::from_str("other").unwrap())),
2121-
OneOrMany::One(OfferToRef::Named(Name::from_str("something").unwrap())),
2122-
);
2123-
2124-
assert!(
2125-
offer_to_all_would_duplicate(
2126-
&offer_to_all,
2127-
&offer,
2128-
&Name::from_str("something").unwrap()
2129-
)
2130-
.is_err()
2131-
);
2132-
}
2133-
21341935
#[test_case(
21351936
document(json!({ "program": { "runner": "elf" } })),
21361937
document(json!({ "program": { "runner": "fle" } })),

tools/lib/cml/src/translate.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ use crate::types::environment::{
1111
DebugRegistration, Environment, EnvironmentExtends, EnvironmentRef, RunnerRegistration,
1212
};
1313
use crate::types::expose::{Expose, ExposeFromRef, ExposeToRef};
14-
use crate::types::offer::{Offer, OfferFromRef, OfferToRef, TargetAvailability};
14+
use crate::types::offer::{
15+
Offer, OfferFromRef, OfferToRef, TargetAvailability, offer_to_all_would_duplicate,
16+
};
1517
use crate::types::right::RightsClause;
1618
use crate::types::r#use::{Use, UseFromRef};
1719
use crate::validate::CapabilityRequirements;
1820
use crate::{
1921
AnyRef, AsClause, Availability, Capability, CapabilityClause, ConfigKey, ConfigNestedValueType,
2022
ConfigRuntimeSource, ConfigType, ConfigValueType, DictionaryRef, EventScope, FromClause,
2123
OneOrMany, Path, PathClause, Program, ResolverRegistration, RootDictionaryRef,
22-
SourceAvailability, offer_to_all_would_duplicate, validate,
24+
SourceAvailability, validate,
2325
};
2426
use cm_rust::NativeIntoFidl;
2527
use cm_types::{self as cm, BorrowedName, Name};
@@ -2190,9 +2192,9 @@ mod tests {
21902192
use crate::error::Error;
21912193
use crate::features::Feature;
21922194
use crate::translate::test_util::must_parse_cml;
2195+
use crate::types::offer::create_offer;
21932196
use crate::{
21942197
AsClause, CapabilityClause, Document, FromClause, OneOrMany, Path, PathClause, Program,
2195-
create_offer,
21962198
};
21972199
use assert_matches::assert_matches;
21982200
use cm_fidl_validator::error::{AvailabilityList, DeclField, Error as CmFidlError, ErrorList};

0 commit comments

Comments
 (0)