-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jasonribble/agent
Agent object
- Loading branch information
Showing
3 changed files
with
74 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::fmt; | ||
struct Agent { | ||
|
||
pub first_name: String | ||
} | ||
|
||
#[derive(Default)] | ||
struct AgentBuilder { | ||
#[allow(dead_code)] | ||
pub first_name: Option<String> | ||
} | ||
|
||
impl Agent { | ||
pub fn builder() -> AgentBuilder { | ||
AgentBuilder::default() | ||
} | ||
} | ||
|
||
impl AgentBuilder { | ||
#[allow(dead_code)] | ||
pub fn first_name(mut self, first_name: &str) -> Self { | ||
self.first_name = Some(String::from(first_name)); | ||
self | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn build(self) -> Result<Agent, &'static str> { | ||
Ok(Agent { | ||
first_name: self.first_name.ok_or("_")? | ||
}) | ||
} | ||
} | ||
|
||
impl fmt::Display for Agent { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.first_name) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Agent; | ||
|
||
#[test] | ||
fn test_agent_name() { | ||
let agent = Agent::builder().first_name("Jason").build(); | ||
|
||
assert!(agent.is_ok()) | ||
} | ||
|
||
#[test] | ||
fn test_agent_name_to_string() { | ||
let agent = Agent::builder().first_name("Jason").build().unwrap(); | ||
|
||
assert_eq!(agent.to_string(), "Jason") | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod contact; | ||
mod agent; | ||
|
||
pub use contact::Builder as ContactBuilder; | ||
pub use contact::Construct as ContactBuilder; | ||
pub use contact::Contact; | ||
pub use contact::Indexed as IndexedContact; | ||
pub use contact::Indexed as IndexedContact; |