Skip to content

Commit 3adbedc

Browse files
authored
Create constructor methods for manifest types (typst#6625)
1 parent e9f1b58 commit 3adbedc

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

crates/typst-syntax/src/package.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ pub struct PackageInfo {
143143
}
144144

145145
impl PackageManifest {
146+
/// Create a new package manifest with the given package info.
147+
pub fn new(package: PackageInfo) -> Self {
148+
PackageManifest {
149+
package,
150+
template: None,
151+
tool: ToolInfo::default(),
152+
unknown_fields: UnknownFields::new(),
153+
}
154+
}
155+
146156
/// Ensure that this manifest is indeed for the specified package.
147157
pub fn validate(&self, spec: &PackageSpec) -> Result<(), EcoString> {
148158
if self.package.name != spec.name {
@@ -173,6 +183,44 @@ impl PackageManifest {
173183
}
174184
}
175185

186+
impl TemplateInfo {
187+
/// Create a new template info with only required fields.
188+
pub fn new(path: impl Into<EcoString>, entrypoint: impl Into<EcoString>) -> Self {
189+
TemplateInfo {
190+
path: path.into(),
191+
entrypoint: entrypoint.into(),
192+
thumbnail: None,
193+
unknown_fields: UnknownFields::new(),
194+
}
195+
}
196+
}
197+
198+
impl PackageInfo {
199+
/// Create a new package info with only required fields.
200+
pub fn new(
201+
name: impl Into<EcoString>,
202+
version: PackageVersion,
203+
entrypoint: impl Into<EcoString>,
204+
) -> Self {
205+
PackageInfo {
206+
name: name.into(),
207+
version,
208+
entrypoint: entrypoint.into(),
209+
authors: vec![],
210+
categories: vec![],
211+
compiler: None,
212+
description: None,
213+
disciplines: vec![],
214+
exclude: vec![],
215+
homepage: None,
216+
keywords: vec![],
217+
license: None,
218+
repository: None,
219+
unknown_fields: BTreeMap::new(),
220+
}
221+
}
222+
}
223+
176224
/// Identifies a package.
177225
#[derive(Clone, Eq, PartialEq, Hash)]
178226
pub struct PackageSpec {
@@ -535,22 +583,11 @@ mod tests {
535583
"#
536584
),
537585
Ok(PackageManifest {
538-
package: PackageInfo {
539-
name: "package".into(),
540-
version: PackageVersion { major: 0, minor: 1, patch: 0 },
541-
entrypoint: "src/lib.typ".into(),
542-
authors: vec![],
543-
license: None,
544-
description: None,
545-
homepage: None,
546-
repository: None,
547-
keywords: vec![],
548-
categories: vec![],
549-
disciplines: vec![],
550-
compiler: None,
551-
exclude: vec![],
552-
unknown_fields: BTreeMap::new(),
553-
},
586+
package: PackageInfo::new(
587+
"package",
588+
PackageVersion { major: 0, minor: 1, patch: 0 },
589+
"src/lib.typ"
590+
),
554591
template: None,
555592
tool: ToolInfo { sections: BTreeMap::new() },
556593
unknown_fields: BTreeMap::new(),

0 commit comments

Comments
 (0)