Fix CratesToml::write_to_{writer, file}

Make them consistent with `Crates2Json::write_to_{writer, file}`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-22 22:55:44 +10:00
parent 0a753f3e4b
commit 6ce48419b6
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -47,18 +47,16 @@ impl CratesToml {
self.write_to_path(Self::default_path()?) self.write_to_path(Self::default_path()?)
} }
pub fn write_to_writer<W: io::Write>( pub fn write_to_writer<W: io::Write>(&self, mut writer: W) -> Result<(), CratesTomlParseError> {
&self,
mut writer: W,
) -> Result<u64, CratesTomlParseError> {
let data = toml::to_vec(&self)?; let data = toml::to_vec(&self)?;
writer.write_all(&data)?; writer.write_all(&data)?;
Ok(data.len().try_into().unwrap()) Ok(())
} }
pub fn write_to_file(&self, file: &mut fs::File) -> Result<(), CratesTomlParseError> { pub fn write_to_file(&self, file: &mut fs::File) -> Result<(), CratesTomlParseError> {
let cnt = self.write_to_writer(&mut *file)?; self.write_to_writer(&mut *file)?;
file.set_len(cnt)?; let pos = file.stream_position()?;
file.set_len(pos)?;
Ok(()) Ok(())
} }