Skip to content

Commit

Permalink
Replace ouroboros with self_cell (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Voultapher committed Jun 19, 2021
1 parent ec2cc17 commit f1f0766
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
6 changes: 3 additions & 3 deletions fluent-bundle/Cargo.toml
Expand Up @@ -29,11 +29,11 @@ include = [
fluent-langneg = "0.13"
fluent-syntax = { version = "0.11", path = "../fluent-syntax" }
intl_pluralrules = "7.0.1"
ouroboros = "0.9"
smallvec = "1"
unic-langid = "0.9"
intl-memoizer = { version = "0.5", path = "../intl-memoizer" }
rustc-hash = "1"
self_cell = "0.9"
smallvec = "1"
unic-langid = "0.9"

[dev-dependencies]
criterion = "0.3"
Expand Down
54 changes: 27 additions & 27 deletions fluent-bundle/src/resource.rs
@@ -1,15 +1,20 @@
use fluent_syntax::ast;
use fluent_syntax::parser::{parse_runtime, ParserError};
use ouroboros::self_referencing;

#[self_referencing]
#[derive(Debug)]
pub struct InnerFluentResource {
string: String,
#[borrows(string)]
#[covariant]
ast: ast::Resource<&'this str>,
}
use self_cell::self_cell;

type Resource<'s> = ast::Resource<&'s str>;

self_cell!(
pub struct InnerFluentResource {
owner: String,

#[covariant]
dependent: Resource,
}

impl {Debug}
);

/// A resource containing a list of localization messages.
///
Expand Down Expand Up @@ -77,22 +82,17 @@ impl FluentResource {
pub fn try_new(source: String) -> Result<Self, (Self, Vec<ParserError>)> {
let mut errors = None;

let res = InnerFluentResourceBuilder {
string: source,
ast_builder: |string: &str| match parse_runtime(string) {
Ok(ast) => ast,
Err((ast, err)) => {
errors = Some(err);
ast
}
},
}
.build();
let res = InnerFluentResource::new(source, |source| match parse_runtime(source.as_str()) {
Ok(ast) => ast,
Err((ast, err)) => {
errors = Some(err);
ast
}
});

if let Some(errors) = errors {
Err((Self(res), errors))
} else {
Ok(Self(res))
match errors {
None => Ok(Self(res)),
Some(err) => Err((Self(res), err)),
}
}

Expand All @@ -115,7 +115,7 @@ impl FluentResource {
/// );
/// ```
pub fn source(&self) -> &str {
&self.0.borrow_string()
&self.0.borrow_owner()
}

/// Returns an iterator over [`entries`](fluent_syntax::ast::Entry) of the [`FluentResource`].
Expand All @@ -142,7 +142,7 @@ impl FluentResource {
/// assert!(matches!(resource.entries().next(), Some(ast::Entry::Message(_))));
/// ```
pub fn entries(&self) -> impl Iterator<Item = &ast::Entry<&str>> {
self.0.borrow_ast().body.iter()
self.0.borrow_dependent().body.iter()
}

/// Returns an [`Entry`](fluent_syntax::ast::Entry) at the
Expand All @@ -166,6 +166,6 @@ impl FluentResource {
/// assert!(matches!(resource.get_entry(0), Some(ast::Entry::Message(_))));
/// ```
pub fn get_entry(&self, idx: usize) -> Option<&ast::Entry<&str>> {
self.0.borrow_ast().body.get(idx)
self.0.borrow_dependent().body.get(idx)
}
}

0 comments on commit f1f0766

Please sign in to comment.