Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ pub fn gen_systemd_services(data: &Data, app_name: &str, user: &str) -> eyre::Re
for srv in &data.services {
let service_filename = data
.project_root
.join(format!("etc"))
.join(format!("systemd"))
.join("etc")
.join("systemd")
.join(format!("{}_{}.service", app_name, srv.name));
let mut service_file = File::create(&service_filename)?;
let v = get_systemd_service(app_name, &srv.name, user);
write!(&mut service_file, "{}", v)?;
write!(&mut service_file, "{v}")?;
}
Ok(())
}
Expand All @@ -92,8 +92,9 @@ pub fn get_error_messages(root: &Path) -> eyre::Result<ErrorMessages> {
}

if !def_filename.exists() {
let mut file = OpenOptions::new()
let _file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&def_filename)?;
}
Expand All @@ -102,15 +103,15 @@ pub fn get_error_messages(root: &Path) -> eyre::Result<ErrorMessages> {
let def_file = std::fs::read(&def_filename)?;

if def_file.is_empty() {
return Ok(ErrorMessages {
Ok(ErrorMessages {
language: String::from("TODO"),
codes: vec![ErrorMessage {
code: 0,
symbol: String::from("XXX"),
message: String::from("Please populate error_codes.json"),
source: String::from("None"),
}],
});
})
} else {
let definitions: ErrorMessages = serde_json::from_slice(&def_file)?;
Ok(definitions)
Expand Down
18 changes: 8 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn process_file(file_path: &Path) -> eyre::Result<Definition> {
let file_string = std::fs::read_to_string(file_path)?;
let config_file: Config = from_str(&file_string)?;

return Ok(config_file.definition);
Ok(config_file.definition)
}
_ => Err(eyre!(
"Non RON file OR file without extension in config dir "
Expand Down Expand Up @@ -128,13 +128,13 @@ fn build_object_lists(dir: PathBuf) -> eyre::Result<InputObjects> {
Definition::EndpointSchema(service_name, service_id, endpoint_schema) => {
service_schema_map
.entry((service_name, service_id))
.or_insert(vec![])
.or_default()
.push(endpoint_schema)
}
Definition::EndpointSchemaList(service_name, service_id, endpoint_schemas) => {
service_schema_map
.entry((service_name, service_id))
.or_insert(vec![])
.or_default()
.extend(endpoint_schemas)
}
Definition::Enum(enum_type) => enums.push(enum_type),
Expand Down Expand Up @@ -191,10 +191,8 @@ fn read_version_file(path: &Path) -> eyre::Result<VersionConfig> {
Ok(version_config)
}

fn check_compatibility(
version_config: VersionConfig
) -> eyre::Result<()> {
let current_crate_version = Version::parse(&get_crate_version()).unwrap();
fn check_compatibility(version_config: VersionConfig) -> eyre::Result<()> {
let current_crate_version = Version::parse(get_crate_version()).unwrap();

let binary_version_req = VersionReq::parse(&version_config.binary.version).unwrap();

Expand All @@ -206,8 +204,8 @@ fn check_compatibility(
let caller_libs_version = Version::parse(&version_config.libs.version).unwrap();

if !binary_version_req.matches(&current_crate_version) {
return Err(eyre!("Binary version constraint not satisfied. Version: {} is specified in version.toml. Current binary version is: {}",
&version_config.binary.version, &get_crate_version()));
Err(eyre!("Binary version constraint not satisfied. Version: {} is specified in version.toml. Current binary version is: {}",
&version_config.binary.version, &get_crate_version()))
} else if !libs_version_req.matches(&caller_libs_version) {
return Err(eyre!("endpoint-libs version constraint not satisfied. Version: {} is specified in version.toml. This version of endpoint-gen requires: {}",
caller_libs_version, libs_version_requirement));
Expand All @@ -234,7 +232,7 @@ fn main() -> Result<()> {

let config_dir = {
if let Some(config_dir) = &args.config_dir {
PathBuf::from_str(&config_dir)?
PathBuf::from_str(config_dir)?
} else {
env::current_dir()?
}
Expand Down
47 changes: 32 additions & 15 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ToRust for Type {
Type::Struct { name, .. } => name.clone(),
Type::StructRef(name) => name.clone(),
Type::Object => "serde_json::Value".to_owned(),
Type::DataTable { name, .. } => format!("Vec<{}>", name),
Type::DataTable { name, .. } => format!("Vec<{name}>"),
Type::Vec(ele) => {
format!("Vec<{}>", ele.to_rust_ref(serde_with))
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl ToRust for Type {
if serde_with_opt.is_empty() {
"".to_string()
} else {
format!("#[serde(with = \"{}\")]", serde_with_opt)
format!("#[serde(with = \"{serde_with_opt}\")]")
},
x.name,
x.ty.to_rust_ref(serde_with)
Expand Down Expand Up @@ -283,11 +283,18 @@ impl From<EnumErrorCode> for ErrorCode {{
)?;
}

let mut all_endpoints = vec![];
for s in &data.services {
for endpoint in &s.endpoints {
write!(
&mut f,
"
all_endpoints.push(endpoint);
}
}
all_endpoints.sort_by(|a, b| a.code.cmp(&b.code));

for endpoint in all_endpoints {
write!(
&mut f,
"
impl WsRequest for {end_name2}Request {{
type Response = {end_name2}Response;
const METHOD_ID: u32 = {code};
Expand All @@ -297,11 +304,10 @@ impl WsResponse for {end_name2}Response {{
type Request = {end_name2}Request;
}}
",
end_name2 = endpoint.name.to_case(Case::Pascal),
code = endpoint.code,
schema = serde_json::to_string_pretty(&endpoint).unwrap()
)?;
}
end_name2 = endpoint.name.to_case(Case::Pascal),
code = endpoint.code,
schema = serde_json::to_string_pretty(&endpoint).unwrap()
)?;
}
f.flush()?;
drop(f);
Expand Down Expand Up @@ -330,21 +336,32 @@ pub fn check_endpoint_codes(data: &Data, mut writer: impl Write) -> eyre::Result
variants.push(EnumVariant::new(e.name.clone(), e.code as _));
}
}

variants.sort_by(|a, b| a.value.cmp(&b.value));

let enum_ = Type::enum_("Endpoint", variants);
writeln!(writer, "{}", enum_.to_rust_decl(false))?;
// if it compiles, there're no duplicate codes or names
Ok(())
}
pub fn dump_endpoint_schema(data: &Data, mut writer: impl Write) -> eyre::Result<()> {
let mut cases = vec![];
let mut endpoint_cases = vec![];
for s in &data.services {
for e in &s.endpoints {
cases.push(format!(
"Self::{name} => {name}Request::SCHEMA,",
name = e.name.to_case(Case::Pascal),
endpoint_cases.push((
e.code,
format!(
"Self::{name} => {name}Request::SCHEMA,",
name = e.name.to_case(Case::Pascal),
),
));
}
}

endpoint_cases.sort_by(|a, b| a.0.cmp(&b.0));

let cases: Vec<String> = endpoint_cases.into_iter().map(|(_, case)| case).collect();

let code = format!(
r#"
impl EnumEndpoint {{
Expand All @@ -358,6 +375,6 @@ pub fn dump_endpoint_schema(data: &Data, mut writer: impl Write) -> eyre::Result
"#,
cases = cases.join("\n")
);
writeln!(writer, "{}", code)?;
writeln!(writer, "{code}")?;
Ok(())
}
1 change: 1 addition & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::all)]
pub fn get_systemd_service(app_name: &str, service_name: &str, user: &str) -> String {
format!(
r#"[Unit]
Expand Down
9 changes: 3 additions & 6 deletions src/sql.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::Data;
use endpoint_libs::model::{ProceduralFunction, Type};
use itertools::Itertools;
use std::fs::File;
use std::io::Write;

pub const PARAM_PREFIX: &str = "a_";

Expand All @@ -24,7 +21,7 @@ impl ToSql for Type {
.map(|x| format!("\"{}\" {}", x.name, x.ty.to_sql()));
format!(
"table (\n{}\n)",
fields.map(|x| format!(" {}", x)).join(",\n")
fields.map(|x| format!(" {x}")).join(",\n")
)
}
Type::StructRef(_name) => "jsonb".to_owned(),
Expand All @@ -42,8 +39,8 @@ impl ToSql for Type {
Type::Bytea => "bytea".to_owned(),
Type::UUID => "uuid".to_owned(),
Type::Inet => "inet".to_owned(),
Type::Enum { name, .. } => format!("enum_{}", name),
Type::EnumRef(name) => format!("enum_{}", name),
Type::Enum { name, .. } => format!("enum_{name}"),
Type::EnumRef(name) => format!("enum_{name}"),
// 38 digits in total, with 4-18 decimal digits. So to be exact we need 38+18 digits
Type::BlockchainDecimal => "decimal(56, 18)".to_owned(),
Type::BlockchainAddress => "varchar".to_owned(),
Expand Down
Loading