Merge pull request #27 from mist-go/refactor-visibility
Refactor visibility
This commit is contained in:
@@ -2,7 +2,7 @@ use <super::task>;
|
||||
use <super::logger>;
|
||||
use <super::event>;
|
||||
|
||||
public class Application {
|
||||
pub class Application {
|
||||
String name;
|
||||
|
||||
Vec<task::Task> tasks;
|
||||
@@ -13,7 +13,7 @@ public class Application {
|
||||
bool running;
|
||||
i32 ticks;
|
||||
|
||||
public constructor(str* name) {
|
||||
pub constructor(str* name) {
|
||||
self.name = name.to_string();
|
||||
|
||||
self.tasks = new Vec();
|
||||
@@ -25,7 +25,7 @@ public class Application {
|
||||
self.ticks = 0;
|
||||
}
|
||||
|
||||
public void boot(self mut*) {
|
||||
pub void boot(self mut*) {
|
||||
self.logger.info("booting application");
|
||||
|
||||
self.running = true;
|
||||
@@ -33,7 +33,7 @@ public class Application {
|
||||
self.emit(event::Event::Startup);
|
||||
}
|
||||
|
||||
public void run(self mut*) {
|
||||
pub void run(self mut*) {
|
||||
while(self.running) {
|
||||
self.tick();
|
||||
|
||||
@@ -55,7 +55,7 @@ public class Application {
|
||||
println!("tasks: {}", self.tasks.len());
|
||||
}
|
||||
|
||||
public void shutdown(self mut*) {
|
||||
pub void shutdown(self mut*) {
|
||||
self.emit(event::Event::Shutdown);
|
||||
|
||||
self.logger.warning("shutdown");
|
||||
@@ -63,7 +63,7 @@ public class Application {
|
||||
self.running = false;
|
||||
}
|
||||
|
||||
public void register_plugin(self mut*, String name) {
|
||||
pub void register_plugin(self mut*, String name) {
|
||||
self.plugins.push(name.clone());
|
||||
|
||||
self.emit(event::Event::PluginLoaded(name));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
public enum Event {
|
||||
pub enum Event {
|
||||
Startup,
|
||||
Shutdown,
|
||||
TaskExecuted(String),
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
use <std::fmt::Formatter>;
|
||||
use <std::fmt>;
|
||||
|
||||
public trait TestLogging<T: fmt::Display>: fmt::Display {
|
||||
pub trait TestLogging<T: fmt::Display>: fmt::Display {
|
||||
String get();
|
||||
}
|
||||
|
||||
public enum LogLevel {
|
||||
pub enum LogLevel {
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
|
||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
||||
match (self) {
|
||||
LogLevel::Info => {
|
||||
return write!(f, "[INFO]");
|
||||
@@ -28,22 +29,22 @@ impl fmt::Display for LogLevel {
|
||||
}
|
||||
}
|
||||
|
||||
public class Logger {
|
||||
pub class Logger {
|
||||
String prefix;
|
||||
|
||||
public constructor(str* prefix) {
|
||||
pub constructor(str* prefix) {
|
||||
self.prefix = prefix.to_string();
|
||||
}
|
||||
|
||||
public void info(self*, str* message) {
|
||||
pub void info(self*, str* message) {
|
||||
self.log(LogLevel::Info, message);
|
||||
}
|
||||
|
||||
public void warning(self*, str* message) {
|
||||
pub void warning(self*, str* message) {
|
||||
self.log(LogLevel::Warning, message);
|
||||
}
|
||||
|
||||
public void error(self*, str* message) {
|
||||
pub void error(self*, str* message) {
|
||||
self.log(LogLevel::Error, message);
|
||||
}
|
||||
|
||||
@@ -64,7 +65,7 @@ public class Logger {
|
||||
}
|
||||
|
||||
impl fmt::Display {
|
||||
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
|
||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
||||
return write!(f, "logger ({})", self.prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
mod app;
|
||||
mod task;
|
||||
mod plugin;
|
||||
mod logger;
|
||||
mod event;
|
||||
pub mod app;
|
||||
pub mod task;
|
||||
pub mod plugin;
|
||||
pub mod logger;
|
||||
pub mod event;
|
||||
|
||||
void main() {
|
||||
var mut app_instance = new app::Application("mist-app");
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
public struct PluginInfo {
|
||||
public String name,
|
||||
public String version,
|
||||
pub struct PluginInfo {
|
||||
pub String name,
|
||||
pub String version,
|
||||
}
|
||||
|
||||
public class PluginRegistry<T> {
|
||||
pub class PluginRegistry<T> {
|
||||
T plugins;
|
||||
|
||||
public constructor(T plugins) {
|
||||
pub constructor(T plugins) {
|
||||
self.plugins = plugins;
|
||||
}
|
||||
|
||||
public T plugins(self) {
|
||||
pub T plugins(self) {
|
||||
return self.plugins;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
public enum TaskState {
|
||||
pub enum TaskState {
|
||||
Pending,
|
||||
Running,
|
||||
Finished,
|
||||
Failed,
|
||||
}
|
||||
|
||||
public struct Task {
|
||||
public String name,
|
||||
public TaskState state,
|
||||
public i32 executions,
|
||||
pub struct Task {
|
||||
pub String name,
|
||||
pub TaskState state,
|
||||
pub i32 executions,
|
||||
}
|
||||
|
||||
public Task create_task(str* name) {
|
||||
pub Task create_task(str* name) {
|
||||
return Task {
|
||||
name: name.to_string(),
|
||||
state: TaskState::Pending,
|
||||
|
||||
+3
-2
@@ -20,6 +20,7 @@ pub enum TypePostfix {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Visibility {
|
||||
Public,
|
||||
PublicTarget(Path),
|
||||
Private,
|
||||
}
|
||||
|
||||
@@ -72,8 +73,8 @@ pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TopLevelKind {
|
||||
ModAttribute,
|
||||
Import(Path),
|
||||
Mod(Identifier),
|
||||
Import(Visibility, Path),
|
||||
Mod(Visibility, Identifier),
|
||||
ImplDecl(ImplDecl),
|
||||
TraitDecl {
|
||||
visibility: Visibility,
|
||||
|
||||
+14
-14
@@ -48,9 +48,9 @@ program = {
|
||||
// MODIFIERS
|
||||
// ======================================================
|
||||
|
||||
export = { "public" }
|
||||
mutable = { "mut" }
|
||||
var = { "var" }
|
||||
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
|
||||
mutable = { "mut" }
|
||||
var = { "var" }
|
||||
|
||||
// ======================================================
|
||||
// GENERICS
|
||||
@@ -154,7 +154,7 @@ var_decl = {
|
||||
}
|
||||
|
||||
field = {
|
||||
export? ~ type_expr ~ identifier
|
||||
visibility? ~ type_expr ~ identifier
|
||||
}
|
||||
|
||||
field_list = {
|
||||
@@ -170,11 +170,11 @@ param_list = {
|
||||
// ======================================================
|
||||
|
||||
import = {
|
||||
"use" ~ "<" ~ static_path ~ ">" ~ semicolon
|
||||
visibility? ~ "use" ~ "<" ~ static_path ~ ">" ~ semicolon
|
||||
}
|
||||
|
||||
mod_package = {
|
||||
"mod" ~ identifier ~ semicolon
|
||||
visibility? ~ "mod" ~ identifier ~ semicolon
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -182,7 +182,7 @@ mod_package = {
|
||||
// ======================================================
|
||||
|
||||
struct_decl = {
|
||||
export? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}"
|
||||
visibility? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -212,7 +212,7 @@ enum_fields = _{
|
||||
}
|
||||
|
||||
enum_decl = {
|
||||
export? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}"
|
||||
visibility? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -220,7 +220,7 @@ enum_decl = {
|
||||
// ======================================================
|
||||
|
||||
class_constructor = {
|
||||
export? ~ "constructor" ~ generics? ~ "(" ~ param_list? ~ ")" ~ block
|
||||
visibility? ~ "constructor" ~ generics? ~ "(" ~ param_list? ~ ")" ~ block
|
||||
}
|
||||
|
||||
class_fields = {
|
||||
@@ -236,7 +236,7 @@ self_param = {
|
||||
}
|
||||
|
||||
method = {
|
||||
export? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
|
||||
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
|
||||
}
|
||||
|
||||
class_item = _{
|
||||
@@ -245,7 +245,7 @@ class_item = _{
|
||||
}
|
||||
|
||||
class_decl = {
|
||||
export? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
|
||||
visibility? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -265,7 +265,7 @@ impl_decl = {
|
||||
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
|
||||
|
||||
method_no_body = {
|
||||
export? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon
|
||||
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon
|
||||
}
|
||||
|
||||
trait_item = _{
|
||||
@@ -274,7 +274,7 @@ trait_item = _{
|
||||
}
|
||||
|
||||
trait_decl = {
|
||||
export? ~ "trait" ~ identifier ~ generics? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
|
||||
visibility? ~ "trait" ~ identifier ~ generics? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -282,7 +282,7 @@ trait_decl = {
|
||||
// ======================================================
|
||||
|
||||
function_decl = {
|
||||
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||
visibility? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
||||
+17
-7
@@ -260,7 +260,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::import => TopLevelKind::Import(Path::from(inner.next().unwrap())),
|
||||
Rule::import => TopLevelKind::Import(
|
||||
Visibility::from(&mut inner),
|
||||
Path::from(inner.next().unwrap()),
|
||||
),
|
||||
|
||||
Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
|
||||
|
||||
@@ -301,7 +304,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
fields: inner.map(EnumItem::from).collect(),
|
||||
},
|
||||
|
||||
Rule::mod_package => TopLevelKind::Mod(Identifier::from(inner.next().unwrap())),
|
||||
Rule::mod_package => TopLevelKind::Mod(
|
||||
Visibility::from(&mut inner),
|
||||
Identifier::from(inner.next().unwrap()),
|
||||
),
|
||||
|
||||
Rule::impl_for_decl | Rule::impl_decl => TopLevelKind::ImplDecl(ImplDecl::from(pair)),
|
||||
|
||||
@@ -791,11 +797,15 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
|
||||
impl From<&mut pest::iterators::Pairs<'_, Rule>> for Visibility {
|
||||
fn from(pairs: &mut pest::iterators::Pairs<'_, Rule>) -> Self {
|
||||
if listen_rule(pairs, Rule::export) {
|
||||
Visibility::Public
|
||||
} else {
|
||||
Visibility::Private
|
||||
}
|
||||
consume_rule(pairs, Rule::visibility)
|
||||
.map(|pair| {
|
||||
if let Some(path) = pair.into_inner().next() {
|
||||
Visibility::PublicTarget(Path::from(path))
|
||||
} else {
|
||||
Visibility::Public
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| Visibility::Private)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -312,8 +312,10 @@ impl ToRust for TopLevelKind {
|
||||
fn to_rust(&self, cg: &mut RustCodegen) {
|
||||
match self {
|
||||
Self::ModAttribute => {}
|
||||
Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())),
|
||||
Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())),
|
||||
Self::Import(vis, path) => {
|
||||
cg.addln(&format!("{} use {};", vis.get_rust(), path.get_rust()))
|
||||
}
|
||||
Self::Mod(vis, id) => cg.addln(&format!("{} mod {};", vis.get_rust(), id.get_rust())),
|
||||
Self::FunctionDecl(decl) => decl.to_rust(cg),
|
||||
Self::ImplDecl(impl_) => impl_.to_rust(cg),
|
||||
Self::StructDecl {
|
||||
@@ -739,10 +741,10 @@ impl GetRust for TypePostfix {
|
||||
impl GetRust for Visibility {
|
||||
fn get_rust(&self) -> String {
|
||||
match self {
|
||||
Visibility::Public => "pub ",
|
||||
Visibility::Private => "",
|
||||
Visibility::Public => "pub ".to_string(),
|
||||
Visibility::PublicTarget(path) => format!("pub({}) ", path.get_rust()),
|
||||
Visibility::Private => "".to_string(),
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user