Add new fn CratesToml::write_to_file

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-22 22:22:18 +10:00
parent 05115641ff
commit e1b6fb85aa
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
use super::CrateVersionSource; use super::CrateVersionSource;
use crate::cargo_home; use crate::{cargo_home, create_if_not_exist, FileLock};
#[derive(Clone, Debug, Default, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CratesToml { pub struct CratesToml {
@ -55,6 +55,13 @@ impl CratesToml {
Ok(data.len().try_into().unwrap()) Ok(data.len().try_into().unwrap())
} }
pub fn write_to_file(&self, file: &mut fs::File) -> Result<(), CratesTomlParseError> {
let cnt = self.write_to_writer(&mut *file)?;
file.set_len(cnt)?;
Ok(())
}
pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), CratesTomlParseError> { pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), CratesTomlParseError> {
fs::write(path, &toml::to_vec(&self)?)?; fs::write(path, &toml::to_vec(&self)?)?;
Ok(()) Ok(())