-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b19103
commit 9c0afd1
Showing
15 changed files
with
640 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
[package] | ||
name = "printing" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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,42 @@ | ||
mod media; | ||
use media::*; | ||
|
||
fn main() { | ||
let mut archive = | ||
WebArchive::new("https://library.example.gov", "best-libarian", "b00k5!"); | ||
|
||
archive.register(NewsArticle::new( | ||
"Penguins win the Stanley Cup Championship!", | ||
"The Pittsburgh Penguins once again are the best hockey team in the NHL.", | ||
)); | ||
|
||
archive.register(Book::new( | ||
"Alice's Adventures in Wonderland", | ||
"Lewis Carroll", | ||
"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?”" | ||
)); | ||
|
||
archive.register(( | ||
Book::new("Frankenstein", "Mary Shelley", "You will rejoice to hear that no disaster has accompanied the commencement of an enterprise which you have regarded with such evil forebodings. I arrived here yesterday, and my first task is to assure my dear sister of my welfare and increasing confidence in the success of my undertaking."), | ||
NewsArticle::new( | ||
"Frankenstein is real!", | ||
"Find out where he's been living all these years.", | ||
), | ||
)); | ||
|
||
archive.register(vec![ | ||
NewsArticle::new("Queen Elizabeth II", "The British Monarch is a figurehead of the British people."), | ||
NewsArticle::new("Queen Elizabeth II dies at 96", "The British Monarch has passed away."), | ||
NewsArticle::new("Thousands pay Tribute as Britain Says Final Farewell to Its Queen", "More than 100 world leaders and dignitaries are expected to attend the funeral of Queen Elizabeth II."), | ||
]); | ||
|
||
archive.register( | ||
("William Shakespeare", vec![ | ||
Play::new("Romeo and Juliet", "William Shakespeare", "Romeo: But, soft! what light through yonder window breaks?"), | ||
Play::new("Hamlet", "William Shakespeare", "Hamlet: To be, or not to be, that is the question:"), | ||
Play::new("Macbeth", "William Shakespeare", "Macbeth: Is this a dagger which I see before me, The handle toward my hand?") | ||
]), | ||
); | ||
|
||
archive.publish(); | ||
} |
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,126 @@ | ||
pub struct WebArchive {} | ||
|
||
impl WebArchive { | ||
pub fn new( | ||
_domain: impl ToString, | ||
_username: impl ToString, | ||
_password: impl ToString, | ||
) -> Self { | ||
WebArchive {} | ||
} | ||
|
||
pub fn register<T: Summary>(&mut self, item: T) { | ||
println!("Registered: {}", item.summarize()); | ||
} | ||
|
||
pub fn publish(self) { | ||
println!("Published!"); | ||
} | ||
} | ||
|
||
pub trait Summary { | ||
fn summarize(&self) -> String; | ||
} | ||
|
||
pub struct NewsArticle { | ||
pub headline: String, | ||
pub content: String, | ||
} | ||
|
||
impl NewsArticle { | ||
pub fn new(headline: impl ToString, content: impl ToString) -> Self { | ||
NewsArticle { | ||
headline: headline.to_string(), | ||
content: content.to_string(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Play { | ||
pub title: String, | ||
pub author: String, | ||
pub content: String, | ||
} | ||
|
||
impl Play { | ||
pub fn new( | ||
title: impl ToString, | ||
author: impl ToString, | ||
content: impl ToString, | ||
) -> Self { | ||
Play { | ||
title: title.to_string(), | ||
author: author.to_string(), | ||
content: content.to_string(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Book { | ||
pub title: String, | ||
pub author: String, | ||
pub content: String, | ||
} | ||
|
||
impl Book { | ||
pub fn new( | ||
title: impl ToString, | ||
author: impl ToString, | ||
content: impl ToString, | ||
) -> Self { | ||
Self { | ||
title: title.to_string(), | ||
author: author.to_string(), | ||
content: content.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Summary for String { | ||
fn summarize(&self) -> String { | ||
self.clone() | ||
} | ||
} | ||
|
||
impl Summary for Play { | ||
fn summarize(&self) -> String { | ||
format!("\"{}\" by {}", self.title, self.author) | ||
} | ||
} | ||
|
||
impl Summary for Book { | ||
fn summarize(&self) -> String { | ||
format!("{}, by {}", self.title, self.author) | ||
} | ||
} | ||
|
||
impl Summary for NewsArticle { | ||
fn summarize(&self) -> String { | ||
format!("{}", self.headline) | ||
} | ||
} | ||
|
||
impl<T: Summary, U: Summary> Summary for (T, U) { | ||
fn summarize(&self) -> String { | ||
format!("({}, {})", self.0.summarize(), self.1.summarize()) | ||
} | ||
} | ||
|
||
impl<U: Summary> Summary for (usize, U) { | ||
fn summarize(&self) -> String { | ||
format!("(DOI: {}, {})", self.0, self.1.summarize()) | ||
} | ||
} | ||
|
||
impl<T: Summary> Summary for Vec<T> { | ||
fn summarize(&self) -> String { | ||
format!( | ||
"[{}]", | ||
self | ||
.iter() | ||
.map(|item| item.summarize()) | ||
.collect::<Vec<_>>() | ||
.join(", ") | ||
) | ||
} | ||
} |
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.