Cleanup old modules and create more focused code

This commit is contained in:
Lucien Greathouse
2018-11-16 23:27:19 -08:00
parent e09d23d6c2
commit b4fd2e31b3
9 changed files with 46 additions and 78 deletions

1
server/Cargo.lock generated
View File

@@ -698,7 +698,6 @@ version = "0.5.0"
dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"notify 4.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -27,7 +27,6 @@ serde_json = "1.0"
notify = "4.0"
rand = "0.4"
regex = "1.0"
lazy_static = "1.0"
log = "0.4"
env_logger = "0.5"
rbx-tree = { git = "https://github.com/LPGhatguy/rbx-tree.git" }

View File

@@ -4,10 +4,20 @@ extern crate env_logger;
extern crate librojo;
use std::path::{Path, PathBuf};
use std::process;
use std::{
path::{Path, PathBuf},
process,
env,
};
use librojo::pathext::canonicalish;
fn make_path_absolute(value: &Path) -> PathBuf {
if value.is_absolute() {
PathBuf::from(value)
} else {
let current_dir = env::current_dir().unwrap();
current_dir.join(value)
}
}
fn main() {
env_logger::Builder::from_default_env()
@@ -36,7 +46,7 @@ fn main() {
("init", sub_matches) => {
let sub_matches = sub_matches.unwrap();
let project_path = Path::new(sub_matches.value_of("PATH").unwrap_or("."));
let full_path = canonicalish(project_path);
let full_path = make_path_absolute(project_path);
librojo::commands::init(&full_path);
},
@@ -44,7 +54,7 @@ fn main() {
let sub_matches = sub_matches.unwrap();
let project_path = match sub_matches.value_of("PROJECT") {
Some(v) => canonicalish(PathBuf::from(v)),
Some(v) => make_path_absolute(Path::new(v)),
None => std::env::current_dir().unwrap(),
};

View File

@@ -1,21 +0,0 @@
use std::sync::atomic::{AtomicUsize, Ordering};
/// A unique identifier, not guaranteed to be generated in any order.
pub type Id = usize;
lazy_static! {
static ref LAST_ID: AtomicUsize = AtomicUsize::new(0);
}
/// Generate a new ID, which has no defined ordering.
pub fn get_id() -> Id {
LAST_ID.fetch_add(1, Ordering::SeqCst)
}
#[test]
fn it_gives_unique_numbers() {
let a = get_id();
let b = get_id();
assert!(a != b);
}

View File

@@ -1,4 +1,3 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
#[macro_use] extern crate rouille;
#[macro_use] extern crate serde_derive;
@@ -16,14 +15,10 @@ extern crate tempfile;
// pub mod roblox_studio;
pub mod commands;
pub mod message_queue;
pub mod pathext;
pub mod project;
pub mod rbx_session;
pub mod session;
pub mod session_id;
pub mod vfs;
pub mod web;
pub mod web_util;
// TODO: Remove
pub mod id;
pub mod web_util;

View File

@@ -1,19 +1,37 @@
use std::collections::HashMap;
use std::sync::{mpsc, RwLock, Mutex};
use std::{
collections::HashMap,
sync::{
mpsc,
atomic::{AtomicUsize, Ordering},
RwLock,
Mutex,
},
};
use id::{Id, get_id};
use rbx_tree::RbxId;
/// A unique identifier, not guaranteed to be generated in any order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ListenerId(usize);
/// Generate a new ID, which has no defined ordering.
pub fn get_listener_id() -> ListenerId {
static LAST_ID: AtomicUsize = AtomicUsize::new(0);
ListenerId(LAST_ID.fetch_add(1, Ordering::SeqCst))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Message {
InstanceChanged {
id: Id,
id: RbxId,
},
}
pub struct MessageQueue {
messages: RwLock<Vec<Message>>,
message_listeners: Mutex<HashMap<Id, mpsc::Sender<()>>>,
message_listeners: Mutex<HashMap<ListenerId, mpsc::Sender<()>>>,
}
impl MessageQueue {
@@ -39,8 +57,8 @@ impl MessageQueue {
}
}
pub fn subscribe(&self, sender: mpsc::Sender<()>) -> Id {
let id = get_id();
pub fn subscribe(&self, sender: mpsc::Sender<()>) -> ListenerId {
let id = get_listener_id();
{
let mut message_listeners = self.message_listeners.lock().unwrap();
@@ -50,7 +68,7 @@ impl MessageQueue {
id
}
pub fn unsubscribe(&self, id: Id) {
pub fn unsubscribe(&self, id: ListenerId) {
{
let mut message_listeners = self.message_listeners.lock().unwrap();
message_listeners.remove(&id);

View File

@@ -1,27 +0,0 @@
use std::env::current_dir;
use std::path::{Path, PathBuf};
/// Turns the path into an absolute one, using the current working directory if
/// necessary.
pub fn canonicalish<T: AsRef<Path>>(value: T) -> PathBuf {
let cwd = current_dir().unwrap();
absoluteify(&cwd, value)
}
/// Converts the given path to be absolute if it isn't already using a given
/// root.
fn absoluteify<A, B>(root: A, value: B) -> PathBuf
where
A: AsRef<Path>,
B: AsRef<Path>,
{
let root = root.as_ref();
let value = value.as_ref();
if value.is_absolute() {
PathBuf::from(value)
} else {
root.join(value)
}
}

View File

@@ -28,11 +28,6 @@ impl RbxSession {
construct_initial_tree(&project, &temp_vfs)
};
{
use serde_json;
println!("{}", serde_json::to_string(&tree).unwrap());
}
RbxSession {
tree,
paths_to_ids,
@@ -43,15 +38,15 @@ impl RbxSession {
}
pub fn path_created_or_updated(&mut self, path: &Path) {
println!("Path changed: {}", path.display());
info!("Path changed: {}", path.display());
}
pub fn path_removed(&mut self, path: &Path) {
println!("Path removed: {}", path.display());
info!("Path removed: {}", path.display());
}
pub fn path_renamed(&mut self, from_path: &Path, to_path: &Path) {
println!("Path renamed from {} to {}", from_path.display(), to_path.display());
info!("Path renamed from {} to {}", from_path.display(), to_path.display());
}
pub fn get_tree(&self) -> &RbxTree {

View File

@@ -71,7 +71,7 @@ impl Session {
let vfs_temp = vfs.lock().unwrap();
for root in vfs_temp.get_roots() {
println!("Watching {}", root.display());
info!("Watching path {}", root.display());
let (watch_tx, watch_rx) = mpsc::channel();