Add first iteration of blog example

This commit is contained in:
Timothy Warren 2019-02-06 14:54:11 -05:00
parent bac5c02bd8
commit 48e6aba776
6 changed files with 217 additions and 38 deletions

1
.idea/misc.xml generated
View File

@ -34,6 +34,7 @@
<cargoProject FILE="$PROJECT_DIR$/twelve_days_of_christmas/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/trait_objects/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/gui/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/blog/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

5
.idea/rust.iml generated
View File

@ -137,9 +137,14 @@
<sourceFolder url="file://$MODULE_DIR$/gui/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gui/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/gui/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/blog/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/blog/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/blog/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/blog/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/blog/target" />
<excludeFolder url="file://$MODULE_DIR$/branches/target" />
<excludeFolder url="file://$MODULE_DIR$/closures/target" />
<excludeFolder url="file://$MODULE_DIR$/ctof/target" />

147
.idea/workspace.xml generated
View File

@ -13,7 +13,48 @@
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf />
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/blog/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="224">
<caret line="14" lean-forward="true" selection-start-line="14" selection-end-line="14" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/blog/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="624">
<caret line="39" column="5" lean-forward="true" selection-start-line="39" selection-start-column="5" selection-end-line="39" selection-end-column="5" />
<folding>
<element signature="e#276#277#0" expanded="true" />
<element signature="e#303#304#0" expanded="true" />
<element signature="e#352#353#0" expanded="true" />
<element signature="e#377#378#0" expanded="true" />
<element signature="e#879#880#0" expanded="true" />
<element signature="e#896#897#0" expanded="true" />
<element signature="e#701#702#0" expanded="true" />
<element signature="e#726#727#0" expanded="true" />
<element signature="e#983#984#0" expanded="true" />
<element signature="e#1002#1003#0" expanded="true" />
<element signature="e#856#857#0" expanded="true" />
<element signature="e#875#876#0" expanded="true" />
<element signature="e#1196#1197#0" expanded="true" />
<element signature="e#1221#1222#0" expanded="true" />
<element signature="e#1347#1348#0" expanded="true" />
<element signature="e#1366#1367#0" expanded="true" />
<element signature="e#1423#1424#0" expanded="true" />
<element signature="e#1442#1443#0" expanded="true" />
<element signature="e#1599#1600#0" expanded="true" />
<element signature="e#1615#1616#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
@ -42,8 +83,6 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/scope/src/main.rs" />
<option value="$PROJECT_DIR$/references/src/main.rs" />
<option value="$PROJECT_DIR$/slices/src/main.rs" />
<option value="$PROJECT_DIR$/structs/src/main.rs" />
<option value="$PROJECT_DIR$/rectangles/src/main.rs" />
@ -93,6 +132,8 @@
<option value="$PROJECT_DIR$/trait_objects/Cargo.toml" />
<option value="$PROJECT_DIR$/gui/src/main.rs" />
<option value="$PROJECT_DIR$/gui/src/lib.rs" />
<option value="$PROJECT_DIR$/blog/src/main.rs" />
<option value="$PROJECT_DIR$/blog/src/lib.rs" />
</list>
</option>
</component>
@ -123,6 +164,17 @@
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="blog" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="blog" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
@ -165,7 +217,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549480480153" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549482664512" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" />
@ -193,7 +245,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Cargo Command.Run gui">
<component name="RunManager" selected="Cargo Command.Run blog">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" />
@ -212,6 +264,16 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Run blog" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package blog --bin blog" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/blog" />
<envs />
<method v="2" />
</configuration>
<configuration name="Run gui" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package gui --bin gui" />
@ -232,16 +294,6 @@
<envs />
<method v="2" />
</configuration>
<configuration name="Run threads" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package threads --bin threads" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/threads" />
<envs />
<method v="2" />
</configuration>
<configuration name="Run twelve_days_of_christmas" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package twelve_days_of_christmas --bin twelve_days_of_christmas" />
@ -271,11 +323,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo Command.Run blog" />
<item itemvalue="Cargo Command.Run gui" />
<item itemvalue="Cargo Command.Test lib::tests" />
<item itemvalue="Cargo Command.Run twelve_days_of_christmas" />
<item itemvalue="Cargo Command.Run mutex" />
<item itemvalue="Cargo Command.Run threads" />
</list>
</recent_temporary>
</component>
@ -313,9 +365,10 @@
</component>
<component name="ToolWindowManager">
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.4960707" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.50392926" side_tool="true" visible="true" weight="0.26141885" />
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49586776" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.5041322" side_tool="true" visible="true" weight="0.26141885" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" />
@ -323,7 +376,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29696578" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29696578" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -331,7 +384,7 @@
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
<window_info anchor="bottom" id="Terminal" order="10" visible="true" weight="0.3428018" />
<window_info anchor="bottom" id="Terminal" order="10" weight="0.3428018" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@ -345,24 +398,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/ftoc/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="352">
<caret line="22" lean-forward="true" selection-start-line="22" selection-end-line="22" />
<folding>
<element signature="e#456#457#0" expanded="true" />
<element signature="e#488#489#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/loops/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="944">
<caret line="59" lean-forward="true" selection-start-line="59" selection-end-line="59" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/references/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="368">
@ -785,5 +820,41 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/blog/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="624">
<caret line="39" column="5" lean-forward="true" selection-start-line="39" selection-start-column="5" selection-end-line="39" selection-end-column="5" />
<folding>
<element signature="e#276#277#0" expanded="true" />
<element signature="e#303#304#0" expanded="true" />
<element signature="e#352#353#0" expanded="true" />
<element signature="e#377#378#0" expanded="true" />
<element signature="e#879#880#0" expanded="true" />
<element signature="e#896#897#0" expanded="true" />
<element signature="e#701#702#0" expanded="true" />
<element signature="e#726#727#0" expanded="true" />
<element signature="e#983#984#0" expanded="true" />
<element signature="e#1002#1003#0" expanded="true" />
<element signature="e#856#857#0" expanded="true" />
<element signature="e#875#876#0" expanded="true" />
<element signature="e#1196#1197#0" expanded="true" />
<element signature="e#1221#1222#0" expanded="true" />
<element signature="e#1347#1348#0" expanded="true" />
<element signature="e#1366#1367#0" expanded="true" />
<element signature="e#1423#1424#0" expanded="true" />
<element signature="e#1442#1443#0" expanded="true" />
<element signature="e#1599#1600#0" expanded="true" />
<element signature="e#1615#1616#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/blog/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="224">
<caret line="14" lean-forward="true" selection-start-line="14" selection-end-line="14" />
</state>
</provider>
</entry>
</component>
</project>

7
blog/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "blog"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]

81
blog/src/lib.rs Normal file
View File

@ -0,0 +1,81 @@
pub struct Post {
state: Option<Box<dyn State>>,
content: String,
}
impl Post {
pub fn new() -> Post {
Post {
state: Some(Box::new(Draft {})),
content: String::new(),
}
}
pub fn add_text(&mut self, text: &str) {
self.content.push_str(text);
}
pub fn content(&self) -> &str {
self.state.as_ref().unwrap().content(&self)
}
pub fn request_review(&mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.request_review())
}
}
pub fn approve(&mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.approve())
}
}
}
trait State {
fn request_review(self: Box<Self>) -> Box<dyn State>;
fn approve(self: Box<Self>) -> Box<dyn State>;
fn content<'a>(&self, post: &'a Post) -> &'a str {
""
}
}
struct Draft {}
impl State for Draft {
fn request_review(self: Box<Self>) -> Box<dyn State> {
Box::new(PendingReview {})
}
fn approve(self: Box<Self>) -> Box<dyn State> {
self
}
}
struct PendingReview {}
impl State for PendingReview {
fn request_review(self: Box<Self>) -> Box<dyn State> {
self
}
fn approve(self: Box<Self>) -> Box<dyn State> {
Box::new(Published {})
}
}
struct Published {}
impl State for Published {
fn request_review(self: Box<Self>) -> Box<dyn State> {
self
}
fn approve(self: Box<Self>) -> Box<dyn State> {
self
}
fn content<'a>(&self, post: &'a Post) -> &'a str {
&post.content
}
}

14
blog/src/main.rs Normal file
View File

@ -0,0 +1,14 @@
use blog::Post;
fn main() {
let mut post = Post::new();
post.add_text("I ate a salad for lunch today");
assert_eq!("", post.content());
post.request_review();
assert_eq!("", post.content());
post.approve();
assert_eq!("I ate a salad for lunch today", post.content());
}