chore: Create an internal Schema::to_builder method

This commit is contained in:
Clément Renault 2019-09-16 14:13:58 +02:00
parent ee6a54fe4c
commit ba3ac5ea7b
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -99,6 +99,12 @@ struct InnerSchema {
} }
impl Schema { impl Schema {
fn to_builder(&self) -> SchemaBuilder {
let identifier = self.inner.identifier.clone();
let attributes = self.attributes_ordered();
SchemaBuilder { identifier, attributes }
}
pub fn from_toml<R: Read>(mut reader: R) -> Result<Schema, Box<dyn Error>> { pub fn from_toml<R: Read>(mut reader: R) -> Result<Schema, Box<dyn Error>> {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
reader.read_to_end(&mut buffer)?; reader.read_to_end(&mut buffer)?;
@ -107,10 +113,7 @@ impl Schema {
} }
pub fn to_toml<W: Write>(&self, mut writer: W) -> Result<(), Box<dyn Error>> { pub fn to_toml<W: Write>(&self, mut writer: W) -> Result<(), Box<dyn Error>> {
let identifier = self.inner.identifier.clone(); let builder = self.to_builder();
let attributes = self.attributes_ordered();
let builder = SchemaBuilder { identifier, attributes };
let string = toml::to_string_pretty(&builder)?; let string = toml::to_string_pretty(&builder)?;
writer.write_all(string.as_bytes())?; writer.write_all(string.as_bytes())?;
@ -125,9 +128,7 @@ impl Schema {
} }
pub fn to_json<W: Write>(&self, mut writer: W) -> Result<(), Box<dyn Error>> { pub fn to_json<W: Write>(&self, mut writer: W) -> Result<(), Box<dyn Error>> {
let identifier = self.inner.identifier.clone(); let builder = self.to_builder();
let attributes = self.attributes_ordered();
let builder = SchemaBuilder { identifier, attributes };
let string = serde_json::to_string_pretty(&builder)?; let string = serde_json::to_string_pretty(&builder)?;
writer.write_all(string.as_bytes())?; writer.write_all(string.as_bytes())?;