Skip to content

Commit

Permalink
Added Cache impl for &Source
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Oct 28, 2024
1 parent daf6840 commit cfe2ed2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
26 changes: 26 additions & 0 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ impl<I: AsRef<str>> Cache<()> for Source<I> {
}
}

impl<I: AsRef<str>> Cache<()> for &'_ Source<I> {
type Storage = I;

fn fetch(&mut self, _: &()) -> Result<&Source<I>, Box<dyn fmt::Debug + '_>> {
Ok(*self)
}
fn display(&self, _: &()) -> Option<Box<dyn fmt::Display>> {
None
}
}

impl<I: AsRef<str>, Id: fmt::Display + Eq> Cache<Id> for (Id, Source<I>) {
type Storage = I;

Expand All @@ -294,6 +305,21 @@ impl<I: AsRef<str>, Id: fmt::Display + Eq> Cache<Id> for (Id, Source<I>) {
}
}

impl<I: AsRef<str>, Id: fmt::Display + Eq> Cache<Id> for (Id, &'_ Source<I>) {
type Storage = I;

fn fetch(&mut self, id: &Id) -> Result<&Source<I>, Box<dyn fmt::Debug + '_>> {
if id == &self.0 {
Ok(&self.1)
} else {
Err(Box::new(format!("Failed to fetch source '{}'", id)))
}
}
fn display<'a>(&self, id: &'a Id) -> Option<Box<dyn fmt::Display + 'a>> {
Some(Box::new(id))
}
}

/// A [`Cache`] that fetches [`Source`]s from the filesystem.
#[derive(Default, Debug, Clone)]
pub struct FileCache {
Expand Down
105 changes: 52 additions & 53 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Borrow;
use std::io;
use std::ops::Range;

Expand Down Expand Up @@ -1002,9 +1001,9 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an apple
| |
| |
| `---- This is an orange
---'
"###);
Expand All @@ -1026,9 +1025,9 @@ mod tests {
,-[<unknown>:1:1]
|
1 | äpplë == örängë;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an äpplë
| |
| |
| `---- This is an örängë
---'
"###);
Expand All @@ -1050,9 +1049,9 @@ mod tests {
,-[<unknown>:1:1]
|
1 | äpplë == örängë;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an äpplë
| |
| |
| `---- This is an örängë
---'
"###);
Expand All @@ -1074,9 +1073,9 @@ mod tests {
,-[<unknown>:1:10]
|
1 | äpplë == örängë;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an äpplë
| |
| |
| `---- This is an örängë
---'
"###);
Expand All @@ -1099,7 +1098,7 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == apple == orange
| ^^|^^
| ^^|^^
| `---- This is an orange
---'
"###);
Expand All @@ -1120,7 +1119,7 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple ==
| |
| |
| `- Unexpected end of file
---'
"###);
Expand All @@ -1140,8 +1139,8 @@ mod tests {
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
1 |
| |
| `- No more fruit!
---'
"###);
Expand All @@ -1162,10 +1161,10 @@ mod tests {
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
1 |
| |
| `- No more fruit!
|
|
| Help: have you tried going to the farmer's market?
---'
"###);
Expand All @@ -1186,10 +1185,10 @@ mod tests {
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
1 |
| |
| `- No more fruit!
|
|
| Note: eat your greens!
---'
"###);
Expand All @@ -1211,12 +1210,12 @@ mod tests {
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
1 |
| |
| `- No more fruit!
|
|
| Help: have you tried going to the farmer's market?
|
|
| Note: eat your greens!
---'
"###);
Expand Down Expand Up @@ -1248,13 +1247,13 @@ mod tests {
.write_to_string(Source::from(source));
// TODO: it would be nice if the 2nd line wasn't omitted
assert_snapshot!(msg, @r###"
Error:
Error:
,-[<unknown>:1:1]
|
1 | ,-> apple
: :
: :
3 | |-> orange
| |
| |
| `----------- illegal comparison
---'
"###);
Expand All @@ -1271,13 +1270,13 @@ mod tests {
.write_to_string(Source::from(source));
// TODO: it would be nice if you could tell where the spans start and end.
assert_snapshot!(msg, @r###"
Error:
Error:
,-[<unknown>:1:1]
|
1 | https://example.com/
| ^^|^^^^^^^|^^^^^^^^^
| ^^|^^^^^^^|^^^^^^^^^
| `------------------- scheme
| |
| |
| `----------- URL
---'
"###);
Expand All @@ -1302,17 +1301,17 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an apple
| | |
| | |
| `-------------- Have I mentioned that this is an apple?
| | |
| | |
| `-------------- No really, have I mentioned that?
| |
| |
| `---- This is an orange
| |
| |
| `---- Have I mentioned that this is an orange?
| |
| |
| `---- No really, have I mentioned that?
---'
"###)
Expand All @@ -1334,11 +1333,11 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an apple
| |
| |
| `---- This is an orange
|
|
| Note: stop trying ... this is a fruitless endeavor
---'
"###)
Expand All @@ -1360,11 +1359,11 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an apple
| |
| |
| `---- This is an orange
|
|
| Help: have you tried peeling the orange?
---'
"###)
Expand All @@ -1387,13 +1386,13 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^|^^ ^^^|^^
| ^^|^^ ^^^|^^
| `-------------- This is an apple
| |
| |
| `---- This is an orange
|
|
| Help: have you tried peeling the orange?
|
|
| Note: stop trying ... this is a fruitless endeavor
---'
"###)
Expand All @@ -1414,9 +1413,9 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^^^^^^|^^^^^^^
| ^^^^^^^|^^^^^^^
| `--------- This is a strange comparison
|
|
| Note: No need to try, they can't be compared.
---'
"###)
Expand All @@ -1438,11 +1437,11 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^^^^^^|^^^^^^^
| ^^^^^^^|^^^^^^^
| `--------- This is a strange comparison
|
|
| Note 1: No need to try, they can't be compared.
|
|
| Note 2: Yeah, really, please stop.
---'
"###)
Expand All @@ -1464,11 +1463,11 @@ mod tests {
,-[<unknown>:1:1]
|
1 | apple == orange;
| ^^^^^^^|^^^^^^^
| ^^^^^^^|^^^^^^^
| `--------- This is a strange comparison
|
|
| Note 1: No need to try, they can't be compared.
|
|
| Note 2: Yeah, really, please stop.
| It has no resemblance.
---'
Expand Down

0 comments on commit cfe2ed2

Please sign in to comment.