Skip to content

Commit

Permalink
Replace negated condition with guard clause
Browse files Browse the repository at this point in the history
  • Loading branch information
escritorio-gustavo committed May 22, 2024
1 parent a3ed6d8 commit 121ac37
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,49 +129,50 @@ pub(crate) fn export_to<T: TS + ?Sized + 'static, P: AsRef<Path>>(path: P) -> Re
let mut lock = EXPORT_PATHS.lock().unwrap();

if let Some(entry) = lock.get_mut(&path) {
if !entry.contains(&type_name) {
let (header, decl) = buffer.split_once("\n\n").unwrap();
let imports = if header.len() > NOTE.len() {
&header[NOTE.len()..]
} else {
""
};

let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(&path)?;
if entry.contains(&type_name) {
return Ok(());
}

file.seek(SeekFrom::Start(NOTE.len().try_into().unwrap()))?;
let (header, decl) = buffer.split_once("\n\n").unwrap();
let imports = if header.len() > NOTE.len() {
&header[NOTE.len()..]
} else {
""
};

let content_len = usize::try_from(file.metadata()?.len()).unwrap() - NOTE.len();
let mut original_contents = String::with_capacity(content_len);
file.read_to_string(&mut original_contents)?;
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(&path)?;

let imports = imports
.lines()
.filter(|x| !original_contents.contains(x))
.collect::<String>();
file.seek(SeekFrom::Start(NOTE.len().try_into().unwrap()))?;

file.seek(SeekFrom::Start(NOTE.len().try_into().unwrap()))?;
let content_len = usize::try_from(file.metadata()?.len()).unwrap() - NOTE.len();
let mut original_contents = String::with_capacity(content_len);
file.read_to_string(&mut original_contents)?;

let buffer_size =
imports.as_bytes().len() + decl.as_bytes().len() + content_len + 3;
let imports = imports
.lines()
.filter(|x| !original_contents.contains(x))
.collect::<String>();

let mut buffer = String::with_capacity(buffer_size);
file.seek(SeekFrom::Start(NOTE.len().try_into().unwrap()))?;

buffer.push_str(&imports);
if !imports.is_empty() {
buffer.push('\n');
}
buffer.push_str(&original_contents);
buffer.push_str("\n\n");
buffer.push_str(decl);
let buffer_size = imports.as_bytes().len() + decl.as_bytes().len() + content_len + 3;

file.write_all(buffer.as_bytes())?;
let mut buffer = String::with_capacity(buffer_size);

entry.insert(type_name);
buffer.push_str(&imports);
if !imports.is_empty() {
buffer.push('\n');
}
buffer.push_str(&original_contents);
buffer.push_str("\n\n");
buffer.push_str(decl);

file.write_all(buffer.as_bytes())?;

entry.insert(type_name);
} else {
let mut file = File::create(&path)?;
file.write_all(buffer.as_bytes())?;
Expand Down

0 comments on commit 121ac37

Please sign in to comment.