gitea-release/src/version/cargo.rs

30 lines
760 B
Rust

use anyhow::Result;
use cargo_toml::Manifest;
use std::fs::{self, File};
use std::io::Read;
fn get_file_as_byte_vec(filename: &str) -> Option<Vec<u8>> {
let f = File::open(&filename);
if f.is_err() {
return None;
}
let mut f = f.unwrap();
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
Some(buffer)
}
pub fn read() -> Result<Option<String>> {
let bytes = get_file_as_byte_vec("Cargo.toml");
match bytes {
Some(bytes) => {
let pkg: Manifest = toml::from_slice(&bytes)?;
Ok(Some(pkg.package.unwrap().version))
}
None => Ok(None)
}
}