-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft sketch of ResolvedLocaleAdapter #4607
Draft
sffc
wants to merge
2
commits into
unicode-org:main
Choose a base branch
from
sffc:ResolvedLocaleAdapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
use alloc::collections::{BTreeMap, BTreeSet}; | ||
use core::cell::RefCell; | ||
|
||
use icu_provider::prelude::*; | ||
|
||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
struct ResolvedLocaleInfo { | ||
pub requested_locale: DataLocale, | ||
pub resolved_locale: Option<DataLocale>, | ||
} | ||
|
||
/// TODO: Docs | ||
#[derive(Debug)] | ||
pub struct ResolvedLocaleAdapter<P> { | ||
inner: P, | ||
resolved_locales: RefCell<BTreeMap<DataKey, ResolvedLocaleInfo>>, | ||
drop_payloads: bool, | ||
} | ||
|
||
impl<P> ResolvedLocaleAdapter<P> { | ||
pub fn into_inner(self) -> P { | ||
self.inner | ||
} | ||
|
||
pub fn clear(&mut self) { | ||
self.resolved_locales.borrow_mut().clear() | ||
} | ||
|
||
pub fn take_resolved_locale_for_key(&mut self, key: DataKey) -> Option<DataLocale> { | ||
self.resolved_locales | ||
.borrow_mut() | ||
.remove(&key) | ||
.and_then(|info| info.resolved_locale) | ||
} | ||
|
||
pub fn take_all_resolved_locales(&mut self) -> BTreeSet<DataLocale> { | ||
let map = self.resolved_locales.take(); | ||
map.into_iter() | ||
.filter_map(|(_, info)| info.resolved_locale) | ||
.collect() | ||
} | ||
|
||
pub fn saw_last_resort_fallback(&self) -> bool { | ||
self.resolved_locales.borrow().values().any(|info| { | ||
info.resolved_locale | ||
.as_ref() | ||
.map(|l| l.is_langid_und()) | ||
.unwrap_or(false) | ||
}) | ||
} | ||
} | ||
|
||
impl<P: BufferProvider> BufferProvider for ResolvedLocaleAdapter<P> { | ||
fn load_buffer( | ||
&self, | ||
key: DataKey, | ||
req: DataRequest, | ||
) -> Result<DataResponse<BufferMarker>, DataError> { | ||
let mut response = self.inner.load_buffer(key, req)?; | ||
self.resolved_locales.borrow_mut().insert( | ||
key, | ||
ResolvedLocaleInfo { | ||
requested_locale: req.locale.clone(), | ||
resolved_locale: response.metadata.locale.take(), | ||
}, | ||
); | ||
Ok(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: we shouldn't implement this, even on DataLocale, due to the same reasons as discussed in #1215. We should instead implement
Ord
only on the privateResolvedLocaleInfo
type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4608