{"action":"created","comment":{"url":"https://api.github.com/repos/ardite/accelerate/pulls/comments/53539318","id":53539318,"diff_hunk":"@@ -0,0 +1,299 @@\n+//! This module deals with the \"outside world\". Anything which can have a\n+//! reversible string patch applied may be accelerated.\n+\n+use std::error::Error;\n+use motions::Motion;\n+use operation::Operation;\n+use operation::Operation::*;\n+\n+/// A driver is anything which may be accelerated. For example a database like\n+/// PostgreSQL or MySQL. Even a taco can be accelerated if it adopts this\n+/// interface.\n+pub trait Driver {\n+  /// The error type for the driver. Must inherit from the standard library\n+  /// error trait.\n+  type E: Error;\n+\n+  /// Setup the state within the driver database. Often tables or other\n+  /// structures will need to be setup. Called every time the accelerate tool\n+  /// is used.\n+  fn init_state(&self) -> Result<(), Self::E> {\n+    Ok(())\n+  }\n+\n+  /// Gets the integer for the last motion applied.\n+  fn get_status(&self) -> Result<isize, Self::E>;\n+\n+  /// Sets the integer for the last motion applied.\n+  fn set_status(&mut self, status: isize) -> Result<(), Self::E>;\n+\n+  /// Executes a motion string. Does not discriminate based on the add or sub\n+  /// operation.\n+  fn execute_motion(&self, motion: &String) -> Result<(), Self::E>;\n+\n+  /// Executes a set range of motions on the driver. This function is not to be\n+  /// confused with the driver execute function which only executes a single\n+  /// motion. This function provides the logic for deciding execution order.\n+  ///\n+  /// This function must be given the driver implementation and *all* known\n+  /// motions.\n+  ///\n+  /// Motions will always be applied moving from start to finish. So if start is\n+  /// a larger number then finish we will execute all the \"sub\" motions until we\n+  /// reach finish, and if the start number is smaller we will execute all the\n+  /// \"add\" motions until we reach finish.\n+  fn execute(&mut self, motions: &Vec<Motion>, some_start: isize, some_finish: isize) -> Result<(), Self::E> {\n+    let start = clamp(some_start, 0, motions.len() as isize);\n+    let finish = clamp(some_finish, 0, motions.len() as isize);\n+    if start != finish {\n+      let operation = Operation::get(finish, start);\n+      let mut i = start;\n+      loop {\n+        if i == finish {\n+          break;\n+        }\n+\n+        if operation == Operation::Sub {\n+          i += Operation::sub();\n+        }\n+\n+        match self.execute_motion(&motions[i as usize].add) {\n+          Ok(_) => (),\n+          // Handle execution error by first trying to set the new status.\n+          Err(err) => {\n+            try!(self.set_status(i));\n+            return Err(err);\n+          }\n+        }\n+\n+        if operation == Operation::Add {\n+          i += Operation::add();\n+        }\n+      }\n+      self.set_status(i)\n+    } else {\n+      Ok(())\n+    }\n+  }\n+\n+  /// Starts at the current driver status and executes n motions in either the\n+  /// \"add\" or \"sub\" direction.\n+  fn shift(&mut self, motions: &Vec<Motion>, n: isize) -> Result<(), Self::E> {\n+    let start = try!(self.get_status());\n+    let finish = clamp(start + n, 0, motions.len() as isize);\n+    self.execute(motions, start, finish)\n+  }\n+\n+\n+  /// Executes as many motions as necessary to move the driver from its current\n+  /// status to a certain state.\n+  fn goto(&mut self, motions: &Vec<Motion>, finish: isize) -> Result<(), Self::E> {\n+    let status = try!(self.get_status());\n+    self.execute(motions, status, finish)\n+  }\n+\n+  /// \"sub's\" the last motion then \"add's\" it back.\n+  fn redo(&mut self, motions: &Vec<Motion>) -> Result<(), Self::E> {\n+    try!(self.shift(motions, -1));\n+    self.shift(motions, 1)\n+  }\n+\n+  /// Applies all remaining \"add\" motions to the driver.\n+  fn up(&mut self, motions: &Vec<Motion>) -> Result<(), Self::E> {\n+    let last = motions.len() as isize;\n+    self.goto(motions, last)\n+  }\n+\n+  /// Applies all remaining \"sub\" motions to the driver.\n+  fn down(&mut self, motions: &Vec<Motion>) -> Result<(), Self::E> {\n+    self.goto(motions, 0)\n+  }\n+\n+  /// Applies all remaining \"sub\" motions to the driver before adding them all\n+  /// the subbed motions back.\n+  fn reset(&mut self, motions: &Vec<Motion>) -> Result<(), Self::E> {\n+    let status = try!(self.get_status());\n+    try!(self.execute(motions, status, 0));\n+    self.execute(motions, 0, status)\n+  }\n+}\n+\n+/// Forces an integer to be inside a range. Similar to using both the algebraic\n+/// `min` and `max` functions with the specified values.\n+fn clamp(n: isize, min: isize, max: isize) -> isize {\n+  if n < min {\n+    return min;\n+  }\n+  if n > max {\n+    return max;\n+  }\n+  n\n+}\n+\n+#[cfg(test)]\n+mod tests {\n+  #![allow(unused_must_use)]","path":"src/driver.rs","position":135,"original_position":135,"commit_id":"0173ec32196bde38a1ffac34cfa1041af7eb3645","original_commit_id":"0173ec32196bde38a1ffac34cfa1041af7eb3645","user":{"login":"svmnotn","id":12618337,"avatar_url":"https://avatars.githubusercontent.com/u/12618337?v=3","gravatar_id":"","url":"https://api.github.com/users/svmnotn","html_url":"https://github.com/svmnotn","followers_url":"https://api.github.com/users/svmnotn/followers","following_url":"https://api.github.com/users/svmnotn/following{/other_user}","gists_url":"https://api.github.com/users/svmnotn/gists{/gist_id}","starred_url":"https://api.github.com/users/svmnotn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/svmnotn/subscriptions","organizations_url":"https://api.github.com/users/svmnotn/orgs","repos_url":"https://api.github.com/users/svmnotn/repos","events_url":"https://api.github.com/users/svmnotn/events{/privacy}","received_events_url":"https://api.github.com/users/svmnotn/received_events","type":"User","site_admin":false},"body":"Why?","created_at":"2016-02-20T00:57:21Z","updated_at":"2016-02-20T00:57:21Z","html_url":"https://github.com/ardite/accelerate/pull/9#discussion_r53539318","pull_request_url":"https://api.github.com/repos/ardite/accelerate/pulls/9","_links":{"self":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/comments/53539318"},"html":{"href":"https://github.com/ardite/accelerate/pull/9#discussion_r53539318"},"pull_request":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/9"}}},"pull_request":{"url":"https://api.github.com/repos/ardite/accelerate/pulls/9","id":57014458,"html_url":"https://github.com/ardite/accelerate/pull/9","diff_url":"https://github.com/ardite/accelerate/pull/9.diff","patch_url":"https://github.com/ardite/accelerate/pull/9.patch","issue_url":"https://api.github.com/repos/ardite/accelerate/issues/9","number":9,"state":"open","locked":false,"title":"Add postgres driver to accelerate","user":{"login":"svmnotn","id":12618337,"avatar_url":"https://avatars.githubusercontent.com/u/12618337?v=3","gravatar_id":"","url":"https://api.github.com/users/svmnotn","html_url":"https://github.com/svmnotn","followers_url":"https://api.github.com/users/svmnotn/followers","following_url":"https://api.github.com/users/svmnotn/following{/other_user}","gists_url":"https://api.github.com/users/svmnotn/gists{/gist_id}","starred_url":"https://api.github.com/users/svmnotn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/svmnotn/subscriptions","organizations_url":"https://api.github.com/users/svmnotn/orgs","repos_url":"https://api.github.com/users/svmnotn/repos","events_url":"https://api.github.com/users/svmnotn/events{/privacy}","received_events_url":"https://api.github.com/users/svmnotn/received_events","type":"User","site_admin":false},"body":"- [x] Get things to compile.\r\n- [ ] Complete all functionality that existed in the JS version of the driver.\r\n- [ ] Make tests to make sure everything works.\r\n- [ ] Clean up the code.","created_at":"2016-01-24T21:32:03Z","updated_at":"2016-02-20T00:57:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ee3bbb035e2e3373c69cc09686dc560d8464f46","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ardite/accelerate/pulls/9/commits","review_comments_url":"https://api.github.com/repos/ardite/accelerate/pulls/9/comments","review_comment_url":"https://api.github.com/repos/ardite/accelerate/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ardite/accelerate/issues/9/comments","statuses_url":"https://api.github.com/repos/ardite/accelerate/statuses/0173ec32196bde38a1ffac34cfa1041af7eb3645","head":{"label":"ardite:feature/drivers/postgres","ref":"feature/drivers/postgres","sha":"0173ec32196bde38a1ffac34cfa1041af7eb3645","user":{"login":"ardite","id":16847179,"avatar_url":"https://avatars.githubusercontent.com/u/16847179?v=3","gravatar_id":"","url":"https://api.github.com/users/ardite","html_url":"https://github.com/ardite","followers_url":"https://api.github.com/users/ardite/followers","following_url":"https://api.github.com/users/ardite/following{/other_user}","gists_url":"https://api.github.com/users/ardite/gists{/gist_id}","starred_url":"https://api.github.com/users/ardite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ardite/subscriptions","organizations_url":"https://api.github.com/users/ardite/orgs","repos_url":"https://api.github.com/users/ardite/repos","events_url":"https://api.github.com/users/ardite/events{/privacy}","received_events_url":"https://api.github.com/users/ardite/received_events","type":"Organization","site_admin":false},"repo":{"id":43915648,"name":"accelerate","full_name":"ardite/accelerate","owner":{"login":"ardite","id":16847179,"avatar_url":"https://avatars.githubusercontent.com/u/16847179?v=3","gravatar_id":"","url":"https://api.github.com/users/ardite","html_url":"https://github.com/ardite","followers_url":"https://api.github.com/users/ardite/followers","following_url":"https://api.github.com/users/ardite/following{/other_user}","gists_url":"https://api.github.com/users/ardite/gists{/gist_id}","starred_url":"https://api.github.com/users/ardite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ardite/subscriptions","organizations_url":"https://api.github.com/users/ardite/orgs","repos_url":"https://api.github.com/users/ardite/repos","events_url":"https://api.github.com/users/ardite/events{/privacy}","received_events_url":"https://api.github.com/users/ardite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ardite/accelerate","description":"Accelerate back and forth through time for your database or other in-place systems","fork":false,"url":"https://api.github.com/repos/ardite/accelerate","forks_url":"https://api.github.com/repos/ardite/accelerate/forks","keys_url":"https://api.github.com/repos/ardite/accelerate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ardite/accelerate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ardite/accelerate/teams","hooks_url":"https://api.github.com/repos/ardite/accelerate/hooks","issue_events_url":"https://api.github.com/repos/ardite/accelerate/issues/events{/number}","events_url":"https://api.github.com/repos/ardite/accelerate/events","assignees_url":"https://api.github.com/repos/ardite/accelerate/assignees{/user}","branches_url":"https://api.github.com/repos/ardite/accelerate/branches{/branch}","tags_url":"https://api.github.com/repos/ardite/accelerate/tags","blobs_url":"https://api.github.com/repos/ardite/accelerate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ardite/accelerate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ardite/accelerate/git/refs{/sha}","trees_url":"https://api.github.com/repos/ardite/accelerate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ardite/accelerate/statuses/{sha}","languages_url":"https://api.github.com/repos/ardite/accelerate/languages","stargazers_url":"https://api.github.com/repos/ardite/accelerate/stargazers","contributors_url":"https://api.github.com/repos/ardite/accelerate/contributors","subscribers_url":"https://api.github.com/repos/ardite/accelerate/subscribers","subscription_url":"https://api.github.com/repos/ardite/accelerate/subscription","commits_url":"https://api.github.com/repos/ardite/accelerate/commits{/sha}","git_commits_url":"https://api.github.com/repos/ardite/accelerate/git/commits{/sha}","comments_url":"https://api.github.com/repos/ardite/accelerate/comments{/number}","issue_comment_url":"https://api.github.com/repos/ardite/accelerate/issues/comments{/number}","contents_url":"https://api.github.com/repos/ardite/accelerate/contents/{+path}","compare_url":"https://api.github.com/repos/ardite/accelerate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ardite/accelerate/merges","archive_url":"https://api.github.com/repos/ardite/accelerate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ardite/accelerate/downloads","issues_url":"https://api.github.com/repos/ardite/accelerate/issues{/number}","pulls_url":"https://api.github.com/repos/ardite/accelerate/pulls{/number}","milestones_url":"https://api.github.com/repos/ardite/accelerate/milestones{/number}","notifications_url":"https://api.github.com/repos/ardite/accelerate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ardite/accelerate/labels{/name}","releases_url":"https://api.github.com/repos/ardite/accelerate/releases{/id}","deployments_url":"https://api.github.com/repos/ardite/accelerate/deployments","created_at":"2015-10-08T20:51:55Z","updated_at":"2016-01-23T03:05:04Z","pushed_at":"2016-02-20T00:33:08Z","git_url":"git://github.com/ardite/accelerate.git","ssh_url":"git@github.com:ardite/accelerate.git","clone_url":"https://github.com/ardite/accelerate.git","svn_url":"https://github.com/ardite/accelerate","homepage":null,"size":113,"stargazers_count":2,"watchers_count":2,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":2,"default_branch":"master"}},"base":{"label":"ardite:develop","ref":"develop","sha":"952c5e16c1916781f62f91e20f865ff0aab62483","user":{"login":"ardite","id":16847179,"avatar_url":"https://avatars.githubusercontent.com/u/16847179?v=3","gravatar_id":"","url":"https://api.github.com/users/ardite","html_url":"https://github.com/ardite","followers_url":"https://api.github.com/users/ardite/followers","following_url":"https://api.github.com/users/ardite/following{/other_user}","gists_url":"https://api.github.com/users/ardite/gists{/gist_id}","starred_url":"https://api.github.com/users/ardite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ardite/subscriptions","organizations_url":"https://api.github.com/users/ardite/orgs","repos_url":"https://api.github.com/users/ardite/repos","events_url":"https://api.github.com/users/ardite/events{/privacy}","received_events_url":"https://api.github.com/users/ardite/received_events","type":"Organization","site_admin":false},"repo":{"id":43915648,"name":"accelerate","full_name":"ardite/accelerate","owner":{"login":"ardite","id":16847179,"avatar_url":"https://avatars.githubusercontent.com/u/16847179?v=3","gravatar_id":"","url":"https://api.github.com/users/ardite","html_url":"https://github.com/ardite","followers_url":"https://api.github.com/users/ardite/followers","following_url":"https://api.github.com/users/ardite/following{/other_user}","gists_url":"https://api.github.com/users/ardite/gists{/gist_id}","starred_url":"https://api.github.com/users/ardite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ardite/subscriptions","organizations_url":"https://api.github.com/users/ardite/orgs","repos_url":"https://api.github.com/users/ardite/repos","events_url":"https://api.github.com/users/ardite/events{/privacy}","received_events_url":"https://api.github.com/users/ardite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ardite/accelerate","description":"Accelerate back and forth through time for your database or other in-place systems","fork":false,"url":"https://api.github.com/repos/ardite/accelerate","forks_url":"https://api.github.com/repos/ardite/accelerate/forks","keys_url":"https://api.github.com/repos/ardite/accelerate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ardite/accelerate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ardite/accelerate/teams","hooks_url":"https://api.github.com/repos/ardite/accelerate/hooks","issue_events_url":"https://api.github.com/repos/ardite/accelerate/issues/events{/number}","events_url":"https://api.github.com/repos/ardite/accelerate/events","assignees_url":"https://api.github.com/repos/ardite/accelerate/assignees{/user}","branches_url":"https://api.github.com/repos/ardite/accelerate/branches{/branch}","tags_url":"https://api.github.com/repos/ardite/accelerate/tags","blobs_url":"https://api.github.com/repos/ardite/accelerate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ardite/accelerate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ardite/accelerate/git/refs{/sha}","trees_url":"https://api.github.com/repos/ardite/accelerate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ardite/accelerate/statuses/{sha}","languages_url":"https://api.github.com/repos/ardite/accelerate/languages","stargazers_url":"https://api.github.com/repos/ardite/accelerate/stargazers","contributors_url":"https://api.github.com/repos/ardite/accelerate/contributors","subscribers_url":"https://api.github.com/repos/ardite/accelerate/subscribers","subscription_url":"https://api.github.com/repos/ardite/accelerate/subscription","commits_url":"https://api.github.com/repos/ardite/accelerate/commits{/sha}","git_commits_url":"https://api.github.com/repos/ardite/accelerate/git/commits{/sha}","comments_url":"https://api.github.com/repos/ardite/accelerate/comments{/number}","issue_comment_url":"https://api.github.com/repos/ardite/accelerate/issues/comments{/number}","contents_url":"https://api.github.com/repos/ardite/accelerate/contents/{+path}","compare_url":"https://api.github.com/repos/ardite/accelerate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ardite/accelerate/merges","archive_url":"https://api.github.com/repos/ardite/accelerate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ardite/accelerate/downloads","issues_url":"https://api.github.com/repos/ardite/accelerate/issues{/number}","pulls_url":"https://api.github.com/repos/ardite/accelerate/pulls{/number}","milestones_url":"https://api.github.com/repos/ardite/accelerate/milestones{/number}","notifications_url":"https://api.github.com/repos/ardite/accelerate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ardite/accelerate/labels{/name}","releases_url":"https://api.github.com/repos/ardite/accelerate/releases{/id}","deployments_url":"https://api.github.com/repos/ardite/accelerate/deployments","created_at":"2015-10-08T20:51:55Z","updated_at":"2016-01-23T03:05:04Z","pushed_at":"2016-02-20T00:33:08Z","git_url":"git://github.com/ardite/accelerate.git","ssh_url":"git@github.com:ardite/accelerate.git","clone_url":"https://github.com/ardite/accelerate.git","svn_url":"https://github.com/ardite/accelerate","homepage":null,"size":113,"stargazers_count":2,"watchers_count":2,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/9"},"html":{"href":"https://github.com/ardite/accelerate/pull/9"},"issue":{"href":"https://api.github.com/repos/ardite/accelerate/issues/9"},"comments":{"href":"https://api.github.com/repos/ardite/accelerate/issues/9/comments"},"review_comments":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/9/comments"},"review_comment":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ardite/accelerate/pulls/9/commits"},"statuses":{"href":"https://api.github.com/repos/ardite/accelerate/statuses/0173ec32196bde38a1ffac34cfa1041af7eb3645"}}}}